AOP(面向切面)和事务管理
目录
情景1:(面向切面)
需要导入的jar包有
创建IBankDao接口,并在接口里面写入四个未实现的方法
package dao;
public interface IBankDao {
public void saveMoney();
public void sendMoney();
public void loan();
public void manageMoney();
}
新建接口实现类BankDaoImpl实现IBankDao未实现的方法
package impl;
import dao.IBankDao;
public class BankDaoImpl implements IBankDao {
@Override
public void saveMoney() {
System.out.println("存钱操作");
}
@Override
public void sendMoney() {
System.out.println("取钱操作");
}
@Override
public void loan() {
System.out.println("贷款操作");
}
@Override
public void manageMoney() {
System.out.println("理财操作");
}
}
银行的业务逻辑一般要先认证身份,所以在这里我们新建一个验证身份的类AdminCheck
package impl;
public class AdminCheck {
public void check(){
System.out.println("正在验证身份。。。。");
}
}
新建TransactionManager类
package impl;
import org.aspectj.lang.ProceedingJoinPoint;
public class TransactionManager {
public void beginTransaction(){
System.out.println("事务的开始");
}
public void commitTransaction(){
System.out.println("提交事务");
}
public void doincepter(ProceedingJoinPoint point){
try {
beginTransaction();
point.proceed();
commitTransaction();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
在新建一个写日志的类LogInfo
package impl;
public class LogInfo {
public void writerLog(){
System.out.println("正在写入日志文件");
}
}
在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!--依赖注入-->
<bean id="adminCheck" class="impl.AdminCheck"></bean>
<bean id="transactionManager" class="impl.TransactionManager"></bean>
<bean id="logInfo" class="impl.LogInfo"></bean>
<bean id="bankDao" class="impl.BankDaoImpl"></bean>
<aop:config>
<aop:pointcut id="servicepoint" expression="execution(* impl.*.*(..))"/>
<!--前端切入点-->
<aop:aspect ref="adminCheck">
<aop:before method="check" pointcut-ref="servicepoint"/>
</aop:aspect>
<!--切入的方式:环绕切入-->
<aop:aspect ref="transactionManager">
<aop:around method="doincepter" pointcut-ref="servicepoint"/>
</aop:aspect>
<!--后端切入点-->
<aop:aspect ref="logInfo">
<aop:after method="writerLog" pointcut-ref="servicepoint"/>
</aop:aspect>
</aop:config>
</beans>
新建测试类Test进行测试
package test;
import dao.IBankDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
test1();
}
private static void test1() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
IBankDao bd = ac.getBean("bankDao", IBankDao.class);
bd.sendMoney();
}
}
测试结果
情景2:(事务控制)
这里我们通过spring来连接数据库,需要导入的jar包有
接着我们需要在XML文件中配置数据源,以方便我们连接MySQL数据库进行操作
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!--配置数据源-->
<bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/school"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
</beans>
其中org.apache.commons.dbcp.BasicDataSource这个class可以在我们导入的jar目录下找到
创建数据库,新建一个表并且在表中添加几条数据
新建Class的实体类
package entity;
public class Class {
private int classid;
private String classname;
public int getClassid() {
return classid;
}
public void setClassid(int classid) {
this.classid = classid;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public Class(int classid, String classname) {
this.classid = classid;
this.classname = classname;
}
public Class() {
}
public Class(int classid) {
this.classid = classid;
}
}
新建接口IClass并在接口里面写入查询和修改的方法
package dao;
import entity.Class;
import java.util.List;
public interface IClass {
public List<Class> findAll();
public int updateClass(Class c);
}
新建ClassDaoImpl实现接口IClass
package impl;
import dao.IClass;
import entity.Class;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class ClassDaoImpl implements IClass {
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<Class> findAll() {
return jdbcTemplate.query("select * from class", new RowMapper() {
@Override
public Class mapRow(ResultSet rs, int i) throws SQLException {
Class c = new Class(rs.getInt(1),rs.getString(2));
return c;
}
});
}
@Override
public int updateClass(Class c) {
return jdbcTemplate.update("update class set classname=? where classid=?",c.getClassname(),c.getClassid());
}
}
在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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!--配置数据源-->
<bean id="dataSoure" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/school"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSoure"></property>
</bean>
<bean id="classDao" class="impl.ClassDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSoure"></property>
</bean>
<!--通知式事务-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="txpoint" expression="execution(* impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint"/>
</aop:config>
</beans>
测试