基于XML配置文件
接口和接口实现类
public interface CustomerDao {
public void save();
public void update();
}
public class CustomerDaoImpl implements CustomerDao {
@Override
public void save() {
System.out.println("Demo03:保存用户信息...");
}
@Override
public void update() {
System.out.println("Demo03:更新用户信息...");
}
}
编写切面类
//切面类=切入点+通知
public class MyAspectXml {
public void log(){
System.out.println("Demo03:记录日志...");
}
public void after(){
System.out.println("最终通知...");
}
public void afterReturning(){
System.out.println("后置通知...");
}
public void around(ProceedingJoinPoint joinPoint){
System.out.println("环绕通知1...");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("环绕通知2...");
}
}
配置XML文件
-
配置用户Dao
<bean id="customerDao" class="wangfei910.Demo03.CustomerDaoImpl"/> -
配置切面类
<bean id="myAspectXml" class="wangfei910.Demo03.MyAspectXml"/> -
配置AOP
<aop:config> <!-- ref:引入切面类 --> <aop:aspect ref="myAspectXml"> <!-- 配置的前置通知 --> <aop:before method="log" pointcut="execution(public void wangfei910.Demo03.CustomerDaoImpl.save())"/> <!-- 最终通知 --> <aop:after method="after" pointcut="execution(* *..*.*DaoImpl.*save(..))"/> <!-- 后置通知 --> <aop:after-returning method="afterReturning" pointcut="execution(* *..*.*DaoImpl.*save(..))"/> <!-- 环绕通知 --> <aop:around method="around" pointcut="execution(* *..*.*DaoImpl.*save(..))"/> </aop:aspect> </aop:config>
编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo03Test {
@Resource(name = "customerDao")
private CustomerDao customerDao;
@Test
public void run1() {
customerDao.save();
customerDao.update();
}
}
基于注解
编写接口和实现类
public interface StudentDao {
public void save();
public void update();
}
public class StudentDaoImpl implements StudentDao {
@Override
public void save() {
System.out.println("Demo04:保存用户信息....");
}
@Override
public void update() {
System.out.println("Demo04:更新用户信息....");
}
}
编写注解方式的切面类
@Auther注解表示当前类为切面类- 编写单个前置通知可以直接使用
@Before(切入点表达式) - 对于多个通知的编写可以先自定义切入点,再引入切入点
@Aspect
public class MyAspectAnnotation {
//单个通知、前置通知
@Before(value = "execution(public void wangfei910.Demo04.StudentDaoImpl.save(..))")
public void log(){
System.out.println("Demo04:记录日志...");
}
//自定义切入点
@Pointcut(value = "execution(public void wangfei910.Demo04.StudentDaoImpl.save(..))")
public void function(){}
//引入切入点
@After(value = "MyAspectAnnotation.function()")
public void log1(){
System.out.println("自定义切入点");
}
}
配置XML文件
-
开启自动代理
<aop:aspectj-autoproxy/> -
配置目标对象Dao
<bean id="studentDao" class="wangfei910.Demo04.StudentDaoImpl"/> -
配置切面类:AOP由注解配置实现
<bean id="myAspectAnnotation" class="wangfei910.Demo04.MyAspectAnnotation"/>
编写测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo04Test {
@Resource(name = "studentDao")
private StudentDao studentDao;
@Test
public void run1(){
studentDao.save();
studentDao.update();
}
}
下一篇:Spring学习笔记[6]之事务概述 https://blog.youkuaiyun.com/Rabbit_Judy/article/details/81842664
本文介绍Spring框架中面向切面编程(AOP)的应用方法,包括基于XML配置和注解的方式实现切面逻辑,通过实例展示了如何定义切面类、配置切入点及各种类型的通知。
486

被折叠的 条评论
为什么被折叠?



