繁体   English   中英

用@autowired进行春季注入不起作用

[英]Spring injection with @autowired doesn't work

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

一切正常,我可以调用找到的Controller,但是当我尝试访问该服务时,此为null。 但是它与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>

这是我的专家

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

我哪里错了?

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

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

我看到您具有context:component-scan com.ep.eom.poc.* RestResources是否在com.ep.eom.poc包下?

如果您只是尝试获取“原始”类,则不会将依赖项注入其中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM