您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“如何自動裝配-Aware注入Spring底層組件及原理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何自動裝配-Aware注入Spring底層組件及原理”吧!
Aware 接口,提供了類似回調(diào)函數(shù)的功能
自定義組件想要使用Spring 容器底層的一些組件(Application Context,Bean Factory);自定義組件需要實(shí)現(xiàn)xxxAware接口;在創(chuàng)建對象的時候,會調(diào)用接口規(guī)定的方法注入相關(guān)組件
package org.springframework.beans.factory; public interface Aware { }
package org.springframework.context; import org.springframework.beans.BeansException; import org.springframework.beans.factory.Aware; public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }
package org.springframework.context; import org.springframework.beans.factory.Aware; public interface ApplicationEventPublisherAware extends Aware { void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); }
package org.springframework.beans.factory; public interface BeanClassLoaderAware extends Aware { void setBeanClassLoader(ClassLoader classLoader); }
package org.springframework.beans.factory; import org.springframework.beans.BeansException; public interface BeanFactoryAware extends Aware { void setBeanFactory(BeanFactory beanFactory) throws BeansException; }
package org.springframework.beans.factory; public interface BeanNameAware extends Aware { void setBeanName(String name); }
package org.springframework.context; import org.springframework.beans.factory.Aware; import org.springframework.util.StringValueResolver; public interface EmbeddedValueResolverAware extends Aware { void setEmbeddedValueResolver(StringValueResolver resolver); }
package org.springframework.context; import org.springframework.beans.factory.Aware; import org.springframework.core.env.Environment; public interface EnvironmentAware extends Aware { void setEnvironment(Environment environment); }
package org.springframework.context.annotation; import org.springframework.beans.factory.Aware; import org.springframework.core.type.AnnotationMetadata; public interface ImportAware extends Aware { void setImportMetadata(AnnotationMetadata importMetadata); }
package org.springframework.context.weaving; import org.springframework.beans.factory.Aware; import org.springframework.instrument.classloading.LoadTimeWeaver; public interface LoadTimeWeaverAware extends Aware { void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver); }
package org.springframework.context; import org.springframework.beans.factory.Aware; public interface MessageSourceAware extends Aware { void setMessageSource(MessageSource messageSource); }
package org.springframework.jmx.export.notification; import org.springframework.beans.factory.Aware; public interface NotificationPublisherAware extends Aware { void setNotificationPublisher(NotificationPublisher notificationPublisher); }
package org.springframework.context; import org.springframework.beans.factory.Aware; import org.springframework.core.io.ResourceLoader; public interface ResourceLoaderAware extends Aware { void setResourceLoader(ResourceLoader resourceLoader); }
package com.hw.springannotation.beans; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.EmbeddedValueResolverAware; import org.springframework.stereotype.Component; import org.springframework.util.StringValueResolver; /** * @Description TODO * @Author hw * @Date 2018/11/28 15:44 * @Version 1.0 */ @Component public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("傳入的IOC " + applicationContext); this.applicationContext = applicationContext; } public void setBeanName(String name) { System.out.println("當(dāng)前bean的名字" + name); } public void setEmbeddedValueResolver(StringValueResolver resolver) { String value = resolver.resolveStringValue("你好${os.name} 我是#{20*20}"); System.out.println("解析的字符串:" + value); } }
運(yùn)行:
說白了 Aware接口類,就是一個回調(diào)類的抽象
到此,相信大家對“如何自動裝配-Aware注入Spring底層組件及原理”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。