Various configuration methods of SpringBoot

  • Java configuration mainly relies on Java classes and some annotations. Some common annotations are:

  • @Configuration: Declare a class as a Configuration class instead of an XML file

  • @bean: Declared on a method to add the return value of the method to the Bean container in place of the label

  • @value: Basic type or String property injection

  • PropertySource: Specifies the external properties file

  • The Druid connection pool configuration is used as an example. The database name is Springboot_test

Methods a

<! -- Pop.xml --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>  </dependency>Copy the code
# the SRC/resources/JDBC. Properties JDBC. DriverClassName = com. Mysql.. JDBC Driver JDBC. Url = JDBC: mysql: / / 127.0.0.1:3306 / bos jdbc.username=root jdbc.password=123456Copy the code
//src\main\java\com\itheima\config\DruidConfig.java @Configuration @PropertySource("classpath:jdbc.properties") public class DruidConfig { @Value("${jdbc.url}") String url; @Value("${jdbc.driverClassName}") String driverClassName; @Value("${jdbc.username}") String username; @Value("${jdbc.password}") String password; @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driverClassName); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; }}Copy the code

Reading:

  • @configuration: Declare that DruidConfig is a Configuration class

  • @ PropertySource: specify properties file path is: the classpath: JDBC. Properties

  • @value injects a Value into an attribute (only a primitive type or String)

  • The @bean declares the dataSource() method as a method of the registered Bean, and Spring automatically calls the method, adding the return value of the method to the Spring container.

Way 2

<! -- Pop.xml --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>  </dependency> <! -- ============ not added in IDEA will be red, But does not affect the function of = = = = = = = = = = = = = = = = = -- -- > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <! - = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -- -- >Copy the code
# src/resources/application.properties jdbc.driverClassName=com.mysql.jdbc.Driver The JDBC url = JDBC: mysql: / / 127.0.0.1:3306 / bos JDBC. Username = root JDBC. Password = 123456Copy the code
//src\main\java\com\itheima\config\DruidConfig.java @ConfigurationProperties(prefix = "jdbc") public class DruidProperties { private String url; private String driverClassName; private String username; private String password; public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDriverClassName() { return driverClassName; } public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}Copy the code
//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
	@Bean
	public DataSource dataSource(DruidProperties dp) {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(dp.getDriverClassName());
		dataSource.setUrl(dp.getUrl());
		dataSource.setUsername(dp.getUsername());
		dataSource.setPassword(dp.getPassword());
		return dataSource;
	}
}

Copy the code

Reading:

  • @ ConfifigurationProperties annotations declared the current class of attribute reading class, the class is defined on the various properties, with properties file name must be the JDBC. The latter part is consistent.

  • @ EnableConfigurationProperties () statement to use the properties of the reading class, use the class there are three kinds of injection pattern

1. The @autowired injection

//src\main\java\com\itheima\config\DruidConfig.java
@Configuration
@EnableConfigurationProperties(DruidProperties.class)
public class DruidConfig {
   @Autowired
   private DruidProperties dp;
   @Bean
   public DataSource dataSource() {
   	DruidDataSource dataSource = new DruidDataSource();
   	//setter
   	return dataSource;
   }
}

Copy the code

Constructor injection

``` //src\main\java\com\itheima\config\DruidConfig.java @Configuration @EnableConfigurationProperties(DruidProperties.class) public class DruidConfig { private DruidProperties dp; public DruidConfig(DruidProperties dp){ this.dp = dp; } @Bean public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); //setter return dataSource; }} ` ` `Copy the code

3. Injected as a method parameter to @bean (used in this example)

//src\main\java\com\itheima\config\DruidConfig.java @Configuration @EnableConfigurationPerProperties(DruidProperties.class) public class DruidConfig { @Bean public DataSource dataSource(DruidProperties dp) { DruidDataSource dataSource = new DruidDataSource(); //setter return dataSource; }}Copy the code

The property read class solves the problem that @Value cannot read object properties (such as user.friend.name), but it seems more troublesome

Method 3 (Recommended)

In fact, if only one Bean is needed for a property, we don’t need to inject it into a class.

<! -- Pop.xml --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>  </dependency> <! -- ============ not added in IDEA will be red, But does not affect the function of = = = = = = = = = = = = = = = = = -- -- > < the dependency > < groupId > org. Springframework. Boot < / groupId > <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <! - = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = -- -- >Copy the code
# src/resources/application.properties jdbc.driverClassName=com.mysql.jdbc.Driver The JDBC url = JDBC: mysql: / / 127.0.0.1:3306 / bos JDBC. Username = root JDBC. Password = 123456Copy the code
@Configuration
public class DruidConfig {
	@Bean
	@ConfigurationProperties(prefix = "jdbc")
	public DataSource dataSource() {
		return new DruidDataSource();
	}
}

Copy the code

Methods four

<! -- Pop.xml --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope>  </dependency>Copy the code
# src/resources/application.properties spring.datasource.type=com.alibaba.druid.pool.DruidDataSource Spring. The datasource. DriverClassName = com. Mysql. JDBC. Driver spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / bos spring.datasource.username=root spring.datasource.password=123456Copy the code

Consider: Why can configuration information be read this way without configuration classes?

When starting the main method, look at the SpringApplication constructor, tracing as follows

Easy to find, it gets the class name information from meta-INF /spring.factories and stores it in a one-button multi-value Map

The key is found in the blue part of the file, and the value is found in the green part. If you look back, you can see that it generates instances of these acquired classes and injects them into the IOC container.

Open DataSourceProperties and see if this isn’t method two?

Point into DataSourceProperties. Class

Conclusion:

When we add depend on automatic loading DataSourceAutoConfiguration when performing a start class, read DataSourceProperties class, according to the default prefix spring. The datasource in application. Read the information in the XML

The last

Master these springboot configuration, will let you deal with things in the work to more easily, feel the article is helpful to you, please give me a thumbs up, your support, is the biggest power of my creation!