spring读取外部jdbc配置(非classpath目录上)
spring读取外部jdbc配置(非classpath目录下)
spring文件
以下为转的内容
用spring.数据库是mysql.
这个软件用户能自己配置数据库连接.
我想问下.spring中怎么读取外部的jdbc配置.(不在classpath下的,因为放到classpath下.一打包jar就会打包进去).
我连接池用的是dbcp
下面这种方式读取的是classpath下的.也就是src目录下的
我想读取的是这个项目目录下conf目录下的jdbc.properties;
你看看spring代码就明白了,locations是用来构造org.springframework.core.io.Resource的,所以所有Resource的子类都可以用,url是其中的一种。
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> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 定义JPA 结束 --> </beans>
以下为转的内容
用spring.数据库是mysql.
这个软件用户能自己配置数据库连接.
我想问下.spring中怎么读取外部的jdbc配置.(不在classpath下的,因为放到classpath下.一打包jar就会打包进去).
我连接池用的是dbcp
下面这种方式读取的是classpath下的.也就是src目录下的
我想读取的是这个项目目录下conf目录下的jdbc.properties;
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>conf/jdbc.properties</value> </list> </property> </bean> <!-- mysql数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.gjt.mm.mysql.Driver" /> <property name="url"> <value>${url}</value> </property> <property name="username"> <value>${username}</value> </property> <property name="password"> <value>${password}</value> </property> <!-- 连接池启动时的初始值 --> <property name="initialSize" value="1" /> <!-- 连接池的最大值 --> <property name="maxActive" value="500" /> <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 --> <property name="maxIdle" value="2" /> <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> <property name="minIdle" value="1" /> </bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>file:conf/jdbc.properties</value> </list> </property> </bean>
你看看spring代码就明白了,locations是用来构造org.springframework.core.io.Resource的,所以所有Resource的子类都可以用,url是其中的一种。