zoukankan      html  css  js  c++  java
  • web启动@Autowired不能自动注入

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


    此action的junit测试代码

    Java代码  收藏代码
    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. }    






    注意到action中的代码:

    Java代码

    Java代码  收藏代码
    1. @Autowired  
    2. @Qualifier("accountServiceImpl")  
    3. private AccountService accountService;  




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



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

    问题补充

    经过测试分析发现:

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

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



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





    注:使用了Conventian Plugin

  • 相关阅读:
    JUC回顾之-可重入的互斥锁ReentrantLock
    java基础知识回顾之java Thread类学习(十二)-- 线程中断
    mknod命令
    一个公益水塘引发的纠纷
    对cgic的理解——name选项
    linux的fwrite()使用方法,当前时间写入文本的程序
    /etc/resolv.conf文件详解
    关于函数strtok和strtok_r的使用要点和实现原理
    shell视频
    进程一些命令pstree,ps,pstack,top
  • 原文地址:https://www.cnblogs.com/jpfss/p/7445052.html
Copyright © 2011-2022 走看看