示例application.properties
server.port=8888
spring.datasource.url=jdbc:mysql://localhost:3306/LJingDev
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
testconfig.name=dev
testconfig.age=17
或者使用application.yml
server:
port: 8888
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/LJingDev
username: root
password: 123456
testconfig:
name: dev
age: 18
在需要的变量上添加@Value
注解 + SpEL
注入配置中的值
@Component
public class Testconfig {
@Value("${testconfig.name}")
public String name;
@Value("${testconfig.age}")
public int age;
}
原文:https://www.cnblogs.com/LJingDev/p/12709103.html