posts - 93,  comments - 2,  trackbacks - 0
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);
 
}
下面举个例子
自定义事件NotifyEvent:
import org.springframework.context.ApplicationEvent;

public class NotifyEvent  extends ApplicationEvent  {
    private String email;
    private String content;
    public NotifyEvent(Object source){
        super(source);
    }

    public NotifyEvent(Object source,String email,String content){
        super(source);
        this.email = email;
        this.content = content;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

定义监听器NotifyListener:
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

@Configuration
public class NotifyListener implements ApplicationListener<NotifyEvent>{
    @Override
    public void onApplicationEvent(NotifyEvent event) {
        System.out.println("邮件地址:" + event.getEmail());
        System.out.println("邮件内容:" + event.getContent());
    }
}

单元测试类ListenerTest:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServerLauncher.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ListenerTest {
    @Autowired
    private WebApplicationContext webApplicationContext;

    @Test
    public void testListener(){
        NotifyEvent event = new NotifyEvent("object","abc@qq.com","This is the content");
        webApplicationContext.publishEvent(event);
    }
}
posted on 2020-04-09 14:47 Terry Zou 阅读(1241) 评论(0)  编辑  收藏 所属分类: spring

只有注册用户登录后才能发表评论。


网站导航:
 
<2020年4月>
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789

常用链接

留言簿(2)

随笔分类

随笔档案

文章分类

文章档案

相册

收藏夹

Java

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜