使用struts2,下面为action代码

Java代码

  1. package com.edar.web.platform;
  2. import org.apache.struts2.convention.annotation.InterceptorRef;
  3. import org.apache.struts2.convention.annotation.InterceptorRefs;
  4. import org.apache.struts2.convention.annotation.Namespace;
  5. import org.apache.struts2.convention.annotation.Result;
  6. import org.apache.struts2.convention.annotation.Results;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. import org.springframework.stereotype.Component;
  10. import com.edar.components.AccountBean;
  11. import com.edar.dao.util.Page;
  12. import com.edar.model.Account;
  13. import com.edar.service.platform.AccountService;
  14. import com.edar.web.struts2.GenericAction;
  15. @Component
  16. @Namespace("/platform")
  17. @InterceptorRefs( { @InterceptorRef("paramsPrepareParamsStack") })
  18. @Results( { @Result(name = GenericAction.RELOAD, location = "account.action", type = "redirect") })
  19. public class AccountAction extends GenericAction<AccountBean> {
  20. private static final long serialVersionUID = 1900042912756344244L;
  21. @Autowired
  22. @Qualifier("accountServiceImpl")
  23. private AccountService accountService;
  24. private AccountBean accountBean;
  25. private Page<AccountBean> page = new Page<AccountBean>(16,true);
  26. public AccountBean getAccountBean() {
  27. return accountBean;
  28. }
  29. public void setAccountBean(final AccountBean accountBean) {
  30. this.accountBean = accountBean;
  31. }
  32. public Page<AccountBean> getPage() {
  33. return page;
  34. }
  35. public void setPage(final Page<AccountBean> page) {
  36. this.page = page;
  37. }
  38. @Override
  39. public String delete(){
  40. accountService.delete((Account) dozer.map(accountBean, Account.class));
  41. return SUCCESS;
  42. }
  43. @Override
  44. public String list(){
  45. page = accountService.getPage(accountBean);
  46. return SUCCESS;
  47. }
  48. @Override
  49. protected void prepareModel(){
  50. if(accountBean==null){
  51. accountBean = new AccountBean();
  52. }else{
  53. if(accountBean.getAccountId()!=null){
  54. accountBean = (AccountBean)dozer.map(accountService.getAccount(accountBean.getAccountId()),
  55. AccountBean.class);
  56. }
  57. }
  58. }
  59. @Override
  60. public String save(){
  61. Account account = (Account) dozer.map(accountBean, Account.class);
  62. accountService.save(account);
  63. accountBean.setAccountId(account.getAccountId());
  64. return SUCCESS;
  65. }
  66. public AccountBean getModel() {
  67. return accountBean;
  68. }
  69. }
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&lt;AccountBean&gt; getPage() {
return page;
} public void setPage(final Page&lt;AccountBean&gt; 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测试代码


  1. package com.edar.web.platform;     
  2.     
  3. import org.junit.After;     
  4. import org.junit.AfterClass;     
  5. import org.junit.Assert;     
  6. import org.junit.Before;     
  7. import org.junit.BeforeClass;     
  8. import org.junit.Test;     
  9. import org.springframework.beans.factory.annotation.Autowired;     
  10.     
  11. import com.edar.components.AccountBean;     
  12. import com.edar.test.SpringContextTestCase;     
  13.     
  14. public class AccountActionTest extends SpringContextTestCase{     
  15.          
  16.     
  17.     private static Long accountId;     
  18.     @Autowired    
  19.     private AccountAction accountAction;     
  20.     @BeforeClass    
  21.     public static void setUpBeforeClass() throws Exception {     
  22.     }     
  23.     
  24.     @AfterClass    
  25.     public static void tearDownAfterClass() throws Exception {     
  26.     }     
  27.     
  28.     @Before    
  29.     public void setUp() throws Exception {     
  30.         AccountBean bean = new AccountBean();     
  31.         bean.setName("ysheng53");     
  32.         bean.setMobile("13819181747");     
  33.         bean.setEmail("ysheng53@gmail.com");     
  34.         bean.setPasswd("321");     
  35.         accountAction.setAccountBean(bean);     
  36.     }     
  37.     
  38.     @After    
  39.     public void tearDown() throws Exception {     
  40.     }     
  41.     @Test    
  42.     public void testSave() {     
  43.         String result = accountAction.save();     
  44.         accountId = accountAction.getAccountBean().getAccountId();     
  45.         Assert.assertEquals(AccountAction.SUCCESS, result);     
  46.     }     
  47.     @Test    
  48.     public void testList() {     
  49.         String result = accountAction.list();     
  50.         Assert.assertEquals(AccountAction.SUCCESS, result);     
  51.         Assert.assertTrue(" 结果数小于0了 ",accountAction.getPage().getTotal()>0);     
  52.     }     
  53.     
  54.     @Test(timeout=5000)     
  55.     public void testDelete() {     
  56.         accountAction.getAccountBean().setAccountId(accountId);     
  57.         String result = accountAction.delete();     
  58.         Assert.assertEquals(AccountAction.SUCCESS, result);     
  59.         Assert.assertTrue("<=0",accountAction.getPage().getTotal()<=0);     
  60.     }     
  61.     
  62. }    
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()&gt;0);
} @Test(timeout=5000)
public void testDelete() {
accountAction.getAccountBean().setAccountId(accountId);
String result = accountAction.delete();
Assert.assertEquals(AccountAction.SUCCESS, result);
Assert.assertTrue("&lt;=0",accountAction.getPage().getTotal()&lt;=0);
}

}

注意到action中的代码:


Java代码


  1. @Autowired  
  2. @Qualifier("accountServiceImpl")  
  3. private AccountService accountService;  


@Autowired

@Qualifier("accountServiceImpl")

private AccountService accountService;

现象时,在junit测试代码执行时,accountService能够被注入,但是用tomcat6,在eclipse,wtp中启动时,accountService没有被注入,为null!




问题在查,谁遇到过类似问题;


问题补充


经过测试分析发现:


AccountAction在junit测试时用spring注入进去,而且只有唯一一个;


而在struts2中,每次请求都会有一个AccountAction的实例;




现在的问题是,struts2中新建实例时,那个private AccountService accountService;自动注入无效;






注:使用了Conventian Plugin

web启动@Autowired不能自动注入的更多相关文章

  1. Spring Boot @Autowired 没法自动注入的问题

    Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...

  2. Spring @Autowired 注解自动注入流程是怎么样?

    面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...

  3. Spring框架使用ByName自动注入同名问题剖析

    问题描述   我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...

  4. Spring(二十三):Spring自动注入的实现方式

    注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解包含:Autowrired/Resource/Qualifier/Service/Controller/Repository/C ...

  5. Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。

    1 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...

  6. @Autowired自动注入失败

    新手注意的问题 package cn.ryq.web.controller; import cn.ryq.domain.company.Company;import cn.ryq.service.co ...

  7. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  8. 小白日记46:kali渗透测试之Web渗透-SqlMap自动注入(四)-sqlmap参数详解- Enumeration,Brute force,UDF injection,File system,OS,Windows Registry,General,Miscellaneous

    sqlmap自动注入 Enumeration[数据枚举] --privileges -U username[CU 当前账号] -D dvwa -T users -C user --columns  [ ...

  9. 小白日记45:kali渗透测试之Web渗透-SqlMap自动注入(三)-sqlmap参数详解-Optimization,Injection,Detection,Techniques,Fingerprint

    sqlmap自动注入 Optimization [优化性能参数,可提高效率] -o:指定前三个参数(--predict-output.--keep-alive.--null-connection) - ...

随机推荐

  1. 卡特兰数(Catalan)及其应用

    卡特兰数 大佬博客https://blog.csdn.net/doc_sgl/article/details/8880468 卡特兰数是组合数学中一个常出现在各种计数问题中出现的数列. 卡特兰数前几项 ...

  2. P4 Runtime和p4 info

    p4runtime P4 Runtime是一套基于Protobuf以及gRPC框架上的协议,通过P4runtime,SDN控制器可以控制能够支援p4的设备. p4runtime当前由p4 API wo ...

  3. RAR和ZIP:压缩大战真相 (挺赞值得了解)

    前言--王者归来? 等待足足两年之久,压缩霸主WinZip终于在万众期待下发布了9.0正式版.全世界自然一片沸腾,在世界各大知名下载网站中,WinZip9.0再次带起下载狂潮.然而此时国内并没有王者回 ...

  4. Jquery mobile div常用属性

    组件 页面 jQuery Mobile 应用了 HTML5 标准的特性,在结构化的页面中完整的页面结构分为 header. content.footer 这三个主要区域. 在 body 中插入内容块: ...

  5. CKeditor、CKFinder的安装配置

    CKEditor是不集成文件上传与管理功能的,文件上传管理功能被集成在CKFinder中,这是一个收费的商业软件. 如需要文件上传与管理功能建议使用FCKeditor或者手动破解CKFinder. 下 ...

  6. 【php】提交的特殊字符会被自动转义

    在处理mysql和GET.POST的数据时,常常要对数据的引号进行转义操作. PHP中有三个设置可以实现自动对’(单引号),”(双引号),\\(反斜线)和 NULL 字符转移. PHP称之为魔术引号, ...

  7. Hibernate 中 load() 方法导致的 noSession 异常

    之所以要写这个,是因为最近碰到了一个延迟加载的 load() 导致出现 noSession 的异常. 下面第三种方式解决这个问题需要用到一个本地线程的对象,也就是 ThreadLocal 类,之前写过 ...

  8. mysql 、慢查询、到底如何玩

    在项目开发中,那些开发大佬经常会写出一些SQL语句,一条糟糕的SQL语句可能让你测试的整个程序都非常慢,超过10秒的话,我觉得一般用户就会选择关闭网页,如何优化SQL语句将那些运行时间 比较长的SQL ...

  9. BZOJ 2424 订货(贪心+单调队列)

    怎么题解都是用费用流做的啊...用单调队列多优美啊. 题意:某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初 ...

  10. 【uoj#21】[UR #1]缩进优化 数学

    题目描述 给出 $n$ 个数 ,求 $\text{Min}_{x=1}^{\infty}\sum\limits_{i=1}^n(\lfloor\frac {a_i}x\rfloor+a_i\ \tex ...