使用@autowired进行弹簧注射并不起作用

时间:2017-07-28 13:13:56

标签: java spring spring-mvc

我正在使用Jersey和Spring构建一个示例应用程序。

一切正常,我可以调用找到的Controller,但是当我试图访问该服务时,这是空的。但它是用弹簧自动装配的,所以这不应该是空的。

这是我的RESTController

@Path("/RESTExample")
@Component
@Scope("request")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class RESTResources {

    @Autowired
    protected RESTServices restServices;

    @GET
    @Path("/resource/{id}")
    public Response getResourceWithID(@PathParam("id") Integer resourceID) {
    return EOMUtils.BuildSuccessResponse(restServices.getData(resourceID));

    }

}

这是我的RESTServices类

@Service
public class RESTServicesImpl implements RESTServices {

    @Autowired
    protected RESTDao restDao;

    @Override
    public String getData(Integer resourceId) {

    return restDao.getData(resourceId);
    }

    @Override
    public String getData() {

    return restDao.getData();
    }

这是我的应用程序上下文xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-3.2.xsd">


  <!-- load the properties  -->
  <context:property-placeholder location="classpath:properties/app.properties" />

  <!--  load all the beans declared with @Component and child annotation into the Spring's IOC Container  -->
  <context:annotation-config />
   <context:component-scan base-package="com.ep.eom.poc.*" />

  <!-- Load the Log4j bean into the IOC Container --> 
  <bean id="eom_log_factory"
        class="org.springframework.beans.factory.config.CommonsLogFactoryBean">
        <property name="logName" value="eom_log_factory"/>
  </bean>

    <bean id="log4jInitialization"
        class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
   <property name="targetMethod" value="initLogging" />
   <property name="arguments">
      <list>
         <value>classpath:/log4j/config/log4j_${envTarget}.xml</value>
      </list>
   </property>
</bean>   


</beans>

这是我的专家

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.capgemini.poc</groupId>
    <artifactId>EOM</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>EOM_POC</name>

    <!-- main properties for the project -->
    <properties>
        <org.springframework-version>4.0.6.RELEASE</org.springframework-version>
        <org.slf4j-version>1.6.6</org.slf4j-version>
        <jersey-version>2.11</jersey-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <!-- Support for profiles -->
    <profiles>
        <profile>
            <id>dev</id>
            <build>
                <plugins>
                <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.4.1</version>
                <executions>
                    <execution>
                        <id>default-clean</id>
                        <phase>initialize</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
                    <plugin>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>test</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <!-- config.properties -->
                                        <copy todir="${project.build.outputDirectory}">
                                            <fileset dir="src/main/resources/" includes="**" />
                                        </copy>

                                        <delete file="${project.build.outputDirectory}/log4j/config/log4j_TEST.xml" />
                                        <delete file="${project.build.outputDirectory}/log4j/config/log4j_PRODUCTION.xml" />
                                        <delete file="${project.build.outputDirectory}/log4j/config/log4j_STAGING.xml" />
                                        <delete file="${project.build.outputDirectory}/properties/app_PROD.properties" />
                                        <delete file="${project.build.outputDirectory}/properties/app_STAG.properties" />
                                        <delete file="${project.build.outputDirectory}/properties/app_TEST.properties" />
                                        <delete file="${project.build.outputDirectory}/properties/app_DEV.properties" />

                                        <copy file="src/main/resources/properties/app_DEV.properties" tofile="${project.build.outputDirectory}/properties/app.properties" />

                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>


    <!-- Repository for fetching the libraries -->
    <repositories>
        <repository>
            <id>maven2-repository.java.net</id>
            <name>Java.net Repository for Maven</name>
            <url>http://download.java.net/maven/2/</url>
        </repository>
    </repositories>


        <build>
        <finalName>${artifactId}-${version}</finalName>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <inherited>true</inherited>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
                <executions>
                    <execution>
                        <id>default-testCompile</id>
                        <phase>test-compile</phase>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                        <configuration>
                            <skip>true</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <wtpversion>1.5</wtpversion>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                    <archive>
                        <manifestEntries>
                            <Specification-Title>${project.name}</Specification-Title>
                            <Specification-Version>${project.artifactId}</Specification-Version>
                            <Implementation-Version>-${project.version}</Implementation-Version>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <!-- Spring Core FrameWork -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>


        <!-- SLF4J -->

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${org.slf4j-version}</version>
            <scope>runtime</scope>
        </dependency>


        <!-- Jersey  -->

        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-json-jackson</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-multipart</artifactId>
            <version>${jersey-version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-entity-filtering</artifactId>
            <version>${jersey-version}</version>
        </dependency>

          <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <!-- if your container implements Servlet API older than 3.0, use "jersey-container-servlet-core"  -->
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey-version}</version>
        </dependency>

              <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>

    </dependencies>

</project>

我可以输入RESTResource类,但是RESTServices类是NULL。

我哪里错了?

1 个答案:

答案 0 :(得分:0)

我还没有永远完成春天,但我相信您的问题是您没有从IoC容器中加载RESTResources类。

您需要在应用程序上下文中定义RESTResources并从ApplicationContext加载它。

我发现context:component-scancom.ep.eom.poc.*RestResources包下是com.ep.eom.poc吗?

如果您只是想抓住课程&#34; raw&#34;,它就不会注入依赖项。