无法在桌面应用程序中使用@Autowired

Mah*_*leh 2 spring dependency-injection desktop-application

我试图在桌面应用程序中使用spring,但我在我的JPanel的动作方法中面临自动装配的问题.

我在我的main方法中加载applicationContext,如下所示:

public static void main(String[] args) {

        new ClassPathXmlApplicationContext(
                "classpath:/META-INF/spring/applicationContext.xml");
            MainFrame frame = new MainFrame();
    Signup signup = new Signup();
    frame.add(signup);
    frame.setResizable(false);
    frame.setTitle("Please input your data");
    frame.setBounds(100, 100, 450, 180);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

}
Run Code Online (Sandbox Code Playgroud)

我可以看到它没有任何问题.

我的面板代码:

@Component
public class Signup extends JPanel {


    @Autowired
    private UserDao userDao;

    public Signup() {



        JButton btn_submit = new JButton("Submit");
        btn_submit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                registerUser();
            }
        });



    }

    private void registerUser() {

        User newUser = new User();
        newUser.setName(username);
        newUser.setSalary(salary);
        userDao.addUser(newUser);

    }
}
Run Code Online (Sandbox Code Playgroud)

context:component-scan配置正确,和我使用context:annotation-config过,但我总是得到NullPointerException异常userDao.addUser(newUser); ,这意味着依赖注入不能正常工作,因为它应该.

请告知如何解决此问题.

更新: applicationContext.xml

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

    <context:component-scan base-package="${project.groupId}" />

    <context:annotation-config />

    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>

                <value>classpath:messages/application.properties</value>

            </list>
        </property>
    </bean>


    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="${project.groupId}.domain" />


        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.DerbyDialect
                hibernate.show_sql=false
                hibernate.format_sql=false
                hibernate.hbm2ddl.auto=validate
            </value>
        </property>

    </bean>


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">

        <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />

        <property name="url" value="jdbc:derby:test" />

        <property name="username" value="root" />

        <property name="password" value="root" />

    </bean>


    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />


</beans>
Run Code Online (Sandbox Code Playgroud)

nic*_*ild 8

如果要在桌面环境中配置Spring,那么必须是使用它的人ApplicationContext.

例如,如果您希望获得Signup您在此处发布的课程,您可以在main方法中执行以下操作:

public static void main(String[] args) {
    ApplicationContext appContext = new ClassPathXmlApplicationContext(
            "classpath:/META-INF/spring/applicationContext.xml");
    Signup signup = appContext.getBean(Signup.class);
    //use signup here...
}
Run Code Online (Sandbox Code Playgroud)

使用new Signup()得到的新实例Signup类,这是行不通的,你想要的方式,因为你希望它是一个Spring管理类!(实际上,你可以让它以这种方式工作,但这不是我的答案)