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

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -