bootstrap.yml文件中数据库信息:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/dbname?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true
新建类文件
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Data
@Component
public class GlobalVariable {
@Value("${spring.datasource.druid.driver-class-name}")
private String driver_class_name;
@Value("${spring.datasource.druid.username}")
private String username;
@Value("${spring.datasource.druid.password}")
private String password;
@Value("${spring.datasource.druid.url}")
private String url;
}
使用:
import org.springframework.beans.factory.annotation.Autowired;
@Autowired
private GlobalVariable globalVariable;
System.out.println(globalVariable.getDriver_class_name());
System.out.println(globalVariable.getPassword());
System.out.println(globalVariable.getUrl());
System.out.println(globalVariable.getUsername());