本篇內(nèi)容主要講解“如何自動(dòng)裝配-Aware注入Spring底層組件及原理”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“如何自動(dòng)裝配-Aware注入Spring底層組件及原理”吧!
創(chuàng)新互聯(lián)公司長(zhǎng)期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為庫爾勒企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作,庫爾勒網(wǎng)站改版等技術(shù)服務(wù)。擁有十多年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
Aware 接口,提供了類似回調(diào)函數(shù)的功能
自定義組件想要使用Spring 容器底層的一些組件(Application Context,Bean Factory);自定義組件需要實(shí)現(xiàn)xxxAware接口;在創(chuàng)建對(duì)象的時(shí)候,會(huì)調(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接口類,就是一個(gè)回調(diào)類的抽象
到此,相信大家對(duì)“如何自動(dòng)裝配-Aware注入Spring底層組件及原理”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前文章:如何自動(dòng)裝配-Aware注入Spring底層組件及原理
網(wǎng)址分享:http://chinadenli.net/article20/gsjpco.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站策劃、外貿(mào)建站、Google、App設(shè)計(jì)、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)