Reputation: 55
I must be missing something simple, but I'm having trouble getting an Autowired property to be assigned to a bean. All similar answers posted here seem to revolve around one of three solutions:
I tried to make a minimalist bean to represent my DAO and inject it into a Web Service.
DAO interface:
package wb;
public interface FooDAO {
public String doNothing();
}
DAO implementation:
package wb;
import org.springframework.stereotype.Component;
@Component
public class FooDAOImpl implements FooDAO {
public FooDAOImpl() {
System.out.println("FooDAOImpl: Instantiated " + this);
}
@Override
public String doNothing() {
System.out.println("FooDAOImpl: doNothing() called");
return "Did nothing!";
}
}
Web Service with injection:
package ws;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import wb.FooDAO;
@WebService(serviceName = "WS")
public class WS extends SpringBeanAutowiringSupport {
@Autowired(required = true)
private FooDAO fooDAO;
@WebMethod(exclude = true)
public void setFooDAO(FooDAO fooDAO) {
this.fooDAO = fooDAO;
System.out.println("WS: fooDAO set = " + fooDAO);
}
public WS() {
System.out.println("WS: WS bean instantiated!");
}
@WebMethod(operationName = "doNothing")
@WebResult(name = "whatDidIDo")
public String doNothing() {
System.out.println("WS: doNothing() says DAO = " + fooDAO);
return fooDAO == null ? "Could not do nothing!" : fooDAO.doNothing();
}
}
applicationContext.xml content within the beans tags:
<context:annotation-config />
<context:component-scan base-package="ws"/>
<bean id="fooDAO" class="wb.FooDAOImpl" />
This was all created in the latest NetBeans, in a project created with Spring and Hibernate frameworks. When I deploy to JBoss, and the app starts up, I get the expected Bean instantiation:
11:01:46,767 INFO [stdout] (MSC service thread 1-6) WS: WS bean instantiated!
11:01:47,571 INFO [stdout] (MSC service thread 1-15) FooDAOImpl: Instantiated wb.FooDAOImpl@11176682
Once I call the web service, the log also reports:
11:03:07,097 INFO [stdout] (http--127.0.0.1-8080-1) WS: doNothing() says DAO = null
What am I missing?
Upvotes: 4
Views: 15490
Reputation: 45593
The spring
should start before webservices
.
The the webservices-rt*.jar
is a plugable jar which starts automaticly to find the end points.
In some cases it may happen that the webservices starts before spring. So the injects
can not be done. Make sure that the start up has correct order.
Upvotes: 0
Reputation: 94489
SpringBeanAutowiringSupport
must be a bean. You need to annotate that class with @Service
or another annotation such as @Component
that indicates a class should be a bean when component scanning occurs. These will be picked up by Spring
and made into beans.
Remember that in order to be a participant in autowiring, such as having another bean injected, the class must be a bean itself and managed by Spring's IOC container.
Upvotes: 9