2013-10-04 150 views
0

希望你能幫助我... 我有兩個項目common-lib和common-dao。 每個項目都有自己的彈簧配置文件。彈簧自動裝配不起作用

如果我運行以下測試類,自動裝配工作正常,所有屬性都由spring生成。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"/cmn-dao-spring.xml"}) 
public class ScoreDaoTest extends TestCase { 

@Autowired 
private ScoreDao mScoreDao; 

@Autowired 
private ScoreCreator mScoreCreator; 

@Autowired 
private QuestionCreator mQuestionCreator; 

@Override 
protected void setUp() throws Exception { 
    super.setUp(); 
} 

@Test 
public void testLoadAllScore() throws Exception { 
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 0); 
    Assert.assertTrue(lAllScore.isEmpty()); 
} 

@Test 
public void testSaveScore() throws Exception { 
    Question question = mQuestionCreator.createQuestion(49954854L, new Date(), "Wer bist Du?", "Jörg", "Anja", "Stefan", "Willi", 3, true, false, 1, "DE", "DE_QZ"); 
    Assert.assertNotNull(question); 
    mScoreDao.saveScore(mScoreCreator.createScore(-1L, null, "Stefan", 1033, 27, "Wuhuuuu", question)); 
    List<Score> lAllScore = mScoreDao.loadAllScore(0, 1); 
    Assert.assertFalse(lAllScore.isEmpty()); 
} 

} 

是爲了共同的DAO的項目我的春天文件:

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

<tx:annotation-driven /> 
<context:annotation-config /> 

<import resource="cmn-lib-spring.xml" /> 

<!-- DATABASE CONFIGURATION --> 

<bean 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>database.properties</value> 
    </property> 
</bean> 

<bean id="dataSource" 
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="${jdbc.username}" /> 
    <property name="password" value="${jdbc.password}" /> 
</bean> 

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<!-- BEAN DEFINITIONS --> 

<bean id="scoreDao" class="de.bc.qz.dao.ScoreDao" autowire="byName"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

    </beans> 

從我共同-lib的項目春天文件包含在這一行:

<import resource="cmn-lib-spring.xml" /> 

問題我在我的dao項目的這個班裏面。我的創作者的自動裝配在這裏不起作用:

public class ScoreMapper implements RowMapper<Score> { 

private String suffix = ""; 

@Autowired 
private ScoreCreator mScoreCreator; 

public ScoreMapper(String pSuffix) { 
    suffix = pSuffix; 
} 

public Score mapRow(ResultSet rs, int rowNum) throws SQLException { 
    Score lScore = mScoreCreator.createScore(rs.getLong(suffix+"id"), 
      rs.getDate(suffix+"stempel"), rs.getString(suffix+"username"), 
      rs.getInt(suffix+"points"), rs.getInt(suffix+"level"), 
      rs.getString(suffix+"comment"), 
      new QuestionMapper("q.").mapRow(rs, rowNum)); 
    return lScore; 
} 
    } 

有人能幫我找到問題嗎?

+1

如何創建'ScoreMapper'的實例? –

+0

與「新ScoreMapper(」「)」 –

回答

5

您如何期待Spring自動對任何它不控制的對象進行自動裝配?

如果你想ScoreMapper實例有ScoreCreator scoreCreator使用Spring bean的注入,該ScoreMapper實例本身必須是一個Spring bean,即。由Spring創建和管理。

+0

我認爲這背後有一些魔力:-)你是對的。我將Mapper放入配置中,現在它可以工作。多謝。 –

+0

@StefanBeike不客氣。 –