自己写监听器初始化数据
package com.ttc.listener;
import com.ttc.entity.User;
import com.ttc.service.IUserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @author by benny on 2016/4/23.
* @version 1.0
* @description 首次启动初始化数据的监听器 放到application作用域中 切记 要@Component
* 因为他的作用是找到任何一个spring初始化的bean 所以他自己也是一个bean
*/
@Component
public class InitDataListener implements ServletContextListener, ApplicationContextAware {
public ApplicationContext applicationContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext application = sce.getServletContext();
IUserService userServiceImpl = (IUserService) applicationContext.getBean("userServiceImpl");
User user = userServiceImpl.selectByPrimaryKey(4);
application.setAttribute("initdata", user);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
+1、定义一个监听器并加上注解@Component
+2、实现类ServletContextListener, ApplicationContextAware
结果一直报错:而且断电调试一直无法注入applicationContext。
org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc The web application [ROOT] registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped.
package com.ttc.listener;
import com.ttc.entity.User;
import com.ttc.service.IUserService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* @author by benny on 2016/4/23.
* @version 1.0
* @description 首次启动初始化数据的监听器 放到application作用域中 切记 要@Component
* 因为他的作用是找到任何一个spring初始化的bean 所以他自己也是一个bean
*/
@Component
public class InitDataListener implements ServletContextListener, ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void contextInitialized(ServletContextEvent sce) {
applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
//获取bean
IUserService userServiceImpl = (IUserService) applicationContext.getBean("userServiceImpl");
User user = userServiceImpl.selectByPrimaryKey(4);
ServletContext application = sce.getServletContext();
application.setAttribute("initdata", user);
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
重点:ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());通过WebApplicationContextUtils取得applicationContext
就在刚刚的一刹那,我发现了第一种方式注入为什么不行了,原因是因为:我写的是public ApplicationContext applicationContext;
必须写成public static ApplicationContext applicationContext;,加上static修饰就可以了,具体什么原因还没有去看。调试了那么久原来是因为一个static的问题。