@Scope("prototype")bean范围未创建新bean [英] @Scope("prototype") bean scope not creating new bean
问题描述
我想在控制器中使用带注释的原型bean.但是春天正在创建一个单例豆.这是该代码:
@Component
@Scope("prototype")
public class LoginAction {
private int counter;
public LoginAction(){
System.out.println(" counter is:" + counter);
}
public String getStr() {
return " counter is:"+(++counter);
}
}
控制器代码:
@Controller
public class HomeController {
@Autowired
private LoginAction loginAction;
@RequestMapping(value="/view", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req){
ModelAndView mav = new ModelAndView("home");
mav.addObject("loginAction", loginAction);
return mav;
}
public void setLoginAction(LoginAction loginAction) {
this.loginAction = loginAction;
}
public LoginAction getLoginAction() {
return loginAction;
}
}
速度模板:
LoginAction counter: ${loginAction.str}
Spring config.xml
启用了组件扫描:
<context:annotation-config />
<context:component-scan base-package="com.springheat" />
<mvc:annotation-driven />
我每次都获得递增计数.无法弄清楚我要去哪里错了!
更新
以 @gkamal 的建议为例,我意识到HomeController
webApplicationContext
并解决了问题./p>
更新的代码:
@Controller
public class HomeController {
@Autowired
private WebApplicationContext context;
@RequestMapping(value="/view", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req){
ModelAndView mav = new ModelAndView("home");
mav.addObject("loginAction", getLoginAction());
return mav;
}
public LoginAction getLoginAction() {
return (LoginAction) context.getBean("loginAction");
}
}
作用域原型意味着每次您向spring(getBean或依赖项注入)请求实例时,它都会创建一个新实例并提供对该实例的引用.
在您的示例中,将创建一个LoginAction的新实例,并将其注入到HomeController中.如果您要向其中注入LoginAction的另一个控制器,您将获得一个不同的实例.
如果您希望每次调用都使用不同的实例-那么您每次都需要调用getBean-向单例bean中注入将无法实现这一目标.
I want to use a annotated prototype bean in my controller. But spring is creating a singleton bean instead. Here is the code for that:
@Component
@Scope("prototype")
public class LoginAction {
private int counter;
public LoginAction(){
System.out.println(" counter is:" + counter);
}
public String getStr() {
return " counter is:"+(++counter);
}
}
Controller code:
@Controller
public class HomeController {
@Autowired
private LoginAction loginAction;
@RequestMapping(value="/view", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req){
ModelAndView mav = new ModelAndView("home");
mav.addObject("loginAction", loginAction);
return mav;
}
public void setLoginAction(LoginAction loginAction) {
this.loginAction = loginAction;
}
public LoginAction getLoginAction() {
return loginAction;
}
}
Velocity template:
LoginAction counter: ${loginAction.str}
Spring config.xml
has component scanning enabled:
<context:annotation-config />
<context:component-scan base-package="com.springheat" />
<mvc:annotation-driven />
I'm getting an incremented count each time. Can't figure out where am I going wrong!
Update
As suggested by @gkamal, I made HomeController
webApplicationContext
-aware and it solved the problem.
updated code:
@Controller
public class HomeController {
@Autowired
private WebApplicationContext context;
@RequestMapping(value="/view", method=RequestMethod.GET)
public ModelAndView display(HttpServletRequest req){
ModelAndView mav = new ModelAndView("home");
mav.addObject("loginAction", getLoginAction());
return mav;
}
public LoginAction getLoginAction() {
return (LoginAction) context.getBean("loginAction");
}
}
Scope prototype means that every time you ask spring (getBean or dependency injection) for an instance it will create a new instance and give a reference to that.
In your example a new instance of LoginAction is created and injected into your HomeController . If you have another controller into which you inject LoginAction you will get a different instance.
If you want a different instance for each call - then you need to call getBean each time - injecting into a singleton bean will not achieve that.
这篇关于@Scope("prototype")bean范围未创建新bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!