beans.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring 读取db.properties -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="userService" class="com.liuyun.aop.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userDao" class="com.liuyun.aop.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- spring-jdbc内置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClass}"></property>
<property name="url" value="${jdbc.jdbcUrl}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 通知类交给spring进行管理 -->
<bean id="myAopAdvice" class="com.liuyun.aop.utils.MyAopAdvice"></bean>
<aop:config>
<!-- 设置切入点(哪些类的哪些方法需要增强) -->
<aop:pointcut id="servicePt" expression="execution(* com.liuyun.aop.service.impl.*.*(..))" />
<!-- 使用切面将切入点和通知类进行绑定:即通知类的哪些方法在哪个位置对切入点方法进行增强-->
<aop:aspect id="advice" ref="myAopAdvice">
<aop:before method="before" pointcut-ref="servicePt" />
<aop:after-returning method="afterReturning" pointcut-ref="servicePt" />
<aop:after-throwing method="afterThrowing" pointcut-ref="servicePt" />
<aop:after method="after" pointcut-ref="servicePt" />
<aop:around method="around" pointcut-ref="servicePt" />
</aop:aspect>
</aop:config>
</beans>
该xml中的关键代码:
通知类:
package com.liuyun.aop.utils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
/**
* @author liuyun
* @since 2019年9月1日下午9:02:58
*/
@Component
public class MyAopAdvice {
public void before() {
System.out.println("...before方法");
}
public void afterReturning() {
System.out.println("...afterReturning方法");
}
public void afterThrowing() {
System.out.println("...afterThrowing方法");
}
public void after() {
System.out.println("...after方法");
}
/**
* around方式可以实现其他四种方式:before、afterReturning、afterThrowing、after
*/
public Object around(ProceedingJoinPoint pjp) throws Throwable {
Object resutlValue = null;
try {
System.out.println("around......before方法");
resutlValue = pjp.proceed();// 调用被代理的方法
System.out.println("around......afterReturning方法");
} catch (Throwable e) {
System.out.println("around......afterThrowing方法");
throw e;
} finally {
System.out.println("around......after方法");
}
return resutlValue;
}
}