关于spring的那点事

自己写监听器初始化数据

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的问题。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值