SpringBoot自動裝配之@Enable深入講解
SpringBoot中提供了很多Enable開頭的注解,這些注解都是用于動態(tài)啟用某些功能的。而其底層原理是使用@Import注 解導入一些配置類,實現(xiàn)Bean的動態(tài)加載。
提問:SpringBoot 工程是否可以直接獲取jar包中定義的Bean?
答:不可以
案例:
兩個子模塊
①子模塊要得到
②子模塊的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注解,加載類,這些類都會被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注解進行封裝

自定義@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();
}
}新建實體類:
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);
}
}測試如下:

到此這篇關于SpringBoot自動裝配之@Enable深入講解的文章就介紹到這了,更多相關SpringBoot @Enable內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼
這篇文章主要介紹了SpringBoot集成RabbitMQ實現(xiàn)用戶注冊的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
SpringBoot中定時任務@Scheduled的多線程使用詳解
這篇文章主要為大家詳細介紹了pring Boot定時任務@Scheduled的多線程原理以及如何加入線程池來處理定時任務,感興趣的可以了解一下2023-04-04
SpringBoot 統(tǒng)一請求返回的實現(xiàn)
這篇文章主要介紹了SpringBoot 統(tǒng)一請求返回的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07

