Home >Java >javaTutorial >How to configure Java Spring's annotation-based AOP
Copy A short project is enough.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置数据库操作对象 --> <bean id="dbAssit" class="com.itheima.dbassit.DBAssit"> <property name="dataSource" ref="dataSource"></property> <!-- 指定 connection 和线程绑定 --> <property name="useCurrentConnection" value="true"></property> </bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///spring_day02"></property> <property name="user" value="root"></property> <property name="password" value="1234"></property> </bean> </beans>
Account’s business layer implementation class
@Service("accountService") public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; }
Account’s persistence layer implementation class
@Repository("accountDao") public class AccountDaoImpl implements IAccountDao { @Autowired private DBAssit dbAssit ; }
<!-- 告知 spring,在创建容器时要扫描的包 --> <context:component-scan base-package="com.itheima"></context:component-scan>
Transaction Control Class
@Component("txManager") public class TransactionManager { //定义一个 DBAssit @Autowired private DBAssit dbAssit ; }
Function:
Declare the current class as an aspect class.
Transaction control class
@Component("txManager") @Aspect//表明当前类是一个切面类 public class TransactionManager { //定义一个 DBAssit @Autowired private DBAssit dbAssit ; }
Function:
Treat the current method as a pre-notification.
Attributes:
value: used to specify the entry point expression, you can also specify the entry point expression Quote.
//开启事务 @Before("execution(* com.itheima.service.impl.*.*(..))") public void beginTransaction() { try { dbAssit.getCurrentConnection().setAutoCommit(false); } catch (SQLException e) { e.printStackTrace(); } }
Function:
Think of the current method as a post-advice.
Attributes:
value: used to specify the entry point expression, you can also specify the entry point expression Quote
//提交事务 @AfterReturning("execution(* com.itheima.service.impl.*.*(..))") public void commit() { try { dbAssit.getCurrentConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } }
Function:
Consider the current method as an exception notification.
Attributes:
value: used to specify the entry point expression, you can also specify the entry point expression Quote
//回滚事务 @AfterThrowing("execution(* com.itheima.service.impl.*.*(..))") public void rollback() { try { dbAssit.getCurrentConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } }
Function:
Consider the current method as the final notification.
Attributes:
value: used to specify the entry point expression, you can also specify the entry point expression Quote
//释放资源 @After("execution(* com.itheima.service.impl.*.*(..))") public void release() { try { dbAssit.releaseConnection(); } catch (Exception e) { e.printStackTrace(); } }
<!-- 开启 spring 对注解 AOP 的支持 --> <aop:aspectj-autoproxy/>
Function:
Treat the current method as a surrounding notification.
Attributes:
value: used to specify the entry point expression, you can also specify the entry point expression Quote.
/** * 环绕通知 * @param pjp * @return */ @Around("execution(* com.itheima.service.impl.*.*(..))") public Object transactionAround(ProceedingJoinPoint pjp) { //定义返回值 Object rtValue = null; try { //获取方法执行所需的参数 Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction(); //执行方法 rtValue = pjp.proceed(args); //后置通知:提交事务 commit(); }catch(Throwable e) { //异常通知:回滚事务 rollback(); e.printStackTrace(); }finally { //最终通知:释放资源 release(); } return rtValue; }
Function:
Specify the entry point expression
Attributes:
value: The content of the specified expression
@Pointcut("execution(* com.itheima.service.impl.*.*(..))") private void pt1() {} /** * 引用方式: * 环绕通知 * @param pjp * @return */ @Around("pt1()")//注意:千万别忘了写括号 public Object transactionAround(ProceedingJoinPoint pjp) { //定义返回值 Object rtValue = null; try { //获取方法执行所需的参数 Object[] args = pjp.getArgs(); //前置通知:开启事务 beginTransaction(); //执行方法 rtValue = pjp.proceed(args); //后置通知:提交事务 commit(); }catch(Throwable e) { //异常通知:回滚事务 rollback(); e.printStackTrace(); }finally { //最终通知:释放资源 release(); } return rtValue; }
@Configuration @ComponentScan(basePackages="com.itheima") @EnableAspectJAutoProxy public class SpringConfiguration { }
The above is the detailed content of How to configure Java Spring's annotation-based AOP. For more information, please follow other related articles on the PHP Chinese website!