java读取数据库配置xml,如何通过database.properties文件使数据库配置可配置的persistance.xml文件...

I would like to get confirmation on the query "Is there any way to make database configurations configuration through database.properties using in persistance.xml". Is the below possible?

My following configuration works absolutely fine,

net.javabeat.springdata.model.Address

net.javabeat.springdata.model.Employee

but I've many environment to deploy the application codes like Dev, QA, UAT, SAT, SystemTest etc, so I wanted to make configuration like below

database.properties:

mysql.jdbc.driver.class=com.mysql.jdbc.Driver

mysql.jdbc.url=jdbc:mysql://localhost:3306/test

mysql.jdbc.username=root

mysql.jdbc.password=root

hibernate.show_sql=true

eclipselink.logging.level=FINE

eclipselink.ddl-generation=create-tables

persistence.xml

net.javabeat.springdata.model.Address

net.javabeat.springdata.model.Employee

This would help me to solve my overhead. But I wonder how persistece.xml would load database.properties in order to get placeholders properly?

This How to externalize properties from JPAs persistence.xml? link has done it through JNDI, but I simply wanted to do it through database.properties file.

Edit-1:

Yes, I'm using spring-Context.xml file

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

=========== Read persistent.xml file from here ===========

Edit-1: I see below error now:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.javabeat.springdata.repository.EmployeeRepository net.javabeat.springdata.beans.RegistrationBean.employeeRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Cannot create inner bean '(inner bean)#5d3c9c43' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5d3c9c43': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.eclipse.persistence.jpa.PersistenceProvider@6201c010] did not return an EntityManagerFactory for name 'spring-data-jpa-krishna'

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)

at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)

... 13 more

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepository': Cannot create inner bean '(inner bean)#5d3c9c43' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5d3c9c43': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.eclipse.persistence.jpa.PersistenceProvider@6201c010] did not return an EntityManagerFactory for name 'spring-data-jpa-krishna'

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1475)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1220)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1120)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1044)

at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)

... 15 more

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5d3c9c43': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.eclipse.persistence.jpa.PersistenceProvider@6201c010] did not return an EntityManagerFactory for name 'spring-data-jpa-krishna'

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)

at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)

at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:444)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1117)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1012)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)

... 28 more

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [SpringContext.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: PersistenceProvider [org.eclipse.persistence.jpa.PersistenceProvider@6201c010] did not return an EntityManagerFactory for name 'spring-data-jpa-krishna'

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1572)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)

at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)

at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)

at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)

at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)

at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)

... 36 more

Caused by: java.lang.IllegalStateException: PersistenceProvider [org.eclipse.persistence.jpa.PersistenceProvider@6201c010] did not return an EntityManagerFactory for name 'spring-data-jpa-krishna'

at org.springframework.orm.jpa.LocalEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalEntityManagerFactoryBean.java:90)

at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)

... 43 more

I made changes in pom.xml file:

org.apache.maven.plugins

maven-compiler-plugin

3.1

${java.version}

${java.version}

src/main/resources

persistence.xml

org.hibernate.ejb.HibernatePersistence

net.javabeat.springdata.model.Address

net.javabeat.springdata.model.Employee

解决方案

When you're using Spring, you could choose to not use persistence.xml at all and configure the EntityManagerFactory through a FactoryBean named LocalContainerEntityManagerFactoryBean. This answer can give you an idea on how to configure a LocalContainerEntityManagerFactoryBean.

If you insist to use persistence.xml, you can add the ${...} placeholders in your persistence.xml and use, say, Maven's Resource Filtering to populate the actual values. With this approach, you should define the Property Values as the Maven properties and you won't need a separate database.properties. In this approach, you should add the properties as maven properties, something like following:

com.mysql.jdbc.Driver

jdbc:mysql://localhost:3306/test

root

root

true

FINE

create-tables

And by adding these to your build section in pom.xml:

src/main/resources

Every placeholder with ${...} syntax in your resources in src/main/resources will be replaced with their actual value, i.e. those are defined in properties section in pom.xml.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值