在多用户管理中想把每个用户的信息都保存在session中,其中保存用户信息的bean想用spring管理,则必须不是单例
@Controller
@Scope("prototype")
public class LoginController {
@Autowired
private LoginService loginService;
@Resource
private UserContext userContext;
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
return "login";
}
@RequestMapping(value = "/login", method = RequestMethod.POST)
public ModelAndView loginCheck(User user,HttpServletRequest request) {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("login");
User userInfo = loginService.getUser(user);
if (null != userInfo) {
// UserContext userContext = new UserContext();
userContext.init(userInfo);
request.getSession().setAttribute("user", userContext);
if(userInfo.getId() !=1) {
modelAndView.setViewName("redirect:/matches");
}else {
modelAndView.setViewName("redirect:/home");
}
} else {
modelAndView.addObject("user",user);
modelAndView.addObject("msg", "Invalid account or password.");
}
return modelAndView;
}
-------------
@Service
@Scope("prototype")
public class UserContext {
private List<Menu> menuList = null;
private User user = null;
private ContentOwnerContext currentContentOwner;
------------、
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.
注意想得到prototype的UserContextze则必须在controller也加上@Scope,否则还是单例,因为Controller还是单例。
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
spring的Annotation注解.
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
Spring的Annotation配置相关讲义
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
Spring_Annotation_AOP
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
Spring IOC Annotation 注入 学习实例
spring annotation 从入门到精通
spring annotation 注解 maven 配套的源码
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记