关于struts2结合spring 创建action的思考

最近突然想到一个问题

以前在用struts2(注解)+spring

struts的action拖给spring管了(spring的bean在Ioc容器范围内默认都是singlen的),但是没有加@scope("prototype")却从来没有出现过线程安全问题。

而一年前还在学校时做ssh2(没用注解)练习时,不在bean后加prototype都会出现线程安全问题,当时所有的action后都加了scope=prototype。

难道注解和不用注解,struts2创建action的方式不一样?

于是做了个测试

public class TestAction extends BaseAction {
	@Autowired
	private UserService userService;
	@Action("test")
	public String test() throws Exception {
		System.out.println("action HashCode:"+this.hashCode());
		userService.getUserName();		
		return "success";
	}
}
@Service
public class UserService {
 public void getUserName(){
  System.out.println("service HashCode:"+hashCode()); }
}

 连续运行3次发现输出:

action HashCode:9928297
service HashCode:32262619
action HashCode:13620718
service HashCode:32262619
action HashCode:19792917
service HashCode:32262619

 输出结果说明 每次的action是不一样的

而每次的service是同一个也就是单例的

(难道action并没有交个spring托管)

又把以前在学校做的老项目(纯xml)拿来 把以前的scope="prototype"去掉

发现action确实是单例的   再加上scope="prototype"后action不是单例了

 

所以struts2(注解) 在和spring集成时action默认是new的,不用注解spring扫描action的话也是用的new。

而非注解时如果把action加入bean的话默认是单例的。

所以如果大家struts2是用注解的话就不需要在action上加@scope("prototype")了;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值