您好,欢迎访问一九零五行业门户网

SpringBoot ApplicationContextAware拓展接口如何使用

applicationcontextaware接口:
public interface applicationcontextaware extends aware { void setapplicationcontext(applicationcontext applicationcontext) throws beansexception;}
首先aware接口就知道这是springboot扩展给用户使用的,这里提供了方法setapplicationcontext,参数就是传递spring容器上下文对象进来,我们可以接收这个上下文对象,我们要想知道获取spring容器上下文applicationcontext具体有什么作用,这才是扩展接口的目的所在,获取上下文根据上下文的特性做一些事情。
我们来看applicationcontext对象的方法:
来看看abstractapplicationcontext实现类的方法:
public object getbean(string name) throws beansexception {this.assertbeanfactoryactive();return this.getbeanfactory().getbean(name);} public <t> t getbean(string name, class<t> requiredtype) throws beansexception {this.assertbeanfactoryactive();return this.getbeanfactory().getbean(name, requiredtype);} public object getbean(string name, object... args) throws beansexception {this.assertbeanfactoryactive();return this.getbeanfactory().getbean(name, args);} public <t> t getbean(class<t> requiredtype) throws beansexception {this.assertbeanfactoryactive();return this.getbeanfactory().getbean(requiredtype);} public <t> t getbean(class<t> requiredtype, object... args) throws beansexception {this.assertbeanfactoryactive();return this.getbeanfactory().getbean(requiredtype, args);} public <t> objectprovider<t> getbeanprovider(class<t> requiredtype) {this.assertbeanfactoryactive();return this.getbeanfactory().getbeanprovider(requiredtype);} public <t> objectprovider<t> getbeanprovider(resolvabletype requiredtype) {this.assertbeanfactoryactive();return this.getbeanfactory().getbeanprovider(requiredtype);} public boolean containsbean(string name) {return this.getbeanfactory().containsbean(name);} public boolean issingleton(string name) throws nosuchbeandefinitionexception {this.assertbeanfactoryactive();return this.getbeanfactory().issingleton(name);} public boolean isprototype(string name) throws nosuchbeandefinitionexception {this.assertbeanfactoryactive();return this.getbeanfactory().isprototype(name);}
这里我们可以发现 getbean()方法很眼熟,因为在最最开始学习spring时没有用spring的脚手架创建项目,我们获取bean的方法通常是classpathcontextloader扫描bean的xml文件解析组成applicationcontext对象,再调用它的getbean方法获取实例bean。
由此可以发现我们主要的应用途径就是使用这个getbean的方法,那么动态的注入bean我们通过很多方法就能实现,所以这里不难想到,静态方法中无法使用注入的bean的问题。
其次我们来复现这个问题,大家来看如下的代码:
public class jsongetter {@resourceprivate uuidgetter uuidgetter;public static string test(){ return uuidgetter.getuuid();}public static jsonobject set0bjecttojsonobject(object data){ return jsonobject.parseobject(string.valueof(jsonobject.tojson(data)));}public static jsonobject setstringto3son0bject(string data) { return jsonobject.parseobject(data);}
我们发现在静态的test方法中调用注入的bean直接报错,这里解释一下:归功于类的加载机制与加载顺序,静态属性与静态代码块最先加载(static静态优先),这里加载静态方法是没有bean实例给你用的,自然会报错。
如何解决?我们可以采取spring获取bean对象时调用getbean方法的思路,在容器加载时将spring容器的上下文进行静态存储:
@component@lazy(value = false)public class springcontextholder implements applicationcontextaware, disposablebean { /** * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置) */ private static applicationcontext applicationcontext = null; public static applicationcontext getapplicationcontext() { assertcontextinjected(); return applicationcontext; } @suppresswarnings("unchecked") public static <t> t getbean(string name) { assertcontextinjected(); return (t) applicationcontext.getbean(name); } public static <t> t getbean(class<t> beantype) { assertcontextinjected(); return applicationcontext.getbean(beantype); } @override public void setapplicationcontext(@notnull applicationcontext applicationcontext) throws beansexception { springcontextholder.applicationcontext = applicationcontext; } @override public void destroy() { applicationcontext = null; } private static void assertcontextinjected() { assert.notnull(applicationcontext, "applicationcontext属性未注入, 请在applicationcontext.xml中定义springcontextholder."); } public static void pushevent(applicationevent event){ assertcontextinjected(); applicationcontext.publishevent(event); }}
这里只需要关注的是静态成员变量applicationcontext的定义、赋值与验证:
/** * 将上下文静态设置,在初始化组件时就进行静态上下文的覆盖(这个覆盖是将远spring容器的上下文对象引用加到我们预定设置) */ private static applicationcontext applicationcontext = null;
重写扩展接口的方法,实现静态上下文的覆盖:
@override public void setapplicationcontext(@notnull applicationcontext applicationcontext) throws beansexception { springcontextholder.applicationcontext = applicationcontext; }
将获取它的方法公有修饰,便于共享:
public static applicationcontext getapplicationcontext() { assertcontextinjected(); return applicationcontext; }
写到这里还是不明白,这么定义一个组件,将spring上下文对象静态覆盖到底有何作用?
不要慌,我们来看看这个类的这个方法:
public class appcontext { static transient threadlocal<map<string, string>> contextmap = new threadlocal<>(); ......省略n行业务代码 public static void filllogincontext() { dingappinfo appinfo = springcontextholder.getbean(dingappinfoservice.class).findappinfo(app_code); setdingverifyinfo(appinfo); cloudchatappinfo cloudchatappinfo = springcontextholder.getbean(cloudchatappinfoservice.class).findappinfo(app_code); setcloudchatinfo(cloudchatappinfo); } public static void clear() { contextmap.remove(); //本地线程的remove方法极其重要,注意每次给它使用之后一定要调用remove清理,防止内存泄露。 }}
我们发现上例代码中进行了查库的操作:
dingappinfo appinfo = springcontextholder.getbean(dingappinfoservice.class).findappinfo(app_code);
以上就是springboot applicationcontextaware拓展接口如何使用的详细内容。
其它类似信息

推荐信息