转帐-工厂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);
		
		
		
		
	}

}
相关文章
相关标签/搜索