使用struts2,下面为action代码
Java代码
package com.edar.web.platform;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import com.edar.components.AccountBean;
import com.edar.dao.util.Page;
import com.edar.model.Account;
import com.edar.service.platform.AccountService;
import com.edar.web.struts2.GenericAction;
@Component
@Namespace("/platform")
@InterceptorRefs( { @InterceptorRef("paramsPrepareParamsStack") })
@Results( { @Result(name = GenericAction.RELOAD, location = "account.action", type = "redirect") })
public class AccountAction extends GenericAction<AccountBean> {
private static final long serialVersionUID = 1900042912756344244L;
@Autowired
@Qualifier("accountServiceImpl")
private AccountService accountService;
private AccountBean accountBean;
private Page<AccountBean> page = new Page<AccountBean>(16,true);
public AccountBean getAccountBean() {
return accountBean;
}
public void setAccountBean(final AccountBean accountBean) {
this.accountBean = accountBean;
}
public Page<AccountBean> getPage() {
return page;
}
public void setPage(final Page<AccountBean> page) {
this.page = page;
}
@Override
public String delete(){
accountService.delete((Account) dozer.map(accountBean, Account.class));
return SUCCESS;
}
@Override
public String list(){
page = accountService.getPage(accountBean);
return SUCCESS;
}
@Override
protected void prepareModel(){
if(accountBean==null){
accountBean = new AccountBean();
}else{
if(accountBean.getAccountId()!=null){
accountBean = (AccountBean)dozer.map(accountService.getAccount(accountBean.getAccountId()),
AccountBean.class);
}
}
}
@Override
public String save(){
Account account = (Account) dozer.map(accountBean, Account.class);
accountService.save(account);
accountBean.setAccountId(account.getAccountId());
return SUCCESS;
}
public AccountBean getModel() {
return accountBean;
}
}
此action的junit测试代码
package com.edar.web.platform;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.edar.components.AccountBean;
import com.edar.test.SpringContextTestCase;
public class AccountActionTest extends SpringContextTestCase{
private static Long accountId;
@Autowired
private AccountAction accountAction;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
AccountBean bean = new AccountBean();
bean.setName("ysheng53");
bean.setMobile("13819181747");
bean.setEmail("ysheng53@gmail.com");
bean.setPasswd("321");
accountAction.setAccountBean(bean);
}
@After
public void tearDown() throws Exception {
}
@Test
public void testSave() {
String result = accountAction.save();
accountId = accountAction.getAccountBean().getAccountId();
Assert.assertEquals(AccountAction.SUCCESS, result);
}
@Test
public void testList() {
String result = accountAction.list();
Assert.assertEquals(AccountAction.SUCCESS, result);
Assert.assertTrue(" 结果数小于0了 ",accountAction.getPage().getTotal()>0);
}
@Test(timeout=5000)
public void testDelete() {
accountAction.getAccountBean().setAccountId(accountId);
String result = accountAction.delete();
Assert.assertEquals(AccountAction.SUCCESS, result);
Assert.assertTrue("<=0",accountAction.getPage().getTotal()<=0);
}
}
注意到action中的代码:
Java代码
@Autowired
@Qualifier("accountServiceImpl")
private AccountService accountService;
现象时,在junit测试代码执行时,accountService能够被注入,但是用tomcat6,在eclipse,wtp中启动时,accountService没有被注入,为null!
问题在查,谁遇到过类似问题;
问题补充
经过测试分析发现:
AccountAction在junit测试时用spring注入进去,而且只有唯一一个;
而在struts2中,每次请求都会有一个AccountAction的实例;
现在的问题是,struts2中新建实例时,那个private AccountService accountService;自动注入无效;
注:使用了Conventian Plugin
分享到:
相关推荐
通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合
主要介绍了Spring使用@Autowired为抽象父类注入依赖代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
SSH笔记-通过@Autowired配置注入关系的demo,续上一篇文章中的annotation笔记
Spring@Autowired注解与自动装配.rar
主要介绍了详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
1.@Resource是按名称进行注入的,属于java自带的。@Autowired是按类型进行注入的,属于Spring。
自定义标签中@Autowired的属性为null 解决办法:两步 1.新建一个类SpringContext,实现接口ApplicationContextAware; 2.spring.xml中添加 3.使用SpingContext.getBean("bean名");获取
主要介绍了Intellij IDEA如何去掉@Autowired 注入警告的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
NULL 博文链接:https://forestqqqq.iteye.com/blog/2235292
通过spring 的BeanPostProcessor 将对象注入到spring之前创建一个代理对象注入到spring容器中,通过@autowired 注入的对象就是指定的代理对象内容
主要介绍了详解Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
主要介绍了@Autowired注入为null问题原因分析吗,小编觉得挺不错的,对日后比较有帮助,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Spring注解@Resource和@Autowired.doc
主要介绍了SpringBoot项目使用多线程处理任务时无法通过@Autowired注入bean问题的解决方法,需要的朋友可以参考下
WebappApplication 一定要在包的最外层,否则Spring无法对所有的类进行托管,会造成@Autowired 无法注入。接下来给大家介绍解决Springboot @Autowired 无法注入问题,感兴趣的朋友一起看看吧
1.本文代码实现的功能是配置类,指定自定义配置文件的位置和名称...4.这个博文讲的,我已将代码包发布到了我的资源里,有需要的可以直接下载并导入到自己的id里,看看能不能运行; 原文链接:https://blog.csdn.net/we
主要介绍了详解Spring @Autowired 注入小技巧,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
【Java面试】@Resource 和 @Autowired 的区别.doc
@Autowired使用 构造函数注入 public Class Outer { private Inner inner; @Autowired public Outer(Inner inner) { this.inner = inner; } } 属性注入 public Class Outer { @Autowired private Inner ...
主要给大家介绍了因Spring AOP导致@Autowired依赖注入失败的解决方法,文中通过示例代码给大家介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编来一起学习学习吧。