Web Service中的Spring Autowired无法正常工作 [英] Spring Autowired in Web Service not working

查看:202
本文介绍了Web Service中的Spring Autowired无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须缺少一些简单的东西,但是我很难将Autowired属性分配给Bean.此处发布的所有类似答案似乎都围绕着以下三种解决方案之一:

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:

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

我试图制作一个简约的bean来代表我的DAO,并将其注入Web服务中.

I tried to make a minimalist bean to represent my DAO and inject it into a Web Service.

DAO界面:

package wb;
public interface FooDAO {
    public String doNothing();
}

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服务:

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();
    }
}

Bean标签中的

applicationContext.xml内容:

applicationContext.xml content within the beans tags:

<context:annotation-config />
<context:component-scan base-package="ws"/>

<bean id="fooDAO" class="wb.FooDAOImpl" />

所有这些都是在最新的NetBeans中创建的,该项目是使用Spring和Hibernate框架创建的项目.当我部署到JBoss并启动应用程序时,我得到了预期的Bean实例:

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

一旦我调用了Web服务,日志还将报告:

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

我想念什么?

推荐答案

SpringBeanAutowiringSupport必须是bean.您需要使用@Service或另一个诸如@Component的注释来注释该类,该注释指示在进行组件扫描时该类应该是bean.这些将被Spring拾取并制成豆.

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.

请记住,要成为自动装配的参与者(例如注入另一个bean),该类必须是bean本身,并由Spring的IOC容器进行管理.

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.

这篇关于Web Service中的Spring Autowired无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆