ApplicationContext在啟動(dòng)的時(shí)候就把所有的Bean全部實(shí)例化了拥娄。還可以為Bean配置lazy-init=true來讓Bean延遲實(shí)例化
ApplicationContext 應(yīng)用上下文抽活,繼承BeanFactory接口香府,它是Spring的更高級的容器,提供了更多的有用的功能
BeanFactory 與 ApplicationContext 區(qū)別器紧?
BeanFactory在啟動(dòng)的時(shí)候不會(huì)去實(shí)例化Bean,中有從容器中拿Bean的時(shí)候才會(huì)去實(shí)例化;
BeanFactory是Spring里面最低層的接口敦跌,提供了最簡單的容器的功能,只提供了實(shí)例化對象和拿對象的功能沸毁;
spring注入ApplicationContext對象的三種方式
第一種:直接注入
@Component
public class User {
@Autowired
private ApplicationContext applicationContext;
public void show(){
System.out.println(applicationContext.getClass().getName());
}
}
第二種方式:構(gòu)造器注入
@Component
public class Bank {
private ApplicationContext applicationContext;
public Bank(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void show(){
System.out.println(applicationContext.getClass().getName()+"===========");
}
}
第三種:實(shí)現(xiàn)接口方式
@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事件機(jī)制(訂閱發(fā)布模式 == 觀察者模式)
ApplicationContext事件機(jī)制是觀察者設(shè)計(jì)模式的 實(shí)現(xiàn)峰髓,通過ApplicationEvent類和ApplicationListener接口傻寂,可以實(shí)現(xiàn)ApplicationContext事件處理。
如果容器中有一個(gè)ApplicationListener Bean携兵,每當(dāng)ApplicationContext發(fā)布ApplicationEvent時(shí)疾掰,ApplicationListener Bean將自動(dòng)被觸發(fā)。
ApplicationEvent:容器事件徐紧,必須由ApplicationContext發(fā)布静檬;
ApplicationListener:監(jiān)聽器,可由容器中的任何監(jiān)聽器Bean擔(dān)任并级。
定義容器事件
public class EmailEvent extends ApplicationEvent{
private static final long serialVersionUID = 1L;
//屬性
private String address;
private String text;
//構(gòu)造方法
public EmailEvent(Object source) {
super(source);
}
public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
}
//getter和setter設(shè)置
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;
}
}
定義監(jiān)聽器
port org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
/**
* email之監(jiān)聽類
* 容器事件的監(jiān)聽器類 必須實(shí)現(xiàn)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);
}
}
}
將監(jiān)聽器注入到spring容器
Xml代碼
<!-- 配置事件監(jiān)聽 -->
<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");
//創(chuàng)建一個(gè)事件對象
EmailEvent emailEvent = new EmailEvent("hello ApplicationContext !", "demo@qq.com", "This is a test!");
//主動(dòng)觸發(fā)事件監(jiān)視機(jī)制
applicationContext.publishEvent(emailEvent);
}
}