1. 程式人生 > >spring中jdbc.properties用法

spring中jdbc.properties用法

jdbc.properties程式碼:

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@10.10.1.1:1521:ORCL
jdbc.username=test
jdbc.password=test

applicationContext.xml程式碼:

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

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

    <bean id="dataSource"
    	class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName"
    		value="${jdbc.driverClassName}">
    	</property>
    	<property name="url"
    		value="${jdbc.url}">
    	</property>
    	<property name="username" value="${jdbc.username}"></property>
    	<property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean id="sessionFactory"
    	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    	<property name="dataSource">
    		<ref bean="dataSource" />
    	</property>
    	<property name="hibernateProperties">
    		<props>
    			<prop key="hibernate.dialect">
    				org.hibernate.dialect.OracleDialect
    			</prop>
    		</props>
    	</property>
    	<property name="mappingResources">
    		<list>
    			<value>com/xy6/form/LoginForm.hbm.xml</value>
    		</list>
    	</property>
    </bean>
	<!-- 指定事務管理器類,將sessionFactory注入,讓該事務管理器具有開啟和關閉事務的能力 -->
    <bean id="transactionManager"
    	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
	<!-- 為事務管理器類指定匹配器,通過用name指定的匹配字串進行對對應的方法進行開啟和關閉事務 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
           <tx:method name="save*" propagation="REQUIRED" />
           <tx:method name="delete*" propagation="REQUIRED" />
           <tx:method name="modify*" propagation="REQUIRED" />
           <tx:method name="deploy*" propagation="REQUIRED" />
           <tx:method name="*" read-only="true" />
       </tx:attributes>
    </tx:advice>

    <!-- 為事務管理器類指定進行匹配的範圍,到指定的地方通過匹配器字串進行篩選,對應上後為該方法開啟和關閉事務 -->
    <aop:config proxy-target-class="true">
       <aop:pointcut id="managerOperation" expression="execution(* com.xy6.dao.*.*(..))" />
       <aop:advisor advice-ref="txAdvice" pointcut-ref="managerOperation" />
    </aop:config>

 	 <!-- 該 BeanPostProcessor 將自動對標註 @Autowired 的 Bean 進行注入 -->
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

	<!-- 使用annotation 自動註冊bean,並檢查@Required,@Autowired的屬性已被注入 -->
    <context:component-scan base-package="com.xy6"/>

	<!-- spring轉發 -->
    <bean name="/login" class="com.xy6.action.LoginAction">
    </bean>

    <bean id="loginAction" class="com.xy6.action.LoginAction"></bean>
    <bean id="loginService" class="com.xy6.service.impl.LoginServiceImpl"></bean>
    <bean id="loginDAO" class="com.xy6.dao.impl.LoginDaoImpl">
    	<property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
</beans>