I have an environment variable "KBL_APP_PROPS". This exists in both our local Windows environment and the server's LINUX environment. This environment variable points to the Properties file on the system.
I am trying to write a program that uses the indicated properties file to configure the database connection. I have tried several things, and the code below reflects my current rendition.
My database config class
@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;
}
My main class
@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();
}
}
My program exits with a null pointer exception because env
is null.