Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 读取persistence.xml文件中的PropertyPlaceHolderConfigure_Java_Spring_Spring Mvc_Persistence - Fatal编程技术网

Java 读取persistence.xml文件中的PropertyPlaceHolderConfigure

Java 读取persistence.xml文件中的PropertyPlaceHolderConfigure,java,spring,spring-mvc,persistence,Java,Spring,Spring Mvc,Persistence,我知道我们可以在SpringXML文件中使用spring的PropertyPlaceHolderConfigurebean,它读取指定的属性文件并使用xml文件中的值。像wise一样,我们可以在我的persistence.xml文件中使用这种机制 我可以在数据源bean中使用org.eclipse.persistence.jpa.PersistenceProvider吗,就像在SpringXML文件中一样? <bean id="dataSource" class="org.

我知道我们可以在SpringXML文件中使用spring的PropertyPlaceHolderConfigurebean,它读取指定的属性文件并使用xml文件中的值。像wise一样,我们可以在我的persistence.xml文件中使用这种机制

我可以在数据源bean中使用org.eclipse.persistence.jpa.PersistenceProvider吗,就像在SpringXML文件中一样?

    <bean id="dataSource"
    class="org.eclipse.persistence.jpa.PersistenceProvider">
    <property name="javax.persistence.jdbc.driver" value="${datasource.driverClassName}" />
    <property name="javax.persistence.jdbc.url" value="${datasource.url}" />
    <property name="javax.persistence.jdbc.user" value="${datasource.username}" />
    <property name="javax.persistence.jdbc.password" value="${datasource.password}" />
</bean>

<bean id="entityManager"
    class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/>
    <property name="persistenceUnitName" value="JPAService"/>
    <property name="dataSource" ref="dataSource"/>

</bean>


提前感谢。

不要使用构建来创建persistence.xml的prod或dev版本,只需将所有属性设置移动到spring内容中即可


阅读emeraldjava的原始文章,而不是使用构建来创建persistence.xml的prod或dev版本,只需将所有属性设置移动到spring内容中即可


阅读emeraldjava的原始帖子

正如我在评论中所说,第一部分不可能,请检查此项

关于第二部分:是的,那会有用的。不过,我们使用一个单独的datasource.xml文件,并将其导入到应用程序上下文中以实现更好的模块化

spring-context.xml:

<import resource="classpath:datasouce.xml" />

datasource.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd  
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">

    <bean id="myDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="..." />
        <property name="password" value="..." />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myTestDB" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDatasource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="showSql" value="true" />
               <property name="generateDdl" value="true" />
               <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>

正如我在评论中所说,第一部分不可能,请检查此项

关于第二部分:是的,那会有用的。不过,我们使用一个单独的datasource.xml文件,并将其导入到应用程序上下文中以实现更好的模块化

spring-context.xml:

<import resource="classpath:datasouce.xml" />

datasource.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd  
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">

    <bean id="myDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="..." />
        <property name="password" value="..." />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myTestDB" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDatasource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="showSql" value="true" />
               <property name="generateDdl" value="true" />
               <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>


查看此[SO QUOTE][1][1]:@Pete:请检查发布的aove代码?查看此[SO QUOTE][1][1]:@Pete:请检查发布的aove代码?请参阅sprng doc@Hemanth:No use iam get down异常:
原因:java.lang.IllegalStateException:无法将[java.lang.String]类型的值转换为所需类型[org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager]属性“PersistenceUnitManager”:未找到匹配的编辑器或转换策略[org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager]对于属性“persistenceUnitManager”:使用代码中的第二种方法未找到匹配的编辑器或转换策略
,我得到以下异常,原因是:java.lang.IllegalStateException:在未指定LoadTimeWeaver的情况下无法应用类转换器。我的xml文件声明为``尝试添加此项:
我也这么做了,现在我看到以下内容:
原因:java.lang.IllegalStateException:必须从java代理开始才能使用InstrumentationLoadTimeWeaver。请参阅Spring文档。
Hi-Pete,通过在类下面添加它可以工作
class="org.springframework.instrument.classloading.simpleLoadTimeWeaver
但loadTimeWeaverHm…不能说太多,你必须用谷歌搜索它;)很高兴知道它是有效的。作为参考,我编辑了我的文章,包括我们完整的
datasource.xml
,我们将其导入
spring context.xml
中使用代码中的第二种方法,我得到以下异常
原因:java.lang.IllegalStateException:在未指定LoadTimeWeaver的情况下无法应用类转换器
。我的xml文件声明为``尝试添加此:
我也这样做了,现在我得到以下异常:
原因:java.lang.IllegalStateException:必须从Java代理开始才能使用InstrumentationLoadTimeWeaver。请参阅Spring文档。
Hi-Pete,通过在类下面添加它可以工作
class=“org.springframework.instrument.classloading.simpleLoadTimeWeaver
但loadTimeWeaverHm…不能说太多,你必须用谷歌搜索它;)很高兴知道它能工作。作为参考,我编辑了我的文章,并将完整的
datasource.xml
导入到
spring context.xml
中。