SpringBoot超詳細(xì)講解@Enable*注解和@Import
@Enable*
創(chuàng)建一個(gè)主啟動(dòng)類

package com.you.boot;
import com.you.config.EnableUser;
import com.you.config.UserConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
public class BootEnable02Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BootEnable02Application.class, args);
// 獲取Bean
Object user = context.getBean("user");
System.out.println(user);
}
}新建一個(gè)Module


【如果創(chuàng)建完,不是并列,如下圖(并列),而是包含】

【錯(cuò)誤原因】路徑寫錯(cuò)了,需要把紅框類的去掉

在新Module里寫一個(gè)用戶類和配置類
User類

package com.you.domain;
public class User {
}UserConfig
package com.you.config;
import com.you.domain.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user()
{
return new User();
}
}在主啟動(dòng)類的Pom里引入配置類的依賴

<dependency>
<groupId>com.you</groupId>
<artifactId>boot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>啟動(dòng)。問題SpringBoot不可以直接獲取Jar包中定義的Bean

解決辦法
解放方案一
@ConponentScan("配置類包名")


效果:

解決方案二
@Import
效果圖

解決方案三
@Import的封裝
在配置類包下,創(chuàng)建如下類

package com.you.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}效果:

@Import
@Import提供了四種用法
1、導(dǎo)入Bean
2、導(dǎo)入配置類
3、導(dǎo)入ImportSelector實(shí)現(xiàn)類,一般用于加載配置文件的類
4、導(dǎo)入ImportBeanDefinitionRegistrar實(shí)現(xiàn)類
1、導(dǎo)入Bean
@SpringBootApplication
@Import(User.class)
public class BootEnable02Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(BootEnable02Application.class, args);
User user = context.getBean(User.class);
System.out.println(user);
}
}可以獲取到

2、導(dǎo)入配置類
和上面@Enable*詳解中的一樣

3、導(dǎo)入ImportSelector實(shí)現(xiàn)類
一般用于加載配置文件的類
首先需要寫一個(gè)ImporeSelector的實(shí)現(xiàn)類,還是寫在config包下
package com.you.config;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MySelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.you.domain.User"};
}
}效果

4、導(dǎo)入ImportBeanDefinitionRegistrar實(shí)現(xiàn)類
首先需要寫一個(gè)ImportBeanDefinitionRegistrar實(shí)現(xiàn)類,同樣寫在config包下
package com.you.config;
import com.you.domain.User;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(User.class).getBeanDefinition();
registry.registerBeanDefinition("user",beanDefinition);
}
}效果


到此這篇關(guān)于SpringBoot超詳細(xì)講解@Enable*注解和@Import的文章就介紹到這了,更多相關(guān)SpringBoot @Enable*注解和@Import內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)Swing組件定制Button示例
這篇文章主要介紹了Java實(shí)現(xiàn)Swing組件定制Button,涉及java Swing組件Button相關(guān)屬性設(shè)置與使用操作技巧,需要的朋友可以參考下2018-01-01
使用Java找出兩個(gè)List中的重復(fù)元素三種方法
在Java編程中,我們經(jīng)常需要找出兩個(gè)列表(List)中的重復(fù)元素,在本文中,我們將探討三種方法來(lái)實(shí)現(xiàn)這一目標(biāo),需要的朋友可以參考下2023-10-10
MyBatis SpringMVC整合實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了MyBatis SpringMVC整合實(shí)現(xiàn)步驟詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot如何通過配置文件(yml,properties)限制文件上傳大小
這篇文章主要介紹了SpringBoot如何通過配置文件(yml,properties)限制文件上傳大小,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
解決使用stream將list轉(zhuǎn)map時(shí),key重復(fù)導(dǎo)致報(bào)錯(cuò)的問題
這篇文章主要介紹了解決使用stream將list轉(zhuǎn)map時(shí),key重復(fù)導(dǎo)致報(bào)錯(cuò)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringBoot實(shí)現(xiàn)各種參數(shù)校驗(yàn)總結(jié)(建議收藏!)
本文深入解析了Spring?Validation的使用方法、實(shí)現(xiàn)原理及最佳實(shí)踐,詳細(xì)介紹了各種參數(shù)校驗(yàn)場(chǎng)景,如requestBody和requestParam/PathVariable的使用,并探討了分組校驗(yàn)、嵌套校驗(yàn)和自定義校驗(yàn)的高級(jí)應(yīng)用,需要的朋友可以參考下2024-09-09
Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化有哪些方式?
今天給大家?guī)?lái)的文章是關(guān)于Spring的相關(guān)知識(shí),文章圍繞著Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化有哪些方式展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
java語(yǔ)言自行實(shí)現(xiàn)ULID過程底層原理詳解
這篇文章主要為大家介紹了java語(yǔ)言自行實(shí)現(xiàn)ULID過程底層原理詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10

