Spring 自动连线字段给我空异常

Spring 自动连线字段给我空异常,spring,dependency-injection,inversion-of-control,Spring,Dependency Injection,Inversion Of Control,我有一个使用bean方法的类 我试图使用@Autowired将该方法注入到我的类中,但它给了我NullPointerException public class ChangePasswordServiceImpl extends RemoteServiceServlet implements ChangePasswordService { @Autowired @Qualifier("userDetailsManager") private JdbcUs

我有一个使用bean方法的类

我试图使用
@Autowired
将该方法注入到我的类中,但它给了我NullPointerException

public class ChangePasswordServiceImpl extends RemoteServiceServlet implements
        ChangePasswordService {

    @Autowired
    @Qualifier("userDetailsManager")
    private JdbcUserDetailsManager userDetailsManager;

    @Override
    public void changePassword(String oldPassword, String newPassword) {

        // This is the line that throws NullPointerException, i.e., userDetails
        // Manager is not being injected by Spring
        userDetailsManager.changePassword(oldPassword, newPassword);

    }

}
我的xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"

    xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
             http://www.springframework.org/schema/jdbc
             http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config/>

    <bean id="securityDataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">

        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="jdbc:mysql://localhost:3306/login" />
        <property name="username" value="root" />
        <property name="password" value="password" />
    </bean>

    <bean id="userDetailsManager"
        class="org.springframework.security.provisioning.JdbcUserDetailsManager">
        <property name="dataSource" ref="securityDataSource" />
        <property name="authenticationManager" ref="authenticationManager" />
        <qualifier value="userDetailsManager"/>
    </bean>

</beans>


为什么这个领域没有被填补?我遗漏了什么吗?

因为
ChangePasswordServiceImpl
不是Spring管理的类(它不在xml配置中,因此它不是
ApplicationContext
的一部分)
@Autowired
对您的类没有任何作用,这将导致您在尝试使用该类的实例时获得一个
NullPointerException

您应该在xml配置中为
ChangePasswordServiceImpl
定义一个bean,以及当前存在的其余bean

<bean id="changePasswordService" class="the.package.ChangePasswordServiceImpl"/>

ChangePasswordServiceImpl
是如何实例化的?它是如何注释的?嗯,我不确定。此类是来自GWT的RPC服务的实现。是否应该对其进行注释?我相信是GWT对其进行了实例化。您可能想查看或阅读一些Spring/GWT教程,但是,是的,您遗漏了一些内容:)嗯,因此Spring不会在我所有的类中查找
@Autowired
,而只在
应用程序上下文
中查找。由于它是一个GWT RPC服务实现类,并且我没有实例化它(GWT会这样做),我相信使用普通的Servlet会更简单,对吗?bean不需要在XML配置中进行Spring管理,它可以被注释,这就是我问这个问题的原因。是的,它不需要在XML中,但根据问题中提供的信息,我认为他是在尝试专门通过xml进行连接@jdanielnd:我不太清楚gwt是如何工作的,但是如果您有一个web.xml部署描述符用于您的应用程序,您可以在其中配置您的应用程序上下文。看看这个链接,了解更多关于这样做的信息:我对它感到不舒服。我想要的只是使用SpringSecurity中的UserDetailsManager实现。它有一个更改密码的方法。我对保持应用程序的解耦或使用SpringDI不太感兴趣。然而,由于它的实现在其字段中有bean,我无法找到访问它的方法的另一种方法,因此我的项目中的重大更改变得非常必要。在不深入SpringDI的情况下,难道没有一条捷径可以访问它的方法吗?
//assuming you are holding onto an instance of the ApplicationContext
ChangePasswordServiceImpl service = appContext.getBean("changePasswordService", ChangePasswordServiceImpl.class);
service.changePassword("old", "new");