SpringBook Framework @EableAutoConfiguration Project Application

Keywords: PHP Spring JDBC Maven MySQL

Scenario: In the project, if you want to automatically assemble other customized Maven project library configurations in the current maven project, you will get multiple maven references. If you want simple configurations, spring boot will automatically assemble database configuration classes.

From then on, we think about how our new project assembles third-party jar s. Tracking the spring boot source shows that the method is actually very simple. springboot achieves this through the @EableAutoConfiguration annotation.

Principle: The annotation @EnableAutoConfiguration refers to the @Import ({AutoConfiguration ImportSelector. class}) annotation and defines the getCandidateConfigurations method to load the custom configuration class in the AutoConfiguration ImportSelector class.

 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
        List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
        Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
        return configurations;
    }

As you can see from the information prompted by this method, if you customize the package you need to assemble, you need to define it in the META-INF/spring.factories file. How to define it? Find the file org/spring framework/boot/spring-boot-autoconfigure/2.1.5.RELEASE/spring-boot-autoconfigure-2.1.5.RELEASE.jar!/META-INF/spring.factories in the import package (version 2.1.5 I currently use, find the corresponding method according to the actual version used)

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration

See Auto Configure, then we need to import resources META-INF spring. factories into the current maven project file, add the full path of the configuration class that needs to be assembled in the file, as follows

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.boot.config.core.config.BeanConfiguration #Full paths of configuration classes that need to be assembled

At this point, the configuration class instance in the current project block.

So far, here's the code

Preparing two maven projects

First project

(1) Two bean s are defined in the domain, without attributes and methods, and without annotations. The BeanConfiguration class is as follows

 1 package com.boot.config.core.config;
 2 
 3 import com.boot.config.core.domain.Order;
 4 import com.boot.config.core.domain.Product;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 
 8 @Configuration
 9 public class BeanConfiguration {
10 
11     @Bean
12     public Order creatOrder() {
13         return new Order();
14     }
15 
16     @Bean
17     public Product createProduct() {
18         return new Product();
19     }
20 }
BeanConfiguration.java

(2) Add the following to the spring.factories file

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.boot.config.core.config.BeanConfiguration

Similarly, in confing-core projects, do the same thing.

Second item

(1) Define the data source bean class

 1 package com.boot.config.core;
 2 
 3 /**
 4  * Data Source Attribute Class
 5  *
 6  */
 7 public class DatasourceProperties
 8 {
 9     private  String driverClassName;
10     private String url;
11     private String userName;
12     private String password;
13 
14     public String getDriverClassName() {
15         return driverClassName;
16     }
17 
18     public void setDriverClassName(String driverClassName) {
19         this.driverClassName = driverClassName;
20     }
21 
22     public String getUrl() {
23         return url;
24     }
25 
26     public void setUrl(String url) {
27         this.url = url;
28     }
29 
30     public String getUserName() {
31         return userName;
32     }
33 
34     public void setUserName(String userName) {
35         this.userName = userName;
36     }
37 
38     public String getPassword() {
39         return password;
40     }
41 
42     public void setPassword(String password) {
43         this.password = password;
44     }
45 }
DatasourceProperties.java

(2) Define the data source configuration class

 1 package com.boot.config.core;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 
 7 @Configuration
 8 public class DatasourceConfig {
 9 
10     @Bean
11     @ConfigurationProperties(prefix = "jdbc")
12     public DatasourceProperties createDatasourceProperties() {
13         return new DatasourceProperties();
14     }
15 }
DatasourceConfig.java

(3) Configuration resource file spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.boot.config.core.DatasourceConfig

Third project, citing projects 1 and 2

(1) Reference to third-party jar package dependencies in pom.xml files

        <dependency>
            <groupId>com.boot.config.core</groupId>
            <artifactId>bean-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.boot.config.core</groupId>
            <artifactId>config-core</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

(2) Setting up data source configuration in application.properties file

jdbc.driverClassName=com.mySql.jdbc.mySqlDriver
jdbc.userName=root
jdbc.password=root.123
jdbc.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/bookstore

(3) Printing third-party configuration information in main method

 1 package com.boot.auto.bootauto;
 2 
 3 import com.boot.config.core.DatasourceProperties;
 4 import com.boot.config.core.config.BeanConfiguration;
 5 import com.boot.config.core.domain.Order;
 6 import com.boot.config.core.domain.Product;
 7 import org.springframework.boot.SpringApplication;
 8 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
 9 import org.springframework.context.ConfigurableApplicationContext;
10 import org.springframework.context.annotation.ComponentScan;
11 
12 //Setting Unassembled Configuration Classes
13 //@EnableAutoConfiguration(exclude= {BeanConfiguration.class})
14 @EnableAutoConfiguration
15 @ComponentScan
16 public class BootAutoApplication {
17 
18     public static void main(String[] args) {
19         ConfigurableApplicationContext context = SpringApplication.run(BootAutoApplication.class, args);
20         System.out.println(context.getBean(Order.class));
21         System.out.println(context.getBean(Product.class));
22 
23         DatasourceProperties jdbcBean = context.getBean(DatasourceProperties.class);
24 
25         System.out.println(jdbcBean.getDriverClassName());
26         System.out.println(jdbcBean.getUrl());
27         System.out.println(jdbcBean.getUserName());
28         System.out.println(jdbcBean.getPassword());
29 
30         context.close();
31     }
32 
33 }
BootAutoApplication.java

(4) Printing results

com.boot.config.core.domain.Order@3a71c100
com.boot.config.core.domain.Product@5b69fd74
com.mySql.jdbc.mySqlDriver
jdbc:mysql://localhost:3306/bookstore
root
root.123

From this we can see that our other (third-party) configuration classes have been successfully assembled into our current project.

summary

1. Configuration classes are defined in the referenced third-party maven project (A)

2. Add the full path of the configuration class to be assembled in the META_INFO/spring.properties file of Project A

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.boot.config.core.config.BeanConfiguration

3. Reference Item (B) Add a jar reference to A in pom.xml

4. Use @EnableAutoConfiguration to access the configuration of Project A in an instance of Configurable Application Context.

github source code: https://github.com/LF20160912/boot-proj

Posted by sasori on Sun, 30 Jun 2019 16:38:56 -0700