SpringBootカスタム構成
6659 ワード
SpringBootがDruid接続プールの構成を提供していないなど、自分で構成を定義する必要がある場合があります.自分で構成を書く必要があります.
プロファイル
# Druid
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
##########druid #########
spring.datasource.druid.url=jdbc:mysql://127.0.0.1/db_xm_mall?serverTimezone=UTC
spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.druid.username=chy
spring.datasource.druid.password=abcd
## , 0
spring.datasource.druid.initialSize=10
# , 8
spring.datasource.druid.maxActive=30
spring.datasource.druid.minIdle=10
# ,
spring.datasource.druid.maxWait=2000
# PreparedStatement, false
#spring.datasource.druid.poolPreparedStatements=true
# PreparedStatement , -1( )。 0 PreparedStatement,
spring.datasource.druid.maxOpenPreparedStatements=20
クラスの構成
/**
* Druid
*/
@ConfigurationProperties("spring.datasource.druid") //
@Component // @Configuration,@Configuration @Component
@Setter //Lombok 。 , setter , @Data
public class DruidConfig {
// springboot
private String url;
private String driverClassName;
private String username;
private String password;
private int initialSize;
private int maxActive;
private int minIdle;
private int maxWait;
// private boolean poolPreparedStatements;
private int maxOpenPreparedStatements;
@Bean // Spring
// @Primary // , , ,
public DataSource dataSource() {
DruidDataSource datasource = new DruidDataSource();
datasource.setUrl(url);
datasource.setDriverClassName(driverClassName);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setInitialSize(initialSize);
datasource.setMaxActive(maxActive);
datasource.setMinIdle(minIdle);
datasource.setMaxWait(maxWait);
// datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxOpenPreparedStatements(maxOpenPreparedStatements);
return datasource;
}
}
3点:
プロファイルを書くときに適切なプロンプトがある場合は、依存を追加する必要があります.
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
コンパイル、target/classes/META-INFでspring-configuration-metadataが生成されます.jsonファイルは、コンフィギュレーションファイルに関連するコンフィギュレーションを書くときにプロンプトが表示されます(コンフィギュレーションアイテムがすでに存在する場合はプロンプトは表示されません).