spring boot - Passing environment values in application.properties -
spring boot - Passing environment values in application.properties -
i new spring-boot , trying pass backend credentials environment values application.properties.
to set environment values in tomcat , created setenv.bat , setenv.sh location: \apache-tomcat-7.0.53-windows-x64\apache-tomcat-7.0.53\bin
set username="abc" set password="xyz"
these environment values getting set , able print using
@autowired private environment env; string username = env.getproperty("username"); string pwd = env.getproperty("password");
and trying access these values in application.properties like
spring.datasource.username=${username} spring.datasource.password=${password}
but not working. tried different way , mentioned environment variables name in setenv.bat
set spring.datasource.username="abc" set spring.datasource.password="xyz"
i hoping spring boot read these values env , pass info source not have mention explicitly in application.properties not working. please note, not have bean.xml file , doing pure annotation based development. inputs here..
if understand correctly, want access variables written in application.properties file
this can done in many ways, 1 simple way next in .java file :
@propertysource("classpath:application.properties") class appconfig { @bean public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() { homecoming new propertysourcesplaceholderconfigurer(); } } @test public void test_fetch_property() { annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(appconfig.class); context.registershutdownhook(); environment environment = context.getbean(environment.class); string strwhoami = environment.getproperty("whoami.name").tostring(); assertthat(strwhoami,equalto("\"john doe\"")); }
the whoami.name property fetched application.properties file , tested
application.properties file:
#---------------------------------------------------------- # show self #---------------------------------------------------------- whoami.name="john doe"
i hope can help bit
spring-boot
Comments
Post a Comment