zoukankan      html  css  js  c++  java
  • Spring常用的接口和类(一)

    一、ApplicationContextAware接口

         当一个类需要获取ApplicationContext实例时,可以让该类实现ApplicationContextAware接口。代码展示如下:

    public class Animal implements ApplicationContextAware, BeanNameAware{
        private String beanName;
        private ApplicationContext applicationContext;
    
        public void setBeanName(String name) {
            this.beanName = name;
        }
        
        /**
         * @param applicationContext 该参数将由Spring容器自动赋值
         */
        public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
            this.applicationContext = applicationContext;
        }
    
        public void run(){
            System.out.println(beanName);
            
            //发布自定义事件
            AnimalEvent event = new AnimalEvent(this, "老虎");
            applicationContext.publishEvent(event);
        }
    }

         通过@Autowired注解可以自动装配一些常用对象实例:

    @Autowired
    private MessageSource messageSource; 
    
    @Autowired
    private ResourceLoader resourceLoader; 
    
    @Autowired
    private ApplicationContext applicationContext;

    二、ApplicationEvent抽象类

         当需要创建自定义事件时,可以新建一个继承自ApplicationEvent抽象类的类。代码展示如下:

    /**
     * 自定义事件
     */
    public class AnimalEvent extends ApplicationEvent {
        private String name;
        
        public String getName() {
            return name;
        }
    
        /**
         * @param source 事件源对象
         */
        public AnimalEvent(Object source){
            super(source);
        }
        
        public AnimalEvent(Object source, String name){
            super(source);
            this.name = name;
        }
    }

    三、ApplicationListener接口

         当需要监听自定义事件时,可以新建一个实现ApplicationListener接口的类,并将该类配置到Spring容器中。代码展示如下:

    /**
     * 自定义事件监听器
     */
    public class CustomEventListener implements ApplicationListener {
        public void onApplicationEvent(ApplicationEvent event) {
            if(event instanceof AnimalEvent){
                AnimalEvent animalEvent = (AnimalEvent)event;
                System.out.println("触发自定义事件:Animal name is " + animalEvent.getName());
            }
        }
    }
    <!-- 自定义事件监听器:Spring容器自动注册它 -->
    <bean id="customEventListener" class="com.cjm.spring.CustomEventListener"/> 

         要发布自定义事件,需要调用ApplicationContext的publishEvent方法,具体用法请看Animal类的源码。

    四、BeanNameAware接口

         当bean需要获取自身在容器中的id/name时,可以实现BeanNameAware接口。

    五、InitializingBean接口

          当需要在bean的全部属性设置成功后做些特殊的处理,可以让该bean实现InitializingBean接口。       效果等同于bean的init-method属性的使用或者@PostContsuct注解的使用。       三种方式的执行顺序:先注解,然后执行InitializingBean接口中定义的方法,最后执行init-method属性指定的方法。

    六、DisposableBean接口       当需要在bean销毁之前做些特殊的处理,可以让该bean实现DisposableBean接口。       效果等同于bean的destroy-method属性的使用或者@PreDestory注解的使用。       三种方式的执行顺序:先注解,然后执行DisposableBean接口中定义的方法,最后执行destroy-method属性指定的方法

  • 相关阅读:
    Longest Consecutive Sequence 分类: Leetcode(线性表) 2015-02-04 09:54 55人阅读 评论(0) 收藏
    Median of Two Sorted Arrays 分类: Leetcode(线性表) 2015-02-04 09:05 54人阅读 评论(0) 收藏
    Python+Scrapy(完整包全安装过程) 分类: 安装配置说明 2015-02-02 21:29 108人阅读 评论(0) 收藏
    Remove Element 分类: Leetcode(线性表) 2015-01-29 10:47 55人阅读 评论(0) 收藏
    Search in Rotated Sorted Array 分类: Leetcode(线性表) 2015-01-29 10:23 58人阅读 评论(0) 收藏
    机器学习基础(四)LMS,代价函数的求解和概率意义 分类: 机器学习 2015-01-26 14:52 114人阅读 评论(0) 收藏
    机器学习基础(一)线性回归
    AcWing1341. 十三号星期五
    JavaWebServle执行流程解析
    AcWing 466. 回文日期
  • 原文地址:https://www.cnblogs.com/damowang/p/4305153.html
Copyright © 2011-2022 走看看