spring-boot 什么时候初始化配置文件
时间: 2023-10-14 07:04:27 浏览: 48
在Spring Boot应用程序启动时,Spring容器会自动初始化应用程序中所有的配置文件,包括application.properties、application.yml等。这些配置文件中的属性值会被加载到Spring环境中,并可以在应用程序的其他组件中使用。这个过程是在Spring Boot应用程序启动时自动完成的,你不需要显式地初始化配置文件。
相关问题
spring-boot 启动时 初始化 DynamicServerListLoadBalancer
`DynamicServerListLoadBalancer` 是 Netflix Ribbon 中的一个负载均衡器,用于从服务注册中心获取服务列表并进行负载均衡。
在 Spring Boot 中,可以使用 Netflix Ribbon 和 Spring Cloud Netflix 来集成该负载均衡器。
具体实现方法如下:
1. 添加 Ribbon 和 Spring Cloud Netflix 依赖
在 pom.xml 文件中添加以下依赖:
```xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>
```
2. 创建一个配置类,并指定负载均衡器的实现类
```java
@Configuration
public class RibbonConfiguration {
@Bean
public IRule ribbonRule() {
// 指定负载均衡器的实现类为 ZoneAvoidanceRule
return new ZoneAvoidanceRule();
}
@Bean
public IPing ribbonPing() {
// 指定负载均衡器的心跳检测实现类为 NoOpPing
return new NoOpPing();
}
}
```
这里使用 `ZoneAvoidanceRule` 作为负载均衡器的实现类,默认情况下是使用 `RoundRobinRule`。
3. 在启动类中添加注解 `@RibbonClients`
```java
@SpringBootApplication
@RibbonClients(defaultConfiguration = RibbonConfiguration.class)
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
这里使用 `@RibbonClients` 注解来指定 Ribbon 的配置类,即 `RibbonConfiguration`。
这样,在 Spring Boot 启动时,就会初始化 `DynamicServerListLoadBalancer`,从注册中心获取服务列表,并进行负载均衡。
druid-spring-boot-starter maven怎么配置
要使用 `druid-spring-boot-starter`,你需要在你的 Maven 项目中添加以下依赖:
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
```
然后,你可以在你的 `application.properties` 或 `application.yml` 文件中配置 `druid` 数据源。以下是一个示例:
```yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_name?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
username: root
password: yourpassword
type: com.alibaba.druid.pool.DruidDataSource
# 下面是 druid 配置
# 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时
initial-size: 5
# 最小连接池数量
min-idle: 5
# 最大连接池数量
max-active: 20
# 获取连接时最大等待时间,单位毫秒
max-wait: 60000
# 是否开启 PSCache
pool-prepared-statements: true
# 指定每个连接上 PSCache 的大小
max-pool-prepared-statement-per-connection-size: 20
# 打开removeAbandoned功能
remove-abandoned: true
# 180秒,也就是3分钟
remove-abandoned-timeout: 180
# 关闭abanded连接时输出错误日志
log-abandoned: true
# 监控配置
filter:
# 开启监控统计功能
stat:
enabled: true
# 是否打印 SQL 语句
log-slow-sql: true
# 慢 SQL 记录时间阈值,单位毫秒
slow-sql-millis: 5000
# 配置监控统计拦截的 URI,多个用逗号隔开
web-stat-filter:
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
# 配置 Druid 的 StatViewServlet
stat-view-servlet:
url-pattern: /druid/*
# IP 白名单
allow: 127.0.0.1
# IP 黑名单(共同存在时,deny优先于allow)
deny: 192.168.0.1
# 登录用户名
login-username: admin
# 登录密码
login-password: admin123
```
以上是一个基本的 `druid` 配置,你可以根据自己的实际需求进行调整。配置完成后,你就可以在代码中使用 `DataSource` 了,例如:
```java
@Autowired
DataSource dataSource;
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![md](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)