java - How to property file value in Spring boot config class -


how use application.properties file in config class

application.properties

datasource.username=test

config.class

 @configuration  @enabletransactionmanagement  @enablejparepositories(     entitymanagerfactoryref = "abcfactory",      transactionmanagerref = "abcmanager",     basepackages = { "com.emp.repository" })       public class empconfig {          @value("${datasource.username}")         string username;          @bean(name = "empdatasource")               public datasource empdatasource(string url, string username, string pwd) {                  drivermanagerdatasource datasource = new drivermanagerdatasource();          datasource.setdriverclassname("xxx");          datasource.seturl(url);          datasource.setusername(username);          datasource.setpassword(pwd);                   return datasource;                  }       } 

how can pass property in username set field.

depending on how initialized app, put

@enableautoconfiguration @propertysource("classpath:application.properties") @componentscan @springbootapplication @enabletransactionmanagement 

make sure have 1 of these in configs

@bean public static propertysourcesplaceholderconfigurer propertyplaceholderconfigurer() {     return new propertysourcesplaceholderconfigurer(); } 

then can access values

@value("${datasource.username}") @notnull //optional string username; 

Comments

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -