java - Spring injection with @autowired doesn't work -
i'm building sample application jersey , spring.
all works good, can call controller found, when i'm trying access service, null. it's autowired spring, should not null.
this 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)); } }
this restservices class
@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(); }
and application-context xml configuration file
<?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 properties --> <context:property-placeholder location="classpath:properties/app.properties" /> <!-- load beans declared @component , child annotation spring's ioc container --> <context:annotation-config /> <context:component-scan base-package="com.ep.eom.poc.*" /> <!-- load log4j bean 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>
and 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 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 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 fetching libraries --> <repositories> <repository> <id>maven2-repository.java.net</id> <name>java.net repository 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 container implements servlet api older 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>
i can enter in restresource classes, restservices class null.
where i'm wrong?
i haven't done spring in forever, believe issue not loading restresources
class ioc container.
you need define restresources
in application-context , load applicationcontext.
i see have context:component-scan
com.ep.eom.poc.*
. restresources
under com.ep.eom.poc
package?
if you're trying grab class "raw", won't have dependencies injected it.
Comments
Post a Comment