java - how can i parameterize datasource properties on Spring 4? -


i using spring 4.16. want parameterize persistence data. config right now:

@configuration @enabletransactionmanagement public class persistenceconfiguration {      @bean     public localcontainerentitymanagerfactorybean entitymanagerfactory() {         localcontainerentitymanagerfactorybean entitymanager = new localcontainerentitymanagerfactorybean();         entitymanager.setdatasource(this.datasource());         entitymanager.setpackagestoscan(new string[] {"com.example.movies.domain"});         jpavendoradapter vendoradapter = new hibernatejpavendoradapter();         entitymanager.setjpavendoradapter(vendoradapter);         entitymanager.setjpaproperties(this.properties());         return entitymanager;     }  @bean public datasource datasource() {     drivermanagerdatasource datasource = new drivermanagerdatasource();     datasource.setdriverclassname("com.mysql.jdbc.driver");     datasource.seturl("jdbc:mysql://localhost:3306/sarasa_db");     datasource.setusername("root");     datasource.setpassword("mypassword");     return datasource; }      @bean     public platformtransactionmanager transactionmanager(entitymanagerfactory emf) {         jpatransactionmanager transactionmanager = new jpatransactionmanager();         transactionmanager.setentitymanagerfactory(emf);         return transactionmanager;     }      @bean     public persistenceexceptiontranslationpostprocessor exceptiontranslation() {         return new persistenceexceptiontranslationpostprocessor();     }      private properties properties() {         properties properties = new properties();         properties.setproperty("hibernate.hbm2ddl.auto", "update");         properties.setproperty("hibernate.dialect", "org.hibernate.dialect.mysql5dialect");         properties.setproperty("hibernate.show_sql", "false");         return properties;     }  } 

and want parameterize on application.properties things can. first of all, want put in datasource properties (so reading, possible spring builds datasource automatically, apparently using jdbctemplate...):

spring.datasource.driverclassname=com.mysql.jdbc.driver spring.datasource.url=jdbc:mysql://localhost:3306/sarasa_db spring.datasource.username=root spring.datasource.password=mypassword 

and, if it's possible, properties's properties, couldn't find nothing in doc

do know how ?


edit

this dao implementation

@configuration @import(persistenceconfiguration.class) public class daoconfiguration {      @persistencecontext     private entitymanager entitymanager;      @bean     public clientdao clientdao() {         simplejparepository<client, string> support = this.getsimplejparepository(client.class);         return new mysqlclientdao(support);     }      @bean     @scope(beandefinition.scope_prototype)     @description("hibernate repository helper")     protected <t> simplejparepository<t, string> getsimplejparepository(class<t> domainclass) {         return new simplejparepository<t, string>(domainclass, this.entitymanager);     }  } 

you this:

first define propertysourcesplaceholderconfigurer bean somewhere in spring configuration:

@bean public static propertysourcesplaceholderconfigurer propertyplaceholderconfigurer() {     propertysourcesplaceholderconfigurer ppc = new propertysourcesplaceholderconfigurer();     ppc.setlocation(new classpathresource("application.properties"));     return ppc; } 

this configuration assumes application.properties file placed @ root of classpath.

after setting property placeholder configurer can access properties in database configuration class so:

@configuration @enabletransactionmanagement public class persistenceconfiguration {      @value("${spring.datasource.url}")     private string jdbcurl;     // ...      @bean     public datasource datasource() {        drivermanagerdatasource datasource = new drivermanagerdatasource();        datasource.seturl(jdbcurl);        // ...     } } 

if want easy way parametrize properties, should take @ spring boot. uses application.properties file automatically create data source properties, , many other things. automatic datasource creation mentioned in question.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -