zoukankan      html  css  js  c++  java
  • 对Spring aware理解

     aware翻译过来时就是意识到,我对他的理解就是spring的感知器。是不是很诡异这个名字起得^_^

    先来看看aware接口的结构

     spring提供了许多的aware,Aware.java也只是做一个标志,他并没有定义任何的方法

     spring提供的这些aware只展示红框框起来的额三个

    一、BeanNameAware

    从名字的定义上来看他就是bean对象名字的感知器,他可以获取到bean对象的名字的,目前我只知道在记录日志的时候确实好用,其他的功能有待挖掘。。。。

    package com.lhf.aware;
    
    import org.springframework.beans.factory.BeanNameAware;
    
    public class DomeBeanNameAware implements BeanNameAware {
        private String beanName;
        @Override
        public void setBeanName(String name) {
            this.beanName = name;
        }
        public void test(){
            System.out.println("bean的名称:"+beanName);
        }
    }
    
    》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    
    package com.lhf.aware;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.lhf.aware")
    public class Config {
    
        @Bean
        public DomeBeanNameAware domeBeanNameAware(){
            return new DomeBeanNameAware();
        }
    }
    
    》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    
    package com.lhf.aware;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class App {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
            DomeBeanNameAware bean = context.getBean(DomeBeanNameAware.class);
            bean.test();
            context.close();
        }
    }

    使用很简单,正如上边代码所示,只需要实现BeanNameAware接口并重写setBeanName方法即可,该方法的入参就是bean对象的名称。

    二、ApplicationContextArare

    spring容器的感知器,可以获取到spring的容器对象

    package com.lhf.aware;
    
    import org.springframework.beans.BeansException;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationContextAware;
    import org.springframework.context.support.GenericApplicationContext;
    import org.springframework.stereotype.Component;
    
    @Component
    public class DomeApplicationContextAware<T> implements ApplicationContextAware, InitializingBean, DisposableBean {
    
        private ApplicationContext context;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
            this.context = applicationContext;
            if (context instanceof GenericApplicationContext) {
    //            注销spring容器
                ((GenericApplicationContext) applicationContext).registerShutdownHook();
            }
        }
    
    
        @Override
        public void afterPropertiesSet() throws Exception {
            System.out.println("bean对象被创建了");
        }
    
        @Override
        public void destroy() throws Exception {
            System.out.println("bean对象被销毁了");
        }
    }
    
    》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    package com.lhf.aware;
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan("com.lhf.aware")
    public class Config {
    
        
    }
    
    》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
    
    package com.lhf.aware;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class App {
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
            context.getBean(DomeApplicationContextAware.class);
    
    //        为了证明不是这里关闭的吧下边的一行注释
    //        context.close();
        }
    }

    同样的使用ApplicationContextAware同样是实现该接口,并重写set方法。在这个东东很秀,要知道,非spring的对象是不能直接使用spring的bean对象的,而通过这个就可以用了。

    三、BeanFactoryAware

    类似的,beanFactory的感知器用法和上边两种一样,都是实现BeanFactoryAware,重写set方法,代码就不贴了。

    总结aware的功能还是很方便的,而且使用起来也很简单粗暴。博客写的不好,大佬勿喷,欢迎吐槽

  • 相关阅读:
    监测你的SQL SERVER让瓶颈暴露
    SQL Server日志文件总结及日志满的处理
    SQL保持多台服务器数据的一致性
    SAS的函数
    业务单号自动增长的处理办法
    有一点迷茫了
    怎么强制弹出窗口永远在最前面(转)
    XML技术上传文件
    SQL复制表结构的通用存储过程(转)
    ACCESS中使用SQL语句应注意的地方、与sql server的区别及几点技巧(整理中)
  • 原文地址:https://www.cnblogs.com/Tiandaochouqin1/p/12190471.html
Copyright © 2011-2022 走看看