JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener

上传人:亦明 文档编号:149849655 上传时间:2020-10-31 格式:DOC 页数:13 大小:19.61KB
返回 下载 相关 举报
JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener_第1页
第1页 / 共13页
JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener_第2页
第2页 / 共13页
JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener_第3页
第3页 / 共13页
JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener_第4页
第4页 / 共13页
JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener_第5页
第5页 / 共13页
点击查看更多>>
资源描述

《JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener》由会员分享,可在线阅读,更多相关《JavaforWeb学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener(13页珍藏版)》请在金锄头文库上搜索。

1、Java,for,Web学习笔记(七一) Service和Repository(6)在Spring框架中使用Listener Java for Web 学习笔记(七一):Service 和 和 Repository (6 )在 在 Spring 框架中使用 Listener 目的 Listener 是 Servlet 的,不属于 Spring framework,也就是说我们无法在Listener 中主动注入 Spring bean。本学习将解决这个问题。 进一步了解 Spring 的 的 bean 注入 在解决之前,我们先进一步了解 Spring 的注入机制。在 Spring 中,我们可以使

2、用Inject,Anwired,Resource 等方式实现对自动扫描和自动注入。同一 同一上下文环境中,bean 只实例化一次,在不同类中注入的,都是同一个 bean (同一对象)。我们通常在根上下文中进行扫描,即使我们在不同的类中都进行注入,实际是注入的是同一个对象的。 我们将通过小测试来验证这点。 小测试:设置 Service 设置一个简单的 Service,打印对象地址,同时在构造函数中给出 log,看看在哪个阶段进行实例化。 public interface MyTestService public void whoAmI(String className); Service pub

3、lic class MyTestServiceImpl implements MyTestService private static final Logger log = LogManager.getLogger(); public MyTestServiceImpl() log.info(MyTestServiceImpl instance is created, address is + this); Override public void whoAmI(String className) log.info( : , className,this); 小测试:注入该 Service 在

4、 AuthenticationController 中 Controller public class AuthenticationController Inject private AuthenticationService authenticationService; RequestMapping(value=login,method=RequestMethod.GET) public ModelAndView login(Map model,HttpSession session) myTestService.whoAmI(this.getClass().getName(); . . .

5、 . 在 TicketController 中 Controller RequestMapping(ticket) public class TicketController Inject private MyTestService myTestService; RequestMapping(value = , list, method = RequestMethod.GET) public String list(Map model) this.myTestService.whoAmI(this.getClass().getName(); . . 输出结果: 14:19:19.985 loc

6、alhost-startStop-1 INFO (Spring) ContextLoader - Root WebApplicationContext: initialization started . . 14:19:20.633 localhost-startStop-1 INFO (Spring) AutowiredAnnotationBeanPostProcessor - JSR-330 javax.inject.Inject annotation found and supported for autowiring 14:19:20.934 localhost-startStop-1

7、 INFO MyTestServiceImpl:12 () - MyTestServiceImpl instance is created, address is .wei.flowingflying.customer_support.site.test.MyTestServiceImpl407cec . . 六月 23, xx 2:19:21 下午 org.apache.catalina.core.ApplicationContext log 信息: Initializing Spring FrameworkServlet springDispatcher . . 14:19:23.217

8、-nio-8080-exec-5 INFO MyTestServiceImpl:16 whoAmI() - .wei.flowingflying.customer_support.site.AuthenticationController : .wei.flowingflying.customer_support.site.test.MyTestServiceImpl407cec . . 14:19:36.195 -nio-8080-exec-8 wei INFO MyTestServiceImpl:16 whoAmI() - .wei.flowingflying.customer_suppo

9、rt.site.TicketController : .wei.flowingflying.customer_support.site.test.MyTestServiceImpl407cec 我们看到在 AuthenticationController 和 TicketController 中注入的对象实际地址一样,都是 407cec,即为同一对象,是在 Root Context 中被实例化,且只实例化一次。了解这点非常重要,不同 Controller 对某个注入的 Service 进行操作,是可能相互影响的。 在 在 Listener 中实现注入实例 无法直接在 Listener 中自动注

10、入 Listener 是 Serlvet container 的,不是 Spring framework 的,不是任何的Spring Component,不在自动扫描的范围内,我们在里面标记的任何Inject 不会被注入。 我们创建一个 Session Listener 作测试 WebListener public class WeiTempListener implements HttpSessionListener private static final Logger log = LogManager.getLogger(); Inject private MyTestService m

11、yTestService; public WeiTempListener() public void sessionCreated(HttpSessionEvent se) log.info(-); this.myTestService.whoAmI(this.getClass().getName(); public void sessionDestroyed(HttpSessionEvent se) 14:50:31.164 -nio-8080-exec-4 INFO WeiTempListener:32 sessionCreated() - - 六月 23, xx 2:50:31 下午 o

12、rg.apache.catalina.session.StandardSession _New 严重: Session event listener threw exception java.lang.NullPointerException at .wei.flowingflying.customer_support.site.WeiTempListener.sessionCreated(WeiTempListener.java:33) 实现方式 前面已经看到注入的实例化是在 Root Context 中进行。我们需要在 Listener的初始化过程中,想办法从 Root Context 中

13、获得实例。我们需要: 1. 跟踪发现,Listener 的初始化是 RootContext 的初始化之前,这时是无法获取bean 的。因此 删除 WebListener 的标记,否则无法确保初始化的顺序 在 BootStrap 中,在 Root Context 的初始化后加载 Listener,确保能够获取在 Root Context 中实例化的 bean 2. HttpSessionListener 封装很好,不开放初始化接口,因此需要增加继承ServletContextListener,以便暴露初始化的方法,在初始化中作为 bean。 3. 使用 org.springframework.b

14、eans.factory.annotation.Configurable 标注对于非Spring 管理的 bean。 public class BootStrap implements WebApplicationInitializer Override public void onStartup(ServletContext container) throws ServletException container.getServletRegistration(default).addMapping(/resource/*); AnnotationConfigWebApplicationCon

15、text rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(RootContextConfiguration.class); container.addListener(new ContextLoaderListener(rootContext); /【1】设置 Listener 的加载位置,在完成 Root Context 之后 container.addListener(WeiTempListener.class); . . 我们再看看 WeiTempListener /【1】删除WebListener 标记,采用手动在 BootStrap 中加入 /【2】增加 ServletContextListener 接口,以获得初始化入口 public class WeiTempListener implements HttpSessionListener,ServletContextListener private static

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 其它办公文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号