gpt4 book ai didi

java - 如何从系统环境中定义的外部属性文件配置 Spring Boot 数据源?

转载 作者:行者123 更新时间:2023-12-01 18:05:20 25 4
gpt4 key购买 nike

我有一个环境变量“KBL_APP_PROPS”。这个在我们本地的Windows环境和服务器的LINUX环境中都存在。该环境变量指向系统上的属性文件。

我正在尝试编写一个程序,使用指定的属性文件来配置数据库连接。我已经尝试了几种方法,下面的代码反射(reflect)了我当前的表现。

我的数据库配置类

@Configuration
public class DatabaseConfig {
@Autowired
Environment env;

@Bean
public DataSource getDataSource(@Value("dbUri") String url, @Value("dbUser") String userName, @Value("dbPassword") String password) {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("oracle.jdbc.driver.OracleDriver");
dataSourceBuilder.url(url);
dataSourceBuilder.username(userName);
dataSourceBuilder.password(password);
return dataSourceBuilder.build();
}

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(/*@Value("${KBL_APP_PROPS}") String fileName*/) {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
System.out.println(String.format("Env is null %b", env == null));
properties.setLocation(new FileSystemResource(env.getProperty("KBL_APP_PROPS")/*fileName*/));
properties.setIgnoreResourceNotFound(false);
return properties;
}

我的主课

@SpringBootApplication
public class ShortageClaimAutoAccept implements CommandLineRunner {

private static final Logger log = Logger.getLogger(ShortageClaimAutoAccept.class);

@Autowired
JdbcTemplate jdbcTemplate;

// @Autowired
// ClaimRepository claimRepository;


public ShortageClaimAutoAccept() {
}

private void startShortageClaimAutoAcceptApp() {
// This block is for accepting claim details and closing claims
{
// List<Claim> claims = claimRepository.findByUnclosedDaysSinceFiled(30);
// for(Claim claim : claims) {
// System.out.println(claim.toString());
// }
}
// This block is for emailing publishers about claims that need to be reviewed
{

}
}

public static void main(String[] args) {
try {
SpringApplication.run(ShortageClaimAutoAccept.class, args);
} catch (Exception ex) {
log.fatal("uncaught exception in the process: \n", ex);
}
}

@Override
public void run(String... arg0) throws Exception {
startShortageClaimAutoAcceptApp();
}
}

我的程序因空指针异常而退出,因为 env 为空。

最佳答案

@Ralf 的评论帮助我找到了一篇 SO 文章,其中某些事情并未调用 @Autowire。因此,我更改了 DatabaseConfig 以使用 EnvironmentAware。

@Configuration
public class DatabaseConfig implements EnvironmentAware {
Environment environment;

@Override
public void setEnvironment(final Environment environment) {
this.environment = environment;
}

@Bean
public DataSource getDataSource(@Value("${dbUri}") String url, @Value("${dbUser}") String userName, @Value("${dbPassword}") String password) {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.driverClassName("oracle.jdbc.driver.OracleDriver");
dataSourceBuilder.url(url);
dataSourceBuilder.username(userName);
dataSourceBuilder.password(password);
return dataSourceBuilder.build();
}

@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource(environment.getProperty("KBL_APP_PROPS")));
properties.setIgnoreResourceNotFound(false);
return properties;
}
}

关于java - 如何从系统环境中定义的外部属性文件配置 Spring Boot 数据源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60567291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com