我正在尝试在桌面应用程序中使用 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
我总是得到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>