Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 弹簧自动接线不工作_Java_Spring_Autowired - Fatal编程技术网

Java 弹簧自动接线不工作

Java 弹簧自动接线不工作,java,spring,autowired,Java,Spring,Autowired,希望你能帮助我。。。 我有两个项目:公共lib和公共dao。 每个项目都有自己的spring配置文件 如果我运行下面的testclass,自动连接工作正常,所有属性都由spring生成 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"/cmn-dao-spring.xml"}) public class ScoreDaoTest extends TestCase { @Autowired pri

希望你能帮助我。。。 我有两个项目:公共lib和公共dao。 每个项目都有自己的spring配置文件

如果我运行下面的testclass,自动连接工作正常,所有属性都由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());
}

 }
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“/cmn dao spring.xml”})
公共类ScoreDaoTest扩展了TestCase{
@自动连线
私有ScoreDao mScoreDao;
@自动连线
私人计分制单人;
@自动连线
私人问题创建者mQuestionCreator;
@凌驾
受保护的void setUp()引发异常{
super.setUp();
}
@试验
public void testLoadAllScore()引发异常{
List lAllScore=mScoreDao.loadAllScore(0,0);
Assert.assertTrue(lAllScore.isEmpty());
}
@试验
public void testSaveScore()引发异常{
Question Question=mQuestionCreator.createQuestion(49954854L,new Date(),“Wer bist Du?”,“Jörg”,“Anja”,“Stefan”,“Willi”,3,true,false,1,“DE”,“DE_QZ”);
Assert.assertNotNull(问题);
mScoreDao.saveScore(mScoreCreator.createScore(-1L,null,“Stefan”,1033,27,“芜湖”,问题));
List lAllScore=mScoreDao.loadAllScore(0,1);
Assert.assertFalse(lAllScore.isEmpty());
}
}
这是我针对公共dao项目的spring文件:

<?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>

数据库.properties
我的公共库项目中的spring文件包含在此行中:

<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;
}
   }
公共类ScoreMapper实现行映射器{
私有字符串后缀=”;
@自动连线
私人计分制单人;
公共记分器(字符串pSuffix){
后缀=pSuffix;
}
公共分数映射行(结果集rs,int rowNum)抛出SQLException{
Score lScore=mScoreCreator.createScore(rs.getLong(后缀+“id”),
rs.getDate(后缀+“stempel”)、rs.getString(后缀+“用户名”),
rs.getInt(后缀+“点数”)、rs.getInt(后缀+“级别”),
rs.getString(后缀+注释),
新的问题映射器(“q”).mapRow(rs,rowNum));
返回lScore;
}
}

有人能帮我找到问题吗?

您认为Spring如何自动关联它无法控制的对象中的任何内容


如果希望
ScoreMapper
实例将
ScoreCreator
注入Springbean,则
ScoreMapper
实例本身必须是Springbean,即由Spring创建和管理。

您希望Spring如何自动关联它无法控制的对象中的任何内容


如果希望
ScoreMapper
实例将
ScoreCreator
注入Springbean,则
ScoreMapper
实例本身必须是Springbean,即由Spring创建和管理。

如何使用“new ScoreMapper”(“”)创建
ScoreMapper
的实例如何创建
记分器的实例?
?使用“new ScoreMapper(“”)”我认为这背后有一些魔力:-)你是对的。我将映射器放入配置中,现在它可以工作了。Thx很多。但是映射器通常是用
new
创建的,它在jdbcTemplate中如何工作?@user729您是说在
@Bean
注释的方法中吗?如果是这样,那么它将注册为bean并由Spring管理(即使您是实例化它的人)。如果没有,请澄清。当用作
jdbcTemplate.query(SQL,新对象[]{},新MyRowMapper())
@user噢,在这种情况下,是的。该对象将不是Spring管理的bean。但如果你不需要的话,这不是问题。我认为这背后有一些魔力:-)你是对的。我将映射器放入配置中,现在它可以工作了。Thx很多。但是映射器通常是用
new
创建的,它在jdbcTemplate中如何工作?@user729您是说在
@Bean
注释的方法中吗?如果是这样,那么它将注册为bean并由Spring管理(即使您是实例化它的人)。如果没有,请澄清。当用作
jdbcTemplate.query(SQL,新对象[]{},新MyRowMapper())
@user噢,在这种情况下,是的。该对象将不是Spring管理的bean。但如果你不需要的话,这不是问题。