我在獨立的Java應用程序中遇到問題。問題是我嘗試Autowire我的服務和我的DAO,但當我從UI調用服務方法,因爲依賴注入無法正常工作,我得到一個NullPointerException
。我嘗試了很多東西,其中許多來自類似的問題,但問題仍然存在。我正在使用Spring 4.0.6.RELEASE和Hibernate 4.3.11.Final。這裏是我的代碼:彈簧依賴注入不起作用
1 - 服務的調用:
public class LoginView {
@Autowired
private UsuarioService usuarioService;
...
...
JButton btnAutenticarse = new JButton("Autenticar");
btnAutenticarse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Usuario usuario = usuarioService.login(textField.getText(),
String.valueOf(passwordField.getPassword()), false); // NullPointerException
} catch (InstanceNotFoundException e1) {
...
2 - 服務的定義:
@Service("usuarioService")
public class UsuarioServiceImpl implements UsuarioService {
@Autowired
private UsuarioDao usuarioDao;
...
3 - DAO的定義:
@Repository("usuarioDao")
public class UsuarioDaoHibernate extends GenericDaoHibernate <Usuario, Long>
implements UsuarioDao {
...
4 - GenericDAO的定義:
public class GenericDaoHibernate<E, PK extends Serializable> implements
GenericDao<E, PK> {
@Autowired
private SessionFactory sessionFactory;
....
5 - AppConfig.java
:
@Configuration
@ComponentScan(basePackages = "org.example.model")
public class AppConfig {
@Bean(name = "usuarioService")
public UsuarioService usuarioService() {
return new UsuarioServiceImpl();
}
@Bean(name = "usuarioDao")
public UsuarioDao usuarioDao() {
return new UsuarioDaoHibernate();
}
6 - spring-config.xml
:春
<!-- Enable usage of @Autowired. -->
<context:annotation-config/>
<!-- Enable component scanning for defining beans with annotations. -->
<context:component-scan base-package="org.example.model"/>
<!-- For translating native persistence exceptions to Spring's
DataAccessException hierarchy. -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/appdb" />
<property name="username" value="username" />
<property name="password" value="password"/>
</bean>
<bean id="dataSourceProxy" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy"
p:targetDataSource-ref="dataSource"/>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" >
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>org.example.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<!-- Enable the configuration of transactional behavior based on
annotations. -->
<tx:annotation-driven transaction-manager="transactionManager" />
你如何創建'LoginView'? – Reimeus
這是來自另一個包org.example.view的Swing類。它有一個構造函數'LoginView()',它調用一個名爲'initialize'的方法,其中包含引發'NullPointerException'的代碼部分。 – clanofsol
我認爲問題的關鍵是如果你是用'new'實例化'LoginView'還是讓Spring創建'LoginView'。如果你使用'new',Spring將不會注入這些字段。 –