2009-12-02 32 views
28

có cách nào để tham chiếu tệp .properties trong tệp spring-context.xml và tệp JPA persistence.xml không?tải .properties trong spring-context.xml và persistence.xml

Tôi nghĩ rằng tôi đã từng thấy một ví dụ về điều này trong các tệp ngữ cảnh mùa xuân, mặc dù tôi không thể nhớ nó ở đâu. Có lẽ ai đó biết điều này? Về persistence.xml Tôi thực sự không chắc chắn nếu điều này làm việc ở tất cả.

Mục tiêu của tôi là thay đổi một số thuộc tính giữa cấu hình phát triển và phân phối. Ý tưởng tôi hiện có là thay thế tất cả các thuộc tính theo cách thủ công trong các tệp thông qua kiến ​​từ một cấu hình mẫu. Mặc dù có một cách tốt hơn để làm điều này. :)

Trả lời

16

Bạn có thể tham chiếu tệp thuộc tính bên ngoài từ tệp định nghĩa bean mùa xuân bằng cách sử dụng PropertyPlaceholderConfigurer. Tôi không nghĩ rằng nó sẽ làm việc cho một persistence.xml JPA, mặc dù hỗ trợ JPA của Spring cho phép bạn kết hợp hầu hết, nếu không phải tất cả, nội dung của persistence.xml vào chính tệp bean, trong trường hợp này nó sẽ hoạt động tốt.

+1

Cảm ơn, cho giải pháp của tôi bây giờ tôi đã sử dụng PropertyPlaceholderConfigurer trong tệp context.xml với một jdbc.properties cho nguồn dữ liệu. Tôi đã chuyển các công cụ ngủ đông ra khỏi cả hai tệp thành một hibernate.properties. Hai tệp đó được thay thế bằng các phiên bản khác nhau để phân phối và phát triển. Các hibernate.properties dường như được phát hiện đúng lúc chạy, mặc dù nhiệm vụ kiến ​​hibernatetool phải được cho biết nơi tệp là, ngay cả khi nó nằm trong classpath. Vì vậy, tất cả hiện đang hoạt động. :) – subes

38

Thay vì sử dụng công trình xây dựng của bạn để tạo phiên bản sản phẩm hoặc tệp persistence.xml của bạn, chỉ cần di chuyển tất cả các cài đặt thuộc tính đến nội dung mùa xuân của bạn.

persistence.xml tôi là

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
    version="1.0"> 
    <persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL"> 
    </persistence-unit> 
</persistence> 

Trong nội dung mùa xuân của tôi, tôi sau đó sử dụng PropertyPlaceholderConfigurer để đọc dev/giá trị tài sản sản và thiết lập các thành đậu entityManagerFactory

<?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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 

    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 
     <property name="ignoreResourceNotFound" value="true"/> 
     <property name="locations"> 
      <list> 
       <value>classpath:dev.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${datasource.driverClassName}"/> 
     <property name="url" value="${datasource.url}"/> 
     <property name="username" value="${datasource.username}"/> 
     <property name="password" value="${datasource.password}"/> 
    </bean> 

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

     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect"/> 
       <property name="showSql" value="true" /> 
       <property name="generateDdl" value="true"/> 
      </bean> 
     </property> 
     <property name="jpaProperties"> 
       <!-- set extra properties here, e.g. for Hibernate: --> 
      <props> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/> 
</beans> 
+2

+1 Đã chính xác những gì tôi đang tìm kiếm. Cảm ơn! – andyb

+2

Người dùng có thể muốn thực hiện một nghiên cứu nhỏ trước khi sử dụng ví dụ này. Từ DriverManagerDataSource JavaDoc: "CHÚ Ý: Lớp này không phải là một kết nối thực sự, nó không thực sự kết nối với nhau. Nó chỉ là sự thay thế đơn giản cho một hồ bơi kết nối toàn diện, thực hiện cùng một giao diện chuẩn, nhưng tạo các kết nối mới trên mọi gọi điện." – spaaarky21

Các vấn đề liên quan