java - Proper way to handle properties files in spring code base configuration -
i started writing application wanted include spring configuration in java code can. problem encounter properties file. take have writen far:
file containing beans declaration:
@configuration @importresource("classpath:properties-configuration.xml") public class contextconfigutarion { @value("${database.url}") private string database_url; @value("${database.user}") private string database_user; @value("${database.password}") private string database_password; @value("${database.default.shema}") private string database_default_shema; @bean public basicdatasource datasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname(com.mysql.jdbc.driver.class.getname()); datasource.seturl(database_url); datasource.setusername(database_user); datasource.setpassword(database_password); return datasource; } @bean public localsessionfactorybean sessionfactory() { localsessionfactorybean sessionfactory = new localsessionfactorybean(); sessionfactory.setdatasource(datasource()); sessionfactory.sethibernateproperties(hibernateproperties); // <- here encountered problem return sessionfactory; } ... }
properties-configuration.xml minimum necessery file, used specify file properties location:
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:database.properties" /> </beans>
when started configuring sessionfactory object notice problem. understand in contextconfiguration class have subtract every property database.properties file. if application have lot of properties configuration java code redundantly growing. isn't there better way transfer properties through spring components without extracting every single 1 of them.
second related question: there way preserve application properties in tree structure? because see in above example application properties contain: data source properties, hibernate properties, etc. in fact property tree. if application whould bigger , have more components preserving properies in tree structure whould great. image have properties stored in catalogs that:
/application /hibernate filename.properties /database filename.properties /someoddcomponent /somestrangecomponent filename.properties filename.properties filename.properties
so if ask application.properties whould got sum .properties files in application directory (and sub catalogs), if ask hibernate.properties whould got sum .properties files hibernate directory (and sub catalogs), etc. maybe here exaggerate problem, think?
first ditch xml file , use @propertysource
annotation load properties file , register propertysourceplaceholderconfigurer
in configuration class.
@configuration @propertysource("classpath:database.properties") public class contextconfigutarion { @bean public static propertysourcesplaceholderconfigurer placeholderconfigurer() { return new propertysourcesplaceholderconfigurer (); } }
if have lot of properties instead of specifying @value
annotations use environment
, getproperty
, getrequiredproperty
methods instead.
@configuration @propertysource("classpath:database.properties") public class contextconfigutarion { @autowired private environment env; @bean public basicdatasource datasource() { basicdatasource datasource = new basicdatasource(); datasource.setdriverclassname(com.mysql.jdbc.driver.class.getname()); datasource.seturl(env.getrequiredproperty("database.url")); datasource.setusername(env.getrequiredproperty("database.user)); // other properties return datasource; } }
you don't want have lot of configuration files make things complex , unmaintainable. limit them number. if want tree system don't use property file use yaml file instead. although bit harder load (not supported @propertysource
) allows tree configuration structures in single file.
or better let spring boot handle complexity (that supports both properties , yaml files out of box). reduces configuration , gives nice auto config out-of-the-box.
Comments
Post a Comment