本项目用的还是springboot
项目,咱们直接上代码
一:首先看下nacos中需要动态获取的属性
二:把需要动态读取的配置类中的属性整理一个实体类
mport lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@ConfigurationProperties(prefix = "spring.datasource")
@Component
public class DataSourceEmpty {
private String driverClassName;
private String password;
private String url;
private String username;
}
1:注意nacos中配置的是spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver 。spring.datasource.driver-class-name用 - 横岗连接的属性。实体类中则对应驼峰即可。
2:prefix 是 spring.datasource。driverClassName 是driver-class-name。主要就是利用@ConfigurationProperties这个注解实现。
3:springcloud则是用@Refre