SpringBoot自動(dòng)裝配之@Enable深入講解
SpringBoot中提供了很多Enable開頭的注解,這些注解都是用于動(dòng)態(tài)啟用某些功能的。而其底層原理是使用@Import注 解導(dǎo)入一些配置類,實(shí)現(xiàn)Bean的動(dòng)態(tài)加載。
提問:SpringBoot 工程是否可以直接獲取jar包中定義的Bean?
答:不可以
案例:
兩個(gè)子模塊
①子模塊要得到
②子模塊的User類的bean(這里用編號表示)
方法一:使用@ComponentScan掃描com.itheima.springbooyembal包

package com.enable.entity;
public class User {
}package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
引入依賴:
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>使用ComponentScan:
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@ComponentScan("com.enable.config")
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}測試如下:

方法二:可以使用@Import注解,加載類,這些類都會(huì)被Spring創(chuàng)建,并放入IOC容器。

package com.enable.entity;
public class User {
}package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}
引入依賴
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>使用Import注解
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@Import(UserConfig.class)
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}測試如下:

方法三:對@Import注解進(jìn)行封裝

自定義@EnableUser注解
package com.enable.config;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfig.class)
public @interface EnableUser {
}自定義配置類
package com.enable.config;
import com.enable.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
@Bean
public User user(){
return new User();
}
}新建實(shí)體類:
package com.enable.entity;
public class User {
}
引入依賴
<dependency>
<groupId>com.enable</groupId>
<artifactId>springboot-enable-other</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>使用自定義的注解
package com.example.demo;
import com.enable.config.EnableUser;
import com.enable.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
@EnableUser
public class SpringbootApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootApplication.class, args);
Object user = context.getBean("user");
System.out.println(user);
}
}測試如下:

到此這篇關(guān)于SpringBoot自動(dòng)裝配之@Enable深入講解的文章就介紹到這了,更多相關(guān)SpringBoot @Enable內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot集成RabbitMQ實(shí)現(xiàn)用戶注冊的示例代碼
這篇文章主要介紹了SpringBoot集成RabbitMQ實(shí)現(xiàn)用戶注冊的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
詳細(xì)介紹使用Java調(diào)用Python的四種方法
這篇文章主要給大家介紹了關(guān)于使用Java調(diào)用Python的四種方法,每種方法根據(jù)實(shí)際項(xiàng)目需求有其適用場景,其中,推薦使用Runtime.getRuntime()方法,因?yàn)樗鼮楹啙嵡乙子趯?shí)現(xiàn),需要的朋友可以參考下2024-10-10
SpringBoot中定時(shí)任務(wù)@Scheduled的多線程使用詳解
這篇文章主要為大家詳細(xì)介紹了pring Boot定時(shí)任務(wù)@Scheduled的多線程原理以及如何加入線程池來處理定時(shí)任務(wù),感興趣的可以了解一下2023-04-04
SpringBoot 統(tǒng)一請求返回的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot 統(tǒng)一請求返回的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
java中Calendar與Date類型互相轉(zhuǎn)換的方法
這篇文章主要介紹了java中Calendar與Date類型互相轉(zhuǎn)換的方法,Calendar與Date類型是我們?nèi)粘i_發(fā)中常用的兩種數(shù)據(jù)類型,它們用于不同的場景,兩者具有不同的方法,接下來通過實(shí)例給大家詳解,需要的朋友可以參考下2022-09-09
JVM性能調(diào)優(yōu)之運(yùn)行時(shí)參數(shù)小結(jié)
jvm是java的運(yùn)行環(huán)境,在jvm中有很多的參數(shù)可以進(jìn)行設(shè)置,本文主要介紹了JVM性能調(diào)優(yōu)之運(yùn)行時(shí)參數(shù)小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04

