轉帳-工廠bean生成代理:半自動

1.dao層接口+實現類;java

2.service層接口+實現類;mysql

3.applicationContext.xml配置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"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- 還有好多約束沒有添加上面 -->

<!-- dataSource 與數據庫相連 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
	<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/wangyongwu_01"></property>
	<property name="user" value="root"></property>
	<property name="password" value="root"></property>
</bean>

<bean id="accountDaoid" class="com.heima.account01.AccountDaoImp">
	<property name="database" ref="database"></property>
</bean>

<bean id="accountServiceid" class="com.heima.account01.AccountServiceImp">
	<property name="accountDaoid" ref="accountDaoid"></property>
</bean>
<!-- service代理類 -->
<bean id="proxyAccountService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
	<property name="proxyInterfaces" ref="com.heima.spring.代理半自動.AccountService"></property>
	<property name="target" ref="accountService"></property>
	<property name="transactionManager" ref="txManager"></property>
	<property name="transactionAttributes" >
		<props>
			<prop key="transfer"> PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
		</props>
	</property>
</bean>
<!-- 配置事務管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"></property>
</bean>


</beans>

4.測試類sql

public class Test {
	public static void main(String[] args) {
		
		String xmlPath="/spring/src/com/heima/spring/代理半自動/applicationContext.xml";
		ApplicationContext applicationContext = new ClassPahtXmlApplicationContext(xmlPath);
		
		AccountService accountService =(AccountService)applicationContext.getBean("proxyAccountService");
		accountService.transfer("zhangsan", "lisi", 200);
		
		
		
		
	}

}
相關文章
相關標籤/搜索