sunships
2013-01-22 16:28
浏览 425
已采纳

在action中对属性使用@Autowired注解自动注入失败

在下面的单元测试DataAccessTest类中articleManager输出不为null
[code="java"]
package sunships.dhcc.xtest;
...
public class DataAccessTest extends SpringTxTestCase {

@Autowired
private ArticleManager articleManager;

@Test
@Rollback(false)
public void addArticle() {
    System.out.println(articleManager);//非null值
    ......
}

}
[/code]
但同样如下面的代码,我在action类中的添加article方法中,获取的articleManager却为null
[code="java"]
package sunships.dhcc.web;
...
public class ArticleAction extends CrudActionSupport {

@Autowired
private ArticleManager articleManager;

...

@Override
public String save() throws Exception {
    System.out.println("aritcleAction:article->" + article.getSubject() + "  " + article.getContent());
    System.out.println("  " + articleManager);//输出为null
    articleManager.save(article);
    System.out.println("articleAction-save-divide--------------------");
    return RELOAD;
}

...
}[/code]

7条回答 默认 最新

相关推荐 更多相似问题