本文链接:https://blog.csdn.net/u010963948/article/details/83507185

1、ApplicationContext
Spring的核心,Context我们通常解释为上下文环境。ApplicationContext则是应用的容器。 Spring把Bean(object)放在容器中,需要用就通过get方法取出来。在ApplicationContext接口的众多实现类中,有3个是我们经常用到的(见表1-1),并且使用这3个实现类也基本能满足我们Java EE应用开发中的绝大部分需求。 表1-1 ApplicationContext接口的常用实现类介绍 类 名 称 功 能 描 述 ClassPathXmlApplicationContext 从类路径ClassPath中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。例如: //装载单个配置文件实例化ApplicationContext容器 ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml"); //装载多个配置文件实例化ApplicationContext容器 String[] configs = {"bean1.xml","bean2.xml","bean3.xml"}; ApplicationContext cxt = new ClassPathXmlApplicationContext(configs); FileSystemXmlApplicationContext 从指定的文件系统路径中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。例如://装载单个配置文件实例化ApplicationContext容器 ApplicationContext cxt = new FileSystemXMLApplicationContext("beans.xml"); //装载多个配置文件实例化ApplicationContext容器 String[] configs = {"c:/beans1.xml","c:/beans2.xml"}; ApplicationContext cxt = new FileSystemXmlApplicationContext(configs); XmlWebApplicationContext 从Web应用中寻找指定的XML配置文件,找到并装载完成ApplicationContext的实例化工作。这是为Web工程量身定制的,使用WebApplicationContextUtils类的getRequiredWebApplicationContext方法可在JSP与Servlet中取得IoC容器的引用 2、ApplicationEvent 是个抽象类,里面只有一个构造函数和一个长整型的timestamp。其源码如下: public abstract class ApplicationEvent extends EventObject { /** use serialVersionUID from Spring 1.2 for interoperability */
private static final long serialVersionUID = 7099057708183571937L; /** System time when the event happened */
private final long timestamp; /**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
} /**
* Return the system time in milliseconds when the event happened.
*/
public final long getTimestamp() {
return this.timestamp;
}
}
3、ApplicationListener 是一个接口,里面只有一个onApplicationEvent方法。如果在上下文中部署一个实现了ApplicationListener接口的bean,那么每当在一个ApplicationEvent发布到 ApplicationContext时,调用ApplicationContext.publishEvent()方法,这个bean得到通知。类似于Oberver设计模式。 其源码如下: public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event); }
好了,这里简单介绍下我的使用方式吧。 首先创建一个自己的回调的类CallBackInfoEvent 去继承ApplicationEvent: public class CallBackInfoEvent extends ApplicationEvent { /**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public CallBackInfoEvent(RiskCallBackInput input) {
super(input);
}
}
然后创建ApplicationListener的一个实现类MyListener : @Component
public class MyListener implements ApplicationListener<CallBackInfoEvent> { private static final Logger logger = LoggerFactory.getLogger(HuluReportListener.class); /**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(CallBackInfoEvent event) {
RiskCallBackInput input = (RiskCallBackInput) event.getSource();
if (StringUtils.equals(input.getType(), “1”)) {
//TODO 处理业务逻辑增删改查
}
}
}
最后在你的业务service中通过调用以下代码是发布通知事件: @Autowired
private ApplicationContext context; //TODO 业务逻辑
context.publishEvent(new CallBackInfoEvent(Object object)); ok了。
springboot以spring的方式初始化servletContext中的值

原文链接:https://blog.csdn.net/m0_37202351/article/details/86180998

需求:springboot 启动后自动执行某些代码,初始化数据,并将数据放到 servletContext 中。

首先,不可使用 ServletContextListener 即不能用 @WebListener ,因为 servlet 容器初始化后,spring 并未初始化完毕,不能使用 @Autowired 注入 spring 的对象。

推荐使用 ApplicationListener:启动项目, spring 加载完毕后,才执行该 ApplicationListener,并且该 Listener 中可以使用 spring 的内容。

@Component
public class SettingDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
// 将 ApplicationContext 转化为 WebApplicationContext
WebApplicationContext webApplicationContext =
(WebApplicationContext)contextRefreshedEvent.getApplicationContext();
// 从 webApplicationContext 中获取 servletContext
ServletContext servletContext = webApplicationContext.getServletContext();
// servletContext设置值
servletContext.setAttribute("key", "value");
}
}
————————————————

Spring中的ApplicationListener的使用详解案例的更多相关文章

  1. Spring中@Component注解,@Controller注解详解

    在使用Spring的过程中,为了避免大量使用Bean注入的Xml配置文件,我们会采用Spring提供的自动扫描注入的方式,只需要添加几行自动注入的的配置,便可以完成 Service层,Controll ...

  2. Spring中的循环依赖解决详解

    前言 说起Spring中循环依赖的解决办法,相信很多园友们都或多或少的知道一些,但当真的要详细说明的时候,可能又没法一下将它讲清楚.本文就试着尽自己所能,对此做出一个较详细的解读.另,需注意一点,下文 ...

  3. Spring中bean的作用域scope详解

    参考文献:http://blog.csdn.net/jacklearntech/article/details/40157861 http://www.cnblogs.com/qq78292959/p ...

  4. spring中Bean的注入参数详解

    字面值    一般指可用字符串表示的值,这些值可以通过<value>元素标签进行注入.在默认情况下,基本数据类型及其封装类.String等类型都可以采取字面值注入的方式,Spring容器在 ...

  5. Spring中@Value标签的使用详解

    1.@Value标签 由于Spring对通过IOC的方式对对象进行统一管理,所以对任何对象而言,其生成方法均由Spring管理.传统的方法是通过XML配置每一个Bean,并对这个Bean的所有Fiel ...

  6. Spring中Controller和RequestMapping的详解

    先看一个简单的实例: @Controller @RequestMapping("/hello") public class anyTypeController{ @RequestM ...

  7. Spring中配置文件applicationContext.xml配置详解

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. Spring源码之九finishRefresh详解

    Spring源码之九finishRefresh详解 公众号搜索[程序员田同学],专职程序员兼业余写手,生活不止于写代码 Spring IoC 的核心内容要收尾了,本文将对最后一个方法 finishRe ...

  9. 【Spring】——声明式事务配置详解

    项目中用到了spring的事务: @Transactional(rollbackFor = Exception.class, transactionManager = "zebraTrans ...

随机推荐

  1. Net core学习系列(九)——Net Core配置

    一.简介 NET Core为我们提供了一套用于配置的API,它为程序提供了运行时从文件.命令行参数.环境变量等读取配置的方法.配置都是键值对的形式,并且支持嵌套,.NET Core还内建了从配置反序列 ...

  2. Manifest中meta-data扩展元素数据的配置与获取

    简介-meta是什么 在AndroidManifest.xml清单文件中 我们有时会看到如下类似的<meta-data ... >元素开始的配置内容: <meta-data andr ...

  3. jeecg数据库切换至mysql8.0方式

    1.修改pom.xml   mysql版本 <mysql.version>8.0.11</mysql.version> 2.修改dbconfig.properties文件 hi ...

  4. iOS逆向必备绝技之ipa重签名

    一.重签名准备工作: 找到开发者证书和配置文件: 列出所有开发者证书文件: security find-identity -p codesigning -v 找一个开发环境配置文件生成entitlem ...

  5. videojs使用技巧

    1 初始化 Video.js初始化有两种方式. 1.1 标签方式 一种是在<video>标签里面加上class="video-js"和data-setup='{}'属性 ...

  6. 查看apache httpd server中加载了哪些模块

    说明: 有的时候,需要查看当前apache中都加载了哪些模块,通过以下命令进行查看 [root@hadoop1 httpd-]# bin/apachectl -t -D DUMP_MODULES Lo ...

  7. Katalon studio登陆并进行用户名和密码参数化

    前面步骤不截图了,简单说一下: 1.创建空的test case :login 2.点击record录制脚本,走登陆 3.生成的脚本后,点击login用例属性,新建2个变量值 4.创建好后,进入用例页面 ...

  8. PMP 第11章错题总结

    1.项目经理考虑每个人的观点并恢复秩序是合作/解决问题的技术2.评价团队有效性的指标包括---个人技能的改进.团队能力的改进.团队成员离职率的降低.团队凝聚力的加强3.管理质量包括所有质量保证活动,还 ...

  9. squid4

    主机上的squid一直是傻瓜型使用,yum安装.默认配置.千年不动.突然漏扫出来3.X版本不能用了,搜了下,得升4.神奇的发现centos7的源(阿里源)里面竟然最高只有3.网上搜使用yum装的也都是 ...

  10. xml 3 字节的 UTF-8 序列的字节 3 无效

    今天在eclipse中编写**.xml文件时,注释中的中文被eclipse识别到错误:3 字节的 UTF-8 序列的字节 3 无效,曾多次遇到该问题,问题的根源是: The cause of this ...