java - SpringBoot Configuration with application.yml -
i have little springboot application, can execute different functions via openldap.
getuser
createuser
deleteuser
etc.
that works fine. want create application.yml, can manage different environments different credentials. read tutorials, still have understanding problems. code looks that:
usercontroller:
... protected static string serverurl = "xxxxx:90xx"; protected static string ldapbinddn = "cn=admin, xxxxx"; protected static string ldappassword = "xxxx"; ... @requestmapping(value = "/{userid:.+}",method = requestmethod.get,consumes="application/json",produces = "application/json") public userdata getuser(@pathvariable string userid) { dircontext context = connecttoldap(); //some operations... context.close(); return user; } ... // same other functions
my plan now, want specify credentials in application.yml instead of @ beginning of usercontroller (see above).
then have created application.yml in src/main/resources:
# actual environment spring: profiles.actives: development --- # dev profile spring: profiles: dev datasource: serverurl: ldaps://xxxxxx:90xx adminname: xxxx adminpassword: xxxxxx basedn: xxxxx --- # production profile spring: profiles: prod datasource: serverurl: ldaps://xxxx2:90xx adminname: xxxxx2 adminpassword: xxxxx2 basedn: xxxxxx
now need call configuration. have read in 1 tutorial (http://therealdanvega.com/blog/2017/06/26/spring-boot-configuration-using-yaml) have create class "applicationproperties" properties of .yml file.
@component @configurationproperties("datasource") public class applicationproperties { private string serverurl; private string adminname; private string adminpassword; private string basedn; // getter-/setter-methods
}
now need define variables beginning values .yml, right? went usercontroller , tried that:
private string serverurl; private string adminname; private string adminpassword; private string basedn; @autowired applicationproperties appprop; @requestmapping(value = "/{userid:.+}",method = requestmethod.get,consumes="application/json",produces = "application/json") public userdata getuser(@pathvariable string userid) { dircontext context = connecttoldap(); //some operations... context.close(); return user; } ... // same other functions private dircontext connecttoldap(){ system.out.prinln(appprop.getserverurl()); system.out.prinln(appprop.getadminname()); system.out.prinln(appprop.getadminpassword()); .... // code ldap connection }
but variable "appprop" still empty. know, here somewhere big understanding problem. don't know how call these properties .yml file.
thanks every in advance!
you can properties .yml
file creating config class:
application.yml
datasource: serverurl: ldaps://xxxxxx:90xx adminname: xxxx adminpassword: xxxxxx basedn: xxxxx
applicationconfig.java
class :
@configuration @enableconfigurationproperties @configurationproperties(prefix = "datasource") public class applicationconfig { private string serverurl; private string adminname; private string adminpassword; private string basedn; //getters setters }
then can call applicationconfig
class
@autowired public applicationconfig app; public userdata getuser(@pathvariable string userid) { dircontext context = connecttoldap(); //some operations... appprop.getserverurl(); appprop.getadminname(); context.close(); return user; }
and recommend create profile based properties spring boot picks them automatically, application-{profile}.{properties|yml}
can create application-production.yml
file , set profile adding @profile("production")
annotation in class.
Comments
Post a Comment