java - Spring 4 @Configuration order for setting up JPA -
i had working jersey 2.2 applisaction spring 4 in using eclipselink jpa implementation.
the application config calss looks follows:
@configuration @componentscan(value = "com.nws.vedica", lazyinit = true) @propertysource({"classpath:swagger.properties", "classpath:vedica.properties"}) @applicationpath("/api") public class vedicaconfig extends resourceconfig { public vedicaconfig() { packages("com.nws.vedica"); property(servletproperties.filter_forward_on_404, true); register(multipartfeature.class); register(jacksonfeature.class); register(validationfeature.class); register(validationconfigurationcontextresolver.class); register(propertysourcesplaceholderconfigurer.class); register(apilistingresource.class); register(swaggerserializers.class); } }
the jpa configuration class:
@configuration @enabletransactionmanagement public class jpaconfig { private map<string, string> properties; @value("${db.url}") private string dbconnectionurl; @value("${db.user}") private string dbuser; @value("${db.pass}") private string dbpassword; @postconstruct public void init() { properties = new hashmap<>(); properties.put("javax.persistence.jdbc.url", dbconnectionurl); properties.put("javax.persistence.jdbc.user", dbuser); properties.put("javax.persistence.jdbc.password", dbpassword); properties.put("javax.persistence.jdbc.driver", "org.postgresql.driver"); properties.put("javax.persistence.target-database", "postgresql"); properties.put("eclipselink.cache.shared.default", "true"); properties.put("eclipselink.ddl-generation", "none"); properties.put("eclipselink.logging.level.sql", "fine"); properties.put("eclipselink.logging.parameters", "true"); properties.put("eclipselink.deploy-on-startup", "true"); properties.put("eclipselink.ddl-generation.output-mode", "database"); } @bean public jpatransactionmanager jpatransman(){ jpatransactionmanager jtmanager = new jpatransactionmanager( getentitymanagerfactorybean().getobject()); return jtmanager; } @bean public localentitymanagerfactorybean getentitymanagerfactorybean() { localentitymanagerfactorybean lemfb = new localentitymanagerfactorybean(); lemfb.setjpapropertymap(properties); lemfb.setpersistenceunitname(vedantas.pu_name); lemfb.setpersistenceproviderclass(org.eclipse.persistence.jpa.persistenceprovider.class); return lemfb; } }
now, works well. on startup, application config class being loaded first "propertysourcesplaceholderconfigurer" gets registered , can use @value(...) annotations in jpa config class gets loaded second.
today have decided i'll replace eclipselink hibernate because of it's auditing abilities.
to pom.xml have added:
<dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-core</artifactid> <version>5.2.9.final</version> </dependency> <dependency> <groupid>org.hibernate.javax.persistence</groupid> <artifactid>hibernate-jpa-2.1-api</artifactid> <version>1.0.0.final</version> </dependency> <dependency> <groupid>org.hibernate</groupid> <artifactid>hibernate-envers</artifactid> <version>5.2.9.final</version> </dependency>
and have changed jpa config class to:
@configuration @enabletransactionmanagement public class jpaconfig { private map<string, string> properties; @value("${db.url}") private string dbconnectionurl; @value("${db.user}") private string dbuser; @value("${db.pass}") private string dbpassword; @postconstruct public void init() { properties = new hashmap<>(); properties.put("javax.persistence.jdbc.url", dbconnectionurl); properties.put("javax.persistence.jdbc.user", dbuser); properties.put("javax.persistence.jdbc.password", dbpassword); properties.put("javax.persistence.jdbc.driver", "org.postgresql.driver"); properties.put("javax.persistence.target-database", "postgresql"); properties.put("hibernate.hbm2ddl.auto", "create"); properties.put("hibernate.dialect", "org.hibernate.dialect.postgresqldialect"); } @bean public platformtransactionmanager transactionmanager(entitymanagerfactory emf){ jpatransactionmanager transactionmanager = new jpatransactionmanager(); transactionmanager.setentitymanagerfactory(emf); return transactionmanager; } @bean public persistenceexceptiontranslationpostprocessor exceptiontranslation(){ return new persistenceexceptiontranslationpostprocessor(); } @bean public localcontainerentitymanagerfactorybean entitymanagerfactory() { localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setpackagestoscan(new string[]{"com.nws.vedica.model"}); em.setpersistenceunitname(vedantas.pu_name); jpavendoradapter vendoradapter = new hibernatejpavendoradapter(); em.setjpavendoradapter(vendoradapter); em.setjpapropertymap(properties); return em; } }
now, surprise, load/execution order of application config , jpa config classes has swapped jpa config being loaded first , application config secondly causes "propertysourcesplaceholderconfigurer" not being registered @ jpa config class load time, @value annotations not working!
i interested in knowing why so? why has execution order swapped?
i know can trick not declaring jpa config calass @configuration , register bean in application config class like:
@bean public jpaconfig setupjpahibernate() { return new jpaconfig(); }
but still, know, happening here?
the reason order changed introduction of persistenceexceptiontranslationpostprocessor
bean jpaconfig class.
@bean public persistenceexceptiontranslationpostprocessor exceptiontranslation(){ return new persistenceexceptiontranslationpostprocessor(); }
moving vedicaconfig class solves problem me.
Comments
Post a Comment