Snippet of pom.xml:
<dependency> <groupId>org.apache.tomee</groupId> <artifactId>javaee-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency>...other dependencies
The web.xml mainly defines the spring ContextLoaderListener: <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/appContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>The appContext.xml defines some beans, but also specifies a context:component-scan specification as below to capture any annotation-driven components:
<!-- specify the component scan -->
<context:component-scan base-package="com.test" />
Within my application, I have a service and controller class.
@Component
public class TestService {
public String getValue() {
return "value01";
}
}
@Component
@Path("/test")
public class TestController {
@Autowired
private TestService testService;
public TestController() {
System.out.println("TestController::constructor");
}
@PostConstruct
public void init() {
System.out.println("TestController::init. testService: " + testService);
}
@GET
@Path("/value")
@Produces("text/plain")
public String getValue() {
System.out.println("TestController::getValue. testService: " + testService);
return testService.getValue();
}
}
On webapp startup, the init() method is run and I'm seeing testService
is populated with an object; therefore, the spring webappcontext is
working as expected to populate the fields.
Now, when "GET /test/value" is called, the TestController object is created anew (constructor log statement shows this) but the testService is not being populated with @Autowire before the getValue() method is called. The log output in getValue() is showing testService is null for this new TestController instance.
How to make CXF aware of the spring context so objects are retrieved as beans to honor @Autowire specifications during REST api method calls instead of calling the constructor and creating anew?
--This electronic communication and the information and any files transmitted with it, or attached to it, are confidential and are intended solely for the use of the individual or entity to whom it is addressed and may contain information that is confidential, legally privileged, protected by privacy laws, or otherwise restricted from disclosure to anyone else. If you are not the intended recipient or the person responsible for delivering the e-mail to the intended recipient, you are hereby notified that any use, copying, distributing, dissemination, forwarding, printing, or copying of this e-mail is strictly prohibited. If you received this e-mail in error, please return the e-mail to the sender, delete it from your computer, and destroy any printed copy of it.
smime.p7s
Description: S/MIME Cryptographic Signature
