1.您仍然可以使用Tomcat數據源配置嗎?如果是這樣,它如何知道使用哪個配置文件?
您可以使用Tomcat datasourse配置,但是數據源定義與Spring或Spring配置文件無關。如果您使用的是JNDI方法,那麼您可能會定義多個數據源,並可以通過配置文件屬性指定在您的應用程序中使用的數據源,但實際定義與Spring無關。或者,您可以使用非JNDI數據源,即在Spring中配置並可能使用配置文件。
2.如何爲多個環境配置persistence.xml?如果jdbc連接道具甚至在persistence.xml中
不,你只需要一個最小的persistence.xml使用Spring或者根本不需要。
3。如何設置活動配置文件而不重新編譯?
不能
以下是所有你需要起牀和與非JNDI數據源運行。
實施例彈簧配置(非JNDI):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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-3.2.xsd">
<jpa:repositories base-package="uk.co.certait.spring.data.repository" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="jpaVendorAdapter" ref="jpaAdapter"></property>
<property name="persistenceUnitName" value="persistenceUnit" />
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show.sql}</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.cache.use_second_level_cache">${hibernate.enable.cache}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
</props>
</property>
</bean>
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
<bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
</beans>
實施例彈簧的數據源定義
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="url">
<value>${database.url}</value>
</property>
<property name="username">
<value>${database.username}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
</beans>
示例性彈簧配置文件定義:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<beans profile="default">
<bean id="applicationPropertiesPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:profiles/hsqldb.profile.properties</value>
</list>
</property>
</bean>
</beans>
<beans profile="hsqldb">
<bean id="applicationPropertiesPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:profiles/hsqldb.profile.properties</value>
</list>
</property>
</bean>
</beans>
<beans profile="mysql">
<bean id="applicationPropertiesPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:profiles/mysql.profile.properties</value>
</list>
</property>
</bean>
</beans>
<beans profile="mssql">
<bean id="applicationPropertiesPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:profiles/mssql.profile.properties</value>
</list>
</property>
</bean>
</beans>
</beans>
個
最小Persisteance.XML
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
<property name="hibernate.connection.charSet" value="UTF-8" />
</properties>
</persistence-unit>
</persistence>
示例配置文件屬性文件:
#MySQL
#database.url=jdbc:mysql://localhost:3306/test
database.url=jdbc:log4jdbc:mysql://localhost:3306/test
#database.driver=com.mysql.jdbc.Driver
database.driver=net.sf.log4jdbc.DriverSpy
database.username=root
database.password=password
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.show.sql=false
hibernate.ddl.auto=create
hibernate.enable.cache=false
感謝您的答覆和代碼示例!關於JNDI與Spring Datasource相關的問題,是否有任何折衷或退步,以使用非JNDI使用JNDI? – greyfox 2014-09-29 15:36:01