ApplicationContext在启动的时候就把所有的Bean全部实例化了。还可以为Bean配置lazy-init=true来让Bean延迟实例化
ApplicationContext 应用上下文,继承BeanFactory接口,它是Spring的更高级的容器,提供了更多的有用的功能
BeanFactory 与 ApplicationContext 区别?
BeanFactory在启动的时候不会去实例化Bean,中有从容器中拿Bean的时候才会去实例化;
BeanFactory是Spring里面最低层的接口,提供了最简单的容器的功能,只提供了实例化对象和拿对象的功能;
spring注入ApplicationContext对象的三种方式
第一种:直接注入
@Component
public class User {
@Autowired
private ApplicationContext applicationContext;
public void show(){
System.out.println(applicationContext.getClass().getName());
}
}
第二种方式:构造器注入
@Component
public class Bank {
private ApplicationContext applicationContext;
public Bank(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void show(){
System.out.println(applicationContext.getClass().getName()+"===========");
}
}
第三种:实现接口方式
@Component
public class Book implements ApplicationContextAware{
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext=applicationContext;
}
public void show(){
System.out.println(applicationContext.getClass().getName());
}
}
spring事件机制(订阅发布模式 == 观察者模式)
ApplicationContext事件机制是观察者设计模式的 实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext事件处理。
如果容器中有一个ApplicationListener Bean,每当ApplicationContext发布ApplicationEvent时,ApplicationListener Bean将自动被触发。
ApplicationEvent:容器事件,必须由ApplicationContext发布;
ApplicationListener:监听器,可由容器中的任何监听器Bean担任。
定义容器事件
public class EmailEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L;
//属性
private String address;
private String text;
//构造方法
public EmailEvent(Object source) {
super(source);
}
public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
}
//getter和setter设置
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
定义监听器
port org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* email之监听类
* 容器事件的监听器类 必须实现ApplicationListener接口
*/
public class EmailNotifier implements ApplicationListener<ApplicationEvent>{
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof EmailEvent){
EmailEvent emailEvent = (EmailEvent) event;
System.out.println("email's address:"+emailEvent.getAddress());
System.out.println("email's text:"+emailEvent.getText());
} else {
System.out.println("the Spring's event:"+event);
}
}
}
将监听器注入到spring容器
Xml代码
<!-- 配置事件监听 -->
<bean class="com.cxg.test.springPlatfrom.EmailNotifier" />
测试
public class SpringTest {
public static void main(String arg[]){
//读取Spring容器的配置文件
@SuppressWarnings("resource")
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
//创建一个事件对象
EmailEvent emailEvent = new EmailEvent("hello ApplicationContext !", "demo@qq.com", "This is a test!");
//主动触发事件监视机制
applicationContext.publishEvent(emailEvent);
}
}