2

我正在尝试在桌面应用程序中使用 spring,但我在JPanel的操作方法中遇到了自动装配问题。

我在我的主要方法中加载 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我总是得到NullPointerExceptionuserDao.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>
4

1 回答 1

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 托管类!(实际上,您可以让它以这种方式工作,但这超出了我的回答范围)

于 2012-07-08T22:26:47.513 回答