1、如上一篇文章所述,有时候bean想发布一些容器事件,就需要先获取spring容器,然后将Event交由spring容器将事件发布出去。

为了让bean获取它所在的spring容器,可以让该bean实现BeanFactoryAware接口,BeanFactoryAware接口只有一个方法。

 setBeanFactory(BeanFactory beanFactory):beanFactory参数指向创建它的BeanFactory。这个setter方法与我们往常的setter方法的使用有些差别,它并不是由我们来使用,而是由spring调用,spring调用该方法时会将spring容器作为参数传入该方法。

与BeanFactoryAware接口类似的有ApplicationContextAware接口,实现该接口的bean需要实现setApplicationContext(ApplicationContext applicationContext)方法,该方法也是由spring调用。spring容器调用该方法时,将会把自身作为参数传入该方法。

2、还是对上一篇文章的例子做一些修改来说明

目录结构

增加一个Person.java业务bean,其实现了ApplicationContextAware接口。

package com.lfy.bean;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import com.lfy.event.EmailEvent; public class Person implements ApplicationContextAware { private ApplicationContext ctx; @Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException { this.ctx=arg0;
} /**
* 模拟人发送邮件业务,发送邮件将触发EmailEvent事件
* @param event
*/
public void sendEmail(EmailEvent event) { System.out.println("\n执行了Person.sendEmail()方法,模拟发出了一封邮件...\n");
this.ctx.publishEvent(event);
} }

修改beans.xml,将Person注册到spring容器中

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 注册监听器 -->
<bean class="com.lfy.listener.EmailNotifier"/>
<bean id="person" class="com.lfy.bean.Person"/>
</beans>

修改SpringListenerTest.java,模拟某人发送邮件,触发了邮件发送监听事件。

package com.lfy.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lfy.bean.Person;
import com.lfy.event.EmailEvent; public class SpringListenerTest { public static void main(String[] args) {
//创建spring容器
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
EmailEvent ele=new EmailEvent("test","spring_test@163.com","this is a test");
//发布容器事件
//ctx.publishEvent(ele);
Person p=ctx.getBean("person", Person.class);
p.sendEmail(ele);
} }

启动运行我们的测试程序:

3、总结

spring容器会检测容器中的所有bean(比如本例中的Person bean),如果发现某个bean实现了ApplicationContextAware接口,spring容器会在创建该bean后,自动调用该bean的setApplicationContext()方法,调用该方法,会将spring容器本身作为参数传递该方法,总而实现让bean获取所在的spring容器。

spring-第四篇之让bean获取所在的spring容器的更多相关文章

  1. Spring第四篇

    在spring第三篇中介绍了bean元素属性 在第四篇中介绍spring注入的方式 1 set方法注入 建立一个User类 创建私有的属性 set  get 方法  重写toString方法 代码如下 ...

  2. SSH框架之Spring第四篇

    1.1 JdbcTemplate概述 : 它是spring框架中提供的一个对象,是对原始JdbcAPI对象的简单封装.spring框架为我们提供了很多的操作模板类. ORM持久化技术 模板类 JDBC ...

  3. Spring Boot 使用Java代码创建Bean并注册到Spring中

    版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/catoop/article/details/50558333 声明同一个类下的多个实例: packa ...

  4. (41)Spring Boot 使用Java代码创建Bean并注册到Spring中【从零开始学Spring Boot】

    已经好久没有讲一些基础的知识了,这一小节来点简单的,这也是为下节的在Spring Boot中使用多数据源做准备. 从Spring 3.0开始,增加了一种新的途径来配置Bean Definition,这 ...

  5. Spring第四篇【Intellij idea环境下、Struts2和Spring整合】

    前言 Spring的第二和第三篇已经讲解了Spring的基本要点了[也就是Core模块]-本博文主要讲解Spring怎么与Struts2框架整合- Struts2和Spring的整合关键点: acti ...

  6. 初学Java ssh之Spring 第四篇

    今天我来学习学习Spring中Bean. 在Spring中原来还有<beans.../>标签啊,它相当于<bean.../>标签的老爸,老爸可以有很多个儿子,但是老爸只有一个哦 ...

  7. 第四课:通过配置文件获取对象(Spring框架中的IOC和DI的底层就是基于这样的机制)

    首先在D盘创建一个文件hero.txt,内容为:com.hero.Hero(此处必须是Hero的完整路径) 接下来是Hero类 package com.hero; public class Hero ...

  8. 【第四篇】- Maven 构建生命周期之Spring Cloud直播商城 b2b2c电子商务技术总结

    ​ ​ Maven 构建生命周期 Maven 构建生命周期定义了一个项目构建跟发布的过程. 一个典型的 Maven 构建(build)生命周期是由以下几个阶段的序列组成的: ​ 阶段 处理 描述 验证 ...

  9. spring使用注解的方式创建bean ,将组件加入容器中

    第一种使用@Bean的方式 1.创建一个bean package com.springbean; public class Person { private String name; private ...

随机推荐

  1. SSM商城系统开发笔记-问题01-通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明。

    配置搭建完后进行Post请求测试时报错: Caused by: org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 29; cvc ...

  2. python数据结构:numpy

    一. numpy概述 numpy(Numerical Python)提供了python对多维数组对象ndarray(应该是N-dimension array)的支持,具有矢量运算能力,快速.节省空间. ...

  3. idea 创建java web项目ssm-gradle

    环境准备:jdk1.8+tomcat8+idea+gradle 1.创建项目SSM 使用gradle创建项目,按照提示如下   image.png 输入项目名称,组名   image.png   im ...

  4. linux清理 clientmqueue 垃圾文件防止 inode 被占满

    #find /var/spool/clientmqueue/ -type -f |xargs rm -f

  5. 基于cdn方式的vue+element-ui的单页面架构

    一.下载vue2.x,下载element-ui.js以及css 二.html文件 <!DOCTYPE html> <html> <head> <meta ch ...

  6. Codeforces Round #425 (Div. 2) - A

    题目链接:http://codeforces.com/contest/832/problem/A 题意:有n个棍子,两个人轮流取这些棍子,每个人每次只能去恰好k个棍子(不足k个则不能取),问先手取的棍 ...

  7. CH5E26 扑克牌 (计数类DP)

    $ CH~5E26~\times ~ $ 扑克牌: (计数类DP) $ solution: $ 唉,计数类DP总是这么有套路,就是想不到. 这道题我们首先可以发现牌的花色没有价值,只需要知道每种牌有 ...

  8. 关于<label>的for属性的简单探索

    在freecodecamp上HTML教程的Create a Set of Radio Buttons这一节中,看到这样一段话, It is considered best practice to se ...

  9. python 按多维列表中的某一个元素位进行排序

    import os,re top = os.popen("tasklist") process_list = [] split_r = r"\s+" memor ...

  10. Anacond的介绍

    Anacond的介绍 Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大( ...