SpringBoot customize their bean

Copyright: https://blog.csdn.net/weixin_41716049/article/details/90550180

springboot per autowire is directly injected into, and then injected into not set & get to modify the properties, which the height of the package arises a problem. As before with Spring, you can look in the xml configuration file, but SpringBoot it?

method one:

The most common DataSource database as an example. Generally injected directly application.properties DataSource configuration at the data source may be employed, return to SpringBoot default data source, known as the fastest in the history of HikariDataSource. But suppose I want to modify the configuration inside how? For example, a name for the connection pool?

1 package com.config;

2

3 import javax.sql.DataSource;

4

5 import org.springframework.context.annotation.Bean;

6 import org.springframework.context.annotation.Configuration;

7

8 com.zaxxer.hikari.HikariConfig amount;

9 import com.zaxxer.hikari.HikariDataSource;

10

11 @Configuration

12 public class DataSourceConfig {

13

14 @Bean

15 public DataSource getDataSource() {

16

17 HikariConfig config = new HikariConfig();

18 config.setUsername("name");

19 config.setPassword("pass");

20 config.setJdbcUrl("url");

21 //测试22 config.setPoolName("do you see me");

23

24 return new HikariDataSource(config);

25 }

26 }

DataSourceConfig direct establishment of a new category, then add @Configuration (config Rui new) notes. Finally, write a method, pro-test method name can easily. Add comment @Bean, then it will automatically load the configuration SpringBoot the class. You can customize their favorite attributes of.

ps. Here's a question, how SpringBoot know that Bean is the configuration of the data source it? Pro-test method name can be changed at random, it is based on the value returned? Great God, please advise.

Method Two:

Suppose you want to configure the variables in the configuration file inside, and then use direct variable has been defined in the class. This is better maintained.

1 package com.redis;

2

3 import org.springframework.boot.context.properties.ConfigurationProperties;

4 import org.springframework.context.annotation.Bean;

5 importorg.springframework.stereotype.Component;

6

7 import redis.clients.jedis.JedisPool;

8 import redis.clients.jedis.JedisPoolConfig;

9

10 @Component

11 @ConfigurationProperties(prefix = "spring.redis")

12 public class RedisConfig {

13

14 private String host;

15 private int port;

16 private String password;

17

18 / ** 19 * timeout 3000 ms

20 * @ return21 * 2018 Nian 5 Yue 21 Ri

22 */23 @Bean

24 public JedisPool getJedisPool() {

25 JedisPoolConfig config = new JedisPoolConfig();

26 config.setMaxTotal(1000);

27 config.setMaxIdle(1000);

28 config.setMaxWaitMillis(3000);

29 JedisPool pool = new JedisPool(config, host, port, 3000 ,password);

30 return pool;

31 }

32

33 }

Notes becomes @ConfigurationProperties (config Rui new properties), the definition of good can be used directly, very convenient.

Guess you like

Origin blog.csdn.net/weixin_41716049/article/details/90550180