web启动@Autowired不能自动注入
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;
- }
- }
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);
- }
- }
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;
@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不能自动注入的更多相关文章
- Spring Boot @Autowired 没法自动注入的问题
Application 启动类: @SpringBootApplication @EnableConfigurationProperties @ComponentScan(basePackages = ...
- Spring @Autowired 注解自动注入流程是怎么样?
面试中碰到面试官问:"Spring 注解是如果工作的?",当前我一惊,完了这不触及到我的知识误区了吗?,还好我机智,灵机一动回了句:Spring 注解的工作流程倒还没有看到,但是我 ...
- Spring框架使用ByName自动注入同名问题剖析
问题描述 我们在使用spring框架进行项目开发的时候,为了配置Bean的方便经常会使用到Spring当中的Autosire机制,Autowire根据注入规则的不同又可以分为==ByName==和 ...
- Spring(二十三):Spring自动注入的实现方式
注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解包含:Autowrired/Resource/Qualifier/Service/Controller/Repository/C ...
- Injection of autowired dependencies failed; autowire 自动注入失败,测试类已初始化过了Spring容器。
1 严重: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creat ...
- @Autowired自动注入失败
新手注意的问题 package cn.ryq.web.controller; import cn.ryq.domain.company.Company;import cn.ryq.service.co ...
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- 小白日记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 [ ...
- 小白日记45:kali渗透测试之Web渗透-SqlMap自动注入(三)-sqlmap参数详解-Optimization,Injection,Detection,Techniques,Fingerprint
sqlmap自动注入 Optimization [优化性能参数,可提高效率] -o:指定前三个参数(--predict-output.--keep-alive.--null-connection) - ...
随机推荐
- Karen and Coffee CF 816B(前缀和)
Description To stay woke and attentive(专注的) during classes, Karen needs some coffee! Karen, a coffee ...
- 王者荣耀交流协会final发布版本控制报告
二次开发软件说明文档 Dec 6 纠正饼状图点击选择PSP文件无效. 添加饼状图丢失的代码. submit the files that last night I forgot. add shurum ...
- 关于“问吧APP”问卷调查报告分析与体会
上周根据我们走廊奔跑队的“问吧APP”项目对本校范围内的学生发放了上百份调查问卷,并对此作出了统计和整理.针对我们项目所提出的问题涉及到的用户信息有性别.年龄.学历.职业.平时上网途径以及对 ...
- python接口自动化测试框架实现之操作mysq数据库
python操作mysql数据库需要使用到mysqlclient库. 安装:pip install mysqlclient python连接mysql数据库分以下步骤: 1.与mysql建立连接: 2 ...
- css3 flex属性flex-grow、flex-shrink、flex-basis学习笔记
最近在研究css3的flex.遇到的flex:1;这一块,很是很纠结,flex-grow.flex-shrink.flex-basis始终搞不清,最经搜集了大量的介绍,应该能算是明白了.网上大部分解释 ...
- java利用poi读取excel异常问题
最近一个web工程需要完成一个小功能,利用文件上传然后读取文件内容写入到数据库,这里是操作的excel文件,excel文件分两种后缀,03版本的xls和之后的xlsx,现在大家一般都拿非常好用的插件直 ...
- BZOJ4892 Tjoi2017dna(后缀数组)
对每个子串暴力匹配至失配三次即可.可以用SA查lcp.然而在bzoj上被卡常了.当然也可以二分+哈希或者SAM甚至FFT. #include<iostream> #include<c ...
- 洛谷 P3258 [JLOI2014]松鼠的新家
树剖,裸题,鉴定完毕. 我是题面 读完题,恩,树剖,裸题,没劲. 处理很简单,既然每到一个房间吃一块糖,那么就在每条路径上的每个房间放一颗糖,但是每条路径的终点也就是下一条路径的起点,在这里只能加一次 ...
- TCP(Transmission Control Protocol)学习笔记
一.TCP(Transmission Control Protocol)原理介绍(参考维基百科) TCP连接包括三种状态:连接建立.数据传送和连接终止. TCP用三路握手(three-way hand ...
- Spring Boot系列教程七:Spring boot集成MyBatis
一.创建项目 项目名称为 “springboot_mybatis_demo”,创建过程中勾选 “Web”,“MyBatis”,“MySQL”,第一次创建Maven需要下载依赖包(耐心等 ...