您好,登錄后才能下訂單哦!
今天小編給大家分享一下SpringBoot怎么通過自定義注解實(shí)現(xiàn)配置類的自動注入的相關(guān)知識點(diǎn),內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
SpringBoot中通過@ConfigurationProperties
或@Value
注解就可以獲取配置文件中的屬性定義并綁定到Java Bean或?qū)傩陨?,這也是我們平常使用最多的一種方式。但是小胖在開發(fā)過程中就遇到一個問題:在做MQ的開發(fā)中,配置文件中會配置多個生產(chǎn)者分別提供不同的業(yè)務(wù)能力,如果通過@ConfigurationProperties
注解來實(shí)現(xiàn)的話,這就意味著需要創(chuàng)建多個屬性一樣的配置類,雖然說可以實(shí)現(xiàn)功能,但是很明顯,這不是一個很好的設(shè)計(jì)。場景如下所示:
producer1: password: xxx app: xxx address: url1 enabled: false producer2: password: xxx app: xxx address: url1 enabled: false
在我們?nèi)粘5拈_發(fā)工作中,經(jīng)??梢砸姷降氖峭ㄟ^自定義注解+攔截器+反射從而實(shí)現(xiàn)對權(quán)限的校驗(yàn)或者對實(shí)體類字段值格式進(jìn)行校驗(yàn)。那么,我們是不是也可以參考這個思路達(dá)到我們的目的呢?答案是肯定的,其實(shí)如果對Mabatis等組件比較熟悉的話,就可以看到這樣的設(shè)計(jì)。我們話不多少,開搞~
以下內(nèi)容,為了方便,我們將配置相關(guān)內(nèi)容改為人員(people)
首先,有一點(diǎn)是不會改變的,我們需要自定義一個配置類,用于讀取配置文件中的配置。這里,我們需要改變一下我們配置文件信息里。將所有的配置信息放到一個類里。
my: peoples: people1: userName: 張三 userSex: 男 people2: userName: 李四 userSex: 女
然后,定義一個配置類用來接收,這里通過@ConfigurationProperties
注解實(shí)現(xiàn)對配置的注入。要注意,因?yàn)槲覀冊趐eoples下面有很多的people,因此,屬性應(yīng)給定義的是一個MAP的類型。
@Component @ConfigurationProperties(prefix = "my",ignoreUnknownFields = false) public class PeopleConfigs { private Map<String, PeopleEntity> peoples; public Map<String, PeopleEntity> getPeoples() { return peoples; } public void setPeoples(Map<String, PeopleEntity> peoples) { this.peoples = peoples; } @Override public String toString() { return "PeopleConfigs{" + "peoples=" + peoples + '}'; } } public class PeopleEntity { private String userName; private String userSex; public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserSex() { return userSex; } public void setUserSex(String userSex) { this.userSex = userSex; } @Override public String toString() { return "PeopleEntity{" + "userName='" + userName + ''' + ", userSex='" + userSex + ''' + '}'; } }
這樣,Springboot就會自動加載我們這個配置類。但是,這個的整個PeopleConfigs
是一個Bean,并不能達(dá)到我們本文的目的,因此我們進(jìn)行后續(xù)的步驟。
我們聲明一個運(yùn)行時的注解,在屬性上進(jìn)行使用。這里定義name用來標(biāo)記需要注入的是哪個人。
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.FIELD}) public @interface People { String name() default ""; }
首先,定義一個autoConfig的配置類,該類通過@EnableConfigurationProperties
注解,指定PeopleConfig Bean在本類之前進(jìn)行裝載。通過@Bean
方法注解進(jìn)行bean聲明,此處調(diào)用的是單個people配置類的bean生成的方法。
@Configuration @EnableConfigurationProperties({PeopleConfigs.class}) public class PeopleAutoConfig { @Autowired PeopleConfigs peopleConfigs; @Bean public PeopleRegister peopleRegister(){ return new PeopleRegister(peopleConfigs); } }
這里不得不提到BeanPostProcessor
類,該類為我們提供了springBoot在bean初始化前后方便我們進(jìn)行其他自定義操作的一些接口。我們這里通過實(shí)現(xiàn)postProcessBeforeInitialization
方法,在bean裝載之前,通過反射判斷對應(yīng)bean上是否有我們自定義的people注解。如果有,則進(jìn)行注入操作。詳細(xì)代碼如下:
public class PeopleRegister implements BeanPostProcessor, ApplicationContextAware { private final PeopleConfigs peopleConfigs; private GenericApplicationContext applicationContext; PeopleRegister(PeopleConfigs peopleConfigs){ this.peopleConfigs = peopleConfigs; } @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Class<?> beanClass = AopUtils.getTargetClass(bean); Field[] fields = beanClass.getDeclaredFields(); Field[] var5 = fields; int var6 = fields.length; for(int var7 = 0;var7<var6;var7++){ Field field = var5[var7]; People annotation = field.getAnnotation(People.class); if (annotation!=null){ PeopleEntity entity = this.peopleConfigs.getPeoples().get(annotation.name()); if (!this.applicationContext.containsBean(annotation.name())){ ConfigurableListableBeanFactory beanFactory = this.applicationContext.getBeanFactory(); Object wrapperBean = beanFactory.initializeBean(entity, annotation.name()); beanFactory.registerSingleton(annotation.name(), Objects.requireNonNull(wrapperBean)); } try{ field.setAccessible(true); field.set(bean, this.applicationContext.getBean(annotation.name(), PeopleEntity.class)); }catch (Exception e){ e.printStackTrace(); } } } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (GenericApplicationContext)applicationContext; } }
前面工作進(jìn)行完成后,接下來就是我們的使用環(huán)節(jié),這里,我們僅需要通過@People(name = "人")
指定即可:
@Controller public class BaseController { @Autowired PeopleConfigs peopleConfigs; @People(name = "people1") PeopleEntity people1; @People(name = "people2") PeopleEntity people2; @ResponseBody @GetMapping("/test") public String test() { return peopleConfigs.toString()+people1.toString()+people2.toString(); } }
以上就是“SpringBoot怎么通過自定義注解實(shí)現(xiàn)配置類的自動注入”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。