2017-07-28 120 views
0

我正在用Jersey和Spring构建一个示例应用程序。@autowired的弹簧注射不起作用

一切运作良好,我可以调用找到了控制,但是当我试图访问该服务,这为空。但它是用spring自动装配的,所以这不应该是null。

这是我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> 

,这是我的Maven

<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。

在那里我错了吗?

+0

从哪里得到'RestResource'的实例? – Lino

+0

这两个类都在'com.ep.eom.poc。*'包下? – araknoid

+0

您正在使用Jersey,并且您需要正确配置Jersey以将Spring用作DI容器。请参阅[Jersey参考指南](https://jersey.github.io/documentation/latest/spring.html)。 –

回答

0

我没有做过春天永远,但我相信你的问题是,你是不是从IoC容器装载您RESTResources类。

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

我看到你有context:component-scancom.ep.eom.poc.*com.ep.eom.poc包是RestResources吗?

如果你只是试图抢班“原始”,它不会有注入它的依赖关系。

+0

是的,我已经解决了。我缺少这个库中POM:\t \t \t \t org.glassfish.jersey.ext \t \t \t 球衣,spring3 \t \t \t $ {球衣版本} \t \t user1377034