javaEE Controller 的方法能不能设置成 static ,这样是不是更好
java@Controller public class IndexController { private static ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); private static UserService userService = (UserService) ctx.getBean("userService"); @RequestMapping(value = "/", method = RequestMethod.GET) public static String index(Mapmap, HttpServletRequest request, HttpSession session) { Integer uid = (Integer) session.getAttribute("uid"); User user = userService.get(uid); map.put("user", user); return "index"; } }
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
1.楼主表达的不是很清楚,以至于我也不太明白你所谓的好是什么意思。
2.先说楼主提到问题,对controller加上static,这个controller并没有指定是单例,默认情况下就是多列,那么加上static,那么它是否是非线程安全的呢。
3.加上static用现在的话说,然并卵,并没有起到什么优化系统,提高效率等等作用。
为什么要设成static的呢?