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

时间:2012-07-08 22:20:58

标签: 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);

}

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

我的面板代码:

@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);

    }
}

context:component-scan配置正确,我也在使用context:annotation-config但我总是在userDao.addUser(newUser);中获得 NullPointerException 这意味着依赖注入不能正常工作。

请告知如何解决此问题。

更新: 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>

1 个答案:

答案 0 :(得分: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...
}

使用new Signup()获取Signup类的新实例,它不会按照您想要的方式工作,因为您希望它是Spring托管类! (实际上,你可以让它以这种方式工作,但这不是我的答案)