Java Web 服务中的 Spring Autowired 无法正常工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21193843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 07:13:08  来源:igfitidea点击:

Spring Autowired in Web Service not working

javaspringjakarta-eeautowired

提问by user3207820

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:

我一定遗漏了一些简单的东西,但是我无法将 Autowired 属性分配给 bean。此处发布的所有类似答案似乎都围绕以下三种解决方案之一:

  1. extend SpringBeanAutowiringSupport
  2. use <context:component-scan base-package="..." /> in applicationContext.xml
  3. use <context:annotation-config /> in applicationContext.xml
  1. 扩展 SpringBeanAutowiringSupport
  2. 在 applicationContext.xml 中使用 <context:component-scan base-package="..." />
  3. 在 applicationContext.xml 中使用 <context:annotation-config />

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:

bean 标签中的 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. When I deploy to JBoss, and the app starts up, I get the expected Bean instantiation:

这都是在最新的 NetBeans 中创建的,在一个使用 Spring 和 Hibernate 框架创建的项目中。当我部署到 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?

我错过了什么?

采纳答案by Kevin Bowersox

SpringBeanAutowiringSupportmust be a bean. You need to annotate that class with @Serviceor another annotation such as @Componentthat indicates a class should be a bean when component scanning occurs. These will be picked up by Springand made into beans.

SpringBeanAutowiringSupport必须是豆子。您需要使用@Service或其他注释对该类进行注释,例如在@Component发生组件扫描时指示类应该是 bean。这些将被捡起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 容器管理。

回答by Alireza Fattahi

The springshould start before webservices.

spring前应启动webservices

The the webservices-rt*.jaris a plugable jar which starts automaticly to find the end points.

本该webservices-rt*.jar是一个可插拔的罐子它开始全自动找到终点。

In some cases it may happen that the webservices starts before spring. So the injectscan not be done. Make sure that the start up has correct order.

在某些情况下,webservice 可能会在 spring 之前启动。所以injects是做不到的。确保启动顺序正确。