0

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.

bcr666
  • 2,157
  • 1
  • 12
  • 23
  • Your configuration class appears not to be considered for autowiring at all. Otherwise the environment would be assigned or an exception thrown about no suitable bean being found. – Ralf Mar 06 '20 at 16:02
  • Thanks, that helped me find https://stackoverflow.com/questions/19421092/autowired-environment-is-null – bcr666 Mar 06 '20 at 17:20

1 Answers1

0

The comment from @Ralf helped me find a SO article where the @Autowire isn't being called for some things. So I changed my DatabaseConfig to use 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;
    }
}
bcr666
  • 2,157
  • 1
  • 12
  • 23