[英]Spring Autowired in Web Service not working
I must be missing something simple, but I'm having trouble getting an Autowired property to be assigned to a bean. 我必须遗漏一些简单的东西,但是我无法将一个Autowired属性分配给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. 我尝试制作一个简约bean来代表我的DAO并将其注入Web服务。
DAO interface: DAO界面:
package wb;
public interface FooDAO {
public String doNothing();
}
DAO implementation: DAO实施:
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: 注入Web服务:
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: beans标签中的applicationContext.xml内容:
<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. 这些都是在最新的NetBeans中创建的,在使用Spring和Hibernate框架创建的项目中。 When I deploy to JBoss, and the app starts up, I get the expected Bean instantiation: 当我部署到JBoss并启动应用程序时,我得到了预期的Bean实例:
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: 我调用Web服务后,日志还会报告:
11:03:07,097 INFO [stdout] (http--127.0.0.1-8080-1) WS: doNothing() says DAO = null
What am I missing? 我错过了什么?
SpringBeanAutowiringSupport
must be a bean. SpringBeanAutowiringSupport
必须是一个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. 您需要使用@Service
或其他注释(如@Component
注释该类,以指示在发生组件扫描时类应该是bean。 These will be picked up by Spring
and made into beans. 这些将被Spring
收集并制成豆子。
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. 请记住,为了成为自动装配的参与者,例如注入另一个bean,该类必须是bean本身并由Spring的IOC容器管理。
The spring
should start before webservices
. spring
应该在webservices
之前启动。
The the webservices-rt*.jar
is a plugable jar which starts automaticly to find the end points. webservices-rt*.jar
是一个可插入的jar,它自动启动以查找端点。
In some cases it may happen that the webservices starts before spring. 在某些情况下,可能会发生web服务在春天之前启动。 So the injects
can not be done. 所以injects
不能完成。 Make sure that the start up has correct order. 确保启动顺序正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.