1. 程式人生 > 其它 >HM-SpringBoot2.3【SpringBoot自動配置-Enable*註解】

HM-SpringBoot2.3【SpringBoot自動配置-Enable*註解】


1 建立模組1

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4 
 5     <modelVersion>4.0.0</
modelVersion> 6 7 <parent> 8 <groupId>org.springframework.boot</groupId> 9 <artifactId>spring-boot-starter-parent</artifactId> 10 <version>2.5.3</version> 11 <relativePath/> <!-- lookup parent from repository -->
12 </parent> 13 14 <groupId>com.haifei</groupId> 15 <artifactId>springboot9-enable</artifactId> 16 <version>0.0.1-SNAPSHOT</version> 17 <name>springboot9-enable</name> 18 <description>Demo project for Spring Boot</description
> 19 20 <properties> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 <dependency> 26 <groupId>org.springframework.boot</groupId> 27 <artifactId>spring-boot-starter</artifactId> 28 </dependency> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-starter-test</artifactId> 32 <scope>test</scope> 33 </dependency> 34 </dependencies> 35 36 <build> 37 <plugins> 38 <plugin> 39 <groupId>org.springframework.boot</groupId> 40 <artifactId>spring-boot-maven-plugin</artifactId> 41 </plugin> 42 </plugins> 43 </build> 44 45 </project>

2 建立模組2

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4 
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <parent>
 8         <groupId>org.springframework.boot</groupId>
 9         <artifactId>spring-boot-starter-parent</artifactId>
10         <version>2.5.3</version>
11         <relativePath/> <!-- lookup parent from repository -->
12     </parent>
13 
14     <groupId>com.haifei</groupId>
15     <artifactId>springboot10-enable-other</artifactId>
16     <version>0.0.1-SNAPSHOT</version>
17     <name>springboot10-enable-other</name>
18     <description>Demo project for Spring Boot</description>
19 
20     <properties>
21         <java.version>1.8</java.version>
22     </properties>
23 
24     <dependencies>
25         <dependency>
26             <groupId>org.springframework.boot</groupId>
27             <artifactId>spring-boot-starter</artifactId>
28         </dependency>
29     </dependencies>
30 
31 </project>

3 在模組2中建立Bean類及其配置類

1 package com.haifei.domain;
2 
3 public class User {
4 }
 1 package com.haifei.config;
 2 
 3 import com.haifei.domain.User;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 
 7 @Configuration
 8 public class UserConfig {
 9 
10     @Bean
11     public User user(){
12         return new User();
13     }
14 
15 }

4 在模組1中獲取Bean

1 pom.xml
2 
3 +
4 
5         <dependency>
6             <groupId>com.haifei</groupId>
7             <artifactId>springboot10-enable-other</artifactId>
8             <version>0.0.1-SNAPSHOT</version>
9         </dependency>  
 1 package com.haifei.springboot9enable;
 2 
 3 import com.haifei.config.EnableUser;
 4 import com.haifei.config.UserConfig;
 5 import org.springframework.boot.SpringApplication;
 6 import org.springframework.boot.autoconfigure.SpringBootApplication;
 7 import org.springframework.context.ConfigurableApplicationContext;
 8 import org.springframework.context.annotation.ComponentScan;
 9 import org.springframework.context.annotation.Import;
10 
11 /**
12  *
13  */
14 @SpringBootApplication
15 //@ComponentScan("com.haifei.config")
16 //@Import(UserConfig.class)
17 @EnableUser
18 public class Springboot9EnableApplication {
19 
20     public static void main(String[] args) {
21         ConfigurableApplicationContext context = SpringApplication.run(Springboot9EnableApplication.class, args);
22         //獲取Bean(由於該Bean是其他模組中的Bean,需要在pom中匯入依賴座標)
23         Object user = context.getBean("user");
24         System.out.println(user);
25 
26         /*
27         不能直接獲取bean-user,NoSuchBeanDefinitionException: No bean named 'user' available
28         原因:
29             @SpringBootApplication的元註解@ComponentScan指定掃描範圍僅為當前引導類所在包及其子包
30             而這個bean-user在springboot10-enable-other模組中的com.haifei.config包下
31             此類屬於springboot9-enable模組中的com.haifei.springboot9enable包下
32          */
33 
34         /*
35         解決方法1:使用@ComponentScan掃描com.itheima.config包
36         在該類上加上註解@ComponentScan,並指定bean的配置類所在包
37         com.haifei.domain.User@517566b
38          */
39 
40         /*
41         解決方法2:可以使用@Import註解,載入類。這些類都會被Spring建立,並放入IOC容器
42         在該類上加上註解@Import,並指定bean的配置類.class
43         com.haifei.domain.User@e8df99a
44          */
45 
46         /*
47         解決方法3:可以對Import註解進行封裝
48         在springboot10-enable-other模組中的com.haifei.config包下建立註解@EnableUser,並對其配置方法2中的@Import
49         在該類上加上註解@EnableUser即可
50         com.haifei.domain.User@2f40e5db
51          */
52     }
53 
54 }

5 模組2中自定義的@EnableUser註解

 1 package com.haifei.config;
 2 
 3 import org.springframework.context.annotation.Import;
 4 
 5 import java.lang.annotation.*;
 6 
 7 @Target({ElementType.TYPE})
 8 @Retention(RetentionPolicy.RUNTIME)
 9 @Documented //以上三個為@Import註解的元註解
10 @Import(UserConfig.class)
11 public @interface EnableUser {
12 }