【问题标题】:Autowired in WS not working Spring在 WS 中自动装配不工作 Spring
【发布时间】:2013-09-10 15:46:27
【问题描述】:

我已经在我的服务器项目中使用 Jax-ws 实现了一个 WS,并且我正在从客户端应用程序调用该服务。问题是,如果我想在 @WebService 带注释的类中使用 @Autowire,它总是会引发以下错误:

InjectionException:为类创建托管对象时出错:类 com.myco.wsserver.LeaveRequestEndPoint;

如果我调试那个类,对我的自动装配 bean 的引用是空的。如果我从 @webservice 带注释的类中删除 bean 它可以工作,如果我手动获取应用程序上下文然后获取 bean 它也可以工作,但我想知道为什么我不能自动装配任何 bean。

这是我的代码。

WebService 类:

@WebService(serviceName="LeaveRequestHandler")
public class LeaveRequestEndPoint extends SpringBeanAutowiringSupport{

    @Autowired
    GenericBean mBean;

    public LeaveRequestEndPoint(GenericBean mBean){
        this.mBean = mBean;
    }


    @WebMethod(operationName="executeOperation")
    public String getText() {
        return mBean.getText();

    }
}

应用上下文:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <context:annotation-config />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>


    <beans:bean id="genericBean" class="com.myco.wsserver.GenericBean"/>

</beans:beans>

【问题讨论】:

    标签: spring


    【解决方案1】:

    在注入发生之前调用类构造函数。这不是使用spring构建类的方式。有@PostConstruct 注释保证在类构造时注入准备就绪,或者可以使用构造函数注入。

    构造函数现在是一个合适的构造函数。注入是在构造函数运行后完成的。

    您有 2 个选项。

    1. 将@Autowired 注解移至构造函数。 (构造函数注入)

      @自动连线 public LeaveRequestEndPoint(GenericBean mBean)

    2. 删除构造函数并使用 setter getter 注入。 (setter getter 注入)

    GenericBean mBean; 具有默认访问修饰符,这就是注入失败的原因。将 mBean 更改为 private 并添加 setter getter。

    @Autowired
    private GenericBean mBean;
    
    public setMBean(GenericBean mBean)
    {
    this.mBean = mBean;
    }
    public getMBean()
    {
    return this.mbean;
    }
    

    【讨论】:

    • 正如你所建议的,我添加了 setter 和 getter,但它抛出了同样的错误
    • 抱歉。请查看我的编辑。您有一个在注入发生之前调用的类构造函数。
    猜你喜欢
    • 2011-03-10
    • 2018-09-29
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多