日期:2014-05-16  浏览次数:20683 次

spring读取外部jdbc配置(非classpath目录下)
spring文件

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

	<!-- 引入其他sping配置文件 <import resource="admincp-beans/beans-service.xml"/> 
		<import resource="admincp-beans/beans-action.xml"/> -->

	<import resource="admincp-beans/beans-dao.xml" />

	<!-- 定义属性配置文件 开始 -->

	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<value>file:WebContent/WEB-INF/config/jdbc.properties</value>
			</list>
		</property>
	</bean>



	<!-- 定义属性配置文件 结束 -->

	<!-- 定义数据源 开始 -->
	<!-- 本dataSource仅用于开发,生产环境使用JNDI -->
	<bean id="dataSource" class="org.springframework.7e.datasource.DriverManagerDataSource"> 

		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>
	<!--
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
		<property name="url" value="jdbc:mysql://localhost:3306/test" /> 
		<property name="username" value="root" />
		 <property name="password" value="123" /> 
	</bean> 
		 -->
		 <!-- jdbcTemplate -->
	
	
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource" />
	</bean>



	<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
		<property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property 
		name="url" value="jdbc:mysql://113.31.18.21:3306/7etest" /> <property name="username" 
		value="abc" /> <property name="password" value="abc" /> </bean> -->
	<!-- 本dataSource用于生产环境 -->
	<!--<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
		<property name="jndiName"> <value>${jdbc.jndi}</value> </property> </bean> -->
	<!-- 定义数据源 结束 -->

	<!-- 定义JPA 开始 -->
	<bean
		class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

	<bean id="entityManagerFactory"
		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="jpaVendorAdapter">
			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
				<property name="database" value="MYSQL" />
				<property name="showSql" value="true" />
			</bean>
		</property>
		<property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence" />
		<property name="jpaProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.max_fetch_depth">3</prop>
			</props>
		</property>
	</bean>



	&