java - Cannot access properties -
i got spring mvc application. runs on tomcat 7 server.
i want make props.properties file, app access properties during beans initialization process.
so did following:
1.create context-parameter web.xml
<context-param> <param-name>mainprops</param-name> <param-value>${catalina.home}/conf/props.properties</param-value> </context-param> 2. created maincobfig class
@configuration @propertysource("classpath:/racoonsoft/wish/properties/props.properties") @import({webconfig.class }) public class mainconfig { @autowired environment env; @value("${db.host}") static string dbhost; @value("${db.name}") static string dbname; @value("${db.login}") static string dblogin; @value("${db.password}") static string dbpassword; @value("${ozon.login}") static string ozonlogin; @value("${ozon.password}") static string ozonpassword; @value("${ozon.apiurl}") static string ozonapiurl; @value("${payture.apihost}") static string paytureapihost; @value("${payture.merchant}") static string payturemerchant; @bean public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() { return new propertysourcesplaceholderconfigurer(); } @bean public scheduledannotationbeanpostprocessor scheduledannotationbeanpostprocessor() { return new scheduledannotationbeanpostprocessor(); } @bean public ozonprocessor apiprocessor() { return new ozonprocessor(ozonlogin, ozonpassword, ozonapiurl); } @bean public pgsqldatasource pgsqldatasource() throws exception{ pgsqldatasource result = new pgsqldatasource(dbhost,dbname,5432,dblogin,dbpassword,"org.postgresql.driver","jdbc:postgresql:"); result.loadsettings(); if(facebookcontroller.dbproc==null) { facebookcontroller.dbproc = result; } //facebookcontroller.dbproc = result; return result; } @bean public paytureprocessor paytureprocessor() { paytureprocessor proc = new paytureprocessor(paytureapihost,payturemerchant); return proc; } } 3 - created props.properties file , put /conf directory
when start application didnt throw exception (file not found) - beleave sees properties file. during bean initialization fields (dbhost,dblogin etc.) still null`s.
how can put values properties file fields?
help me please.
the annotated factory method of propertysourcesplaceholderconfigurer must static method:
@bean public static propertysourcesplaceholderconfigurer propertysourcesplaceholderconfigurer() { return new propertysourcesplaceholderconfigurer(); } spring api reference manual of @bean remarks this.
a little bit more detailed explanation:
this because propertysourcesplaceholderconfigurer beanfactorypostprocessor (bfpp). bfpp post-processing on bean factory before other (normal) beans being instantiated , intialized. so, bfpp's needed created in order work, before mainconfig bean instantiated. marking factory method static method, can invoke method without instantiating mainconfig.
Comments
Post a Comment