对Spring aware理解
程序员文章站
2023-03-26 21:03:23
aware翻译过来时就是意识到,我对他的理解就是spring的感知器。是不是很诡异这个名字起得^_^ 先来看看aware接口的结构 spring提供了许多的aware,Aware.java也只是做一个标志,他并没有定义任何的方法 spring提供的这些aware只展示红框框起来的额三个 一、Bean ......
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的功能还是很方便的,而且使用起来也很简单粗暴。博客写的不好,大佬勿喷,欢迎吐槽
上一篇: python获取当前文件的上一级目录
下一篇: django-实现登录短信验证
推荐阅读
-
关于Spring Cloud Eureka对服务的监控(上线,下线,续约等)
-
深入理解Spring Aop的执行顺序
-
对任何希望深入理解C#的程序员来说,这本书都是不容错过的经典书籍
-
我对Rails 1.2中 Restful的理解(二)
-
理解PHP中的Session及对Session有效期的控制_PHP
-
深入理解Spring MVC 思想 springmvc+mybatisdubbo+zookeeperrestful redis分布式缓存spring mvc
-
深入理解Spring MVC 思想 mvcspring框架beanservlet
-
基于spring-boot和docker-java实现对docker容器的动态管理和监控功能[附完整源码下载]
-
深入理解Spring MVC 思想 springmvc+mybatisdubbo+zookeeperrestful redis分布式缓存spring mvc
-
技术面试是什么意思(我对技术面试的理解与认识)