2017-06-15 10 views
0

私のスタンドアロンJavaアプリケーションで問題が発生しました。私のサービスと私のDAOの両方をAutowireしようとしているのですが、依存関係注入が正しく機能していないため、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" /> 
+0

どのように 'LoginView'を作成しますか? – Reimeus

+0

別のパッケージorg.example.viewからSwingクラスです。 'NullPointerException'がスローされるコードの一部を含む' initialize'メソッドを呼び出すコンストラクタ 'LoginView()'を持っています。 – clanofsol

+0

質問のポイントは、 'LoginView'を' new'でインスタンス化していた場合、またはSpringに 'LoginView'を作成させた場合でした。 'new'を使用している場合、Springはフィールドを挿入しません。 –

答えて

0

春は春でAutowiredフィールドは、管理対象Beanを注入します。 new LoginView() Springは依存関係を注入できません。

public class LoginView { 

    @Autowired 
    private UsuarioService usuarioService; 
} 

あなたは春には、そのクラスの管理を任せることはできない場合、あなたはそれを別の方法を設計する必要があります

@Component 
public class LoginView { 

    @Autowired 
    private UsuarioService usuarioService; 
} 

でなければなりません。

フィールド注入の代わりにコンストラクタ注入を使用することをお勧めします。

@Component 
public class InitClass{ 

    private UsarioService usarioService; 

    @Autowired 
    public InitClass(UsarioService usarioService){ 
    this.usarioService = usarioService; 
    }   

    @PostConstruct 
    public void init(){ 
    new LoginView(usarioService); 
    }   
} 

次に、このクラスを使用すると、@PostConstructで今やっているすべての初期化を処理します:私が行う可能性がありますどのような

は春すべき別のクラスはBeanを管理し、このような何かを行うことです。これまでにSpring Beanが完全に初期化されていない可能性があるため、@PostConstructでこれを行う必要があります。

しかし、すべてがどのように初期化されているかを見ることなく、最良の戦略が何であるかは分かりません。

関連する問題