jebrick
jebrick

Reputation: 339

Spring Boot ConfigurationProperties issues

I have been having issues getting my properties to load in a spring boot app. I made a very simple version and it still fails. I have been going over other questions from the site and just have not been able to figure it out.

application.properties is in main/java/resources

rim.dbuser=RIM_API_USER
rim.dbpassword=rimpassword
rim.dbconnection=jdbc:oracle:thin:@kuga.myrim.com:1515:dev179
rim.dbdriver=oracle.jdbc.driver.OracleDriver

A simple class for the properties

@ConfigurationProperties(prefix = "rim")
public class RIMProperties {

private  String driver;
private  String dbURL;
private  String user;
private  String password;

<getters and setters>

My object using the RIMproperties

@Component
public class RIMObject {

@Autowired
private RIMProperties rimProperties;

public void print(){
    System.out.println("dbuser = "+rimProperties.getUser());
    System.out.println("password = "+rimProperties.getPassword());
    System.out.println("driver = "+ rimProperties.getDriver());
    System.out.println("DBURL = "+rimProperties.getDbURL());
}


}

and my app class

@SpringBootApplication
@EnableConfigurationProperties(RIMProperties.class)
public class App {
public static void main(String[] args)
{
    ApplicationContext context = SpringApplication.run(App.class, args);

    RIMObject rimObject = context.getBean(RIMObject.class);
    rimObject.print();

    RIMProperties prop = context.getBean(RIMProperties.class);
    System.out.println(prop.getDriver());
    System.out.println(prop.getDbURL());
}
}

I get nulls on everything. Not sure why it is not picking up the properties.

Upvotes: 0

Views: 180

Answers (2)

Amit Phaltankar
Amit Phaltankar

Reputation: 3424

I assume, you have posted correct code.

As mentioned in some other answer looks like your java field names differ to the properties fields.

For more info take a look at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-typesafe-configuration-properties


Spring Boot Autoconfiguration

Moreover, as you are using spring-boot, is there a reason you are not using spring-boot's out of the box auto configuration feature?

In you example you are mapping datasource properties in your java class. You just need to use spring-boot's standard datasource fields in properties and it will automatically create a DataSource and even JdbcTemplate instance which you can simply autowire anywhere in your app.

Please have a look for more https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html#boot-features-connect-to-production-database

Upvotes: 1

QuakeCore
QuakeCore

Reputation: 1926

I think the problem is with your variable names, change them to match your props file.

private String driver; to private String dbdriver; you get the idea...

Upvotes: 2

Related Questions