2016-04-26 22 views
0

現在、RESTサービスSpringアプリケーションをセットアップしようとしており、モデルとそのコントローラ+ daoとサービスを実装してMySQLデータベースと通信しています。 AccountServiceのが@Component豆いる間Spring @Autowiredインスタンスがnull

enter image description here

今私のAccountControllerは、注釈付き@RestControllerを持つBeanです。 AccountServiceは@ Autowire'd AccountDaoメンバを保持し、AccountControllerは@ Autowire'd AccountServiceメンバを保持します。次のように

dataAccess.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" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
     destroy-method="close"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/fashion" /> 
     <property name="username" value="root" /> 
     <property name="password" value="fashion" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="mappingResources"> 
      <list> 
       <value>de/appsiting/fashion/model/Account.hbm.xml</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect"> 
       org.hibernate.dialect.MySQL5Dialect 
       </prop> 
       <prop key="hibernate.format_sql">true</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">create</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"> 
     <property name="dataSource"> 
      <ref bean="dataSource" /> 
     </property> 
    </bean> 

    <bean id="accountDao" class="de.appsiting.fashion.persistence.AccountDao" /> 
</beans> 
Account.hbm.xmlは赤色に着色されしかし

それはアプリケーションによって発見されていない示します。このように、私の最初の質問は、なぜそれは次のようにserviceBeans.xmlが満たされている.hbm.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:aop="http://www.springframework.org/schema/aop" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:task="http://www.springframework.org/schema/task" 
     xsi:schemaLocation=" 
    http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 

    <task:annotation-driven/> 

    <bean id="hibernateStringEncryptor" class="org.jasypt.hibernate4.encryptor.HibernatePBEStringEncryptor"> 
     <property name="registeredName"> 
      <value>strongHibernateStringEncryptor</value> 
     </property> 
     <property name="algorithm"> 
      <value>PBEWithMD5AndTripleDES</value> 
     </property> 
     <property name="password"> 
      <value>jasypt</value> 
     </property> 
    </bean> 

    <bean id="accountService" class="de.appsiting.fashion.service.AccountService"> 
     <property name="accountDao" ref="accountDao" /> 
    </bean> 

    <!-- *** TX LAYER --> 
    <bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 
    <tx:advice id="tx.advice" transaction-manager="txManager"> 
     <tx:attributes> 
      <tx:method name="*" propagation="REQUIRED" read-only="false" /> 
      <tx:method name="get*" propagation="REQUIRED" read-only="false" /> 
     </tx:attributes> 
    </tx:advice> 

    <!-- *** AOP --> 
    <aop:config> 
     <aop:pointcut id="accountServiceMethods" expression="execution(* de.appsiting.fashion.service.AccountService.*(..))" /> 

     <aop:advisor advice-ref="tx.advice" pointcut-ref="accountServiceMethods" /> 
    </aop:config> 
</beans> 

今私のAccountTestを実行しているときaccountServiceはnullであるため、テストは失敗します。

import de.appsiting.fashion.service.AccountService; 
import org.apache.log4j.Logger; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.junit.Test; 


/** 
* Created by mak on 4/22/16. 
*/ 
public class AccountTest { 
    private static Logger log = Logger.getLogger(AccountTest.class); 

    @Autowired 
    private AccountService accountService; 

    @Test 
    public void testCreateAccount() throws Exception { 
     Account account = new Account(); 
     account.setUsername("testfashion"); 
     account.setPassword("testfashion"); 
     account.setEmail("[email protected]"); 

     accountService.addAccount(account); 
    } 
} 

なぜこのようなことが起こりますか?

+0

あなたのテストケースで 'serviceBeans.xml'を使用していますか? –

+0

私はとても馬鹿だと感じます。私は数時間それを見てきました。それはそれです。私はxmlsを読み込むことを忘れました。ありがとう – macskay

+0

あなたは間違いを実感しました。 –

答えて

1

アプリケーションコンテキストを初期化するためには、SpringJUnit4ClassRunnerを使用する必要があります。これにより、依存性注入が実行されます。

もう1つの選択肢はMockitoです。これは、IMHOでは多くの問題を回避し、モックを使用する利点があります。

0

テストでは2つの設定がありません。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"Location of xml files that you need to load for tests to run"}) 
public class AccountTest { 

} 

上記の設定では、テストのコンテキストが読み込まれます。 または、テストのアプリケーションコンテキストをsetupメソッドでロードし、そのコンテキストを使用してアカウントサービスを取得することができます。

0

私は、あなたが、あなたのserviceBeans.xmlに次のように追加し

<bean id="accountDao" class="de.appsiting.fashion.persistence.AccountDao" /> 

AccountServiceのクラスの@Autowired AccountDao注釈を削除し、AccountDaoのセッターを追加すべきだと思います。

関連する問題