Spring中配置aop (xml)

只是为了方便翻阅做个一个笔记,有什么错误希望大家可以指出

1、spring中 用xml配置aop

1)目标对象

复制代码
package cn.itcast.service;

public class UserServiceImpl implements UserService {

    @Override
    public void save() {
        // TODO Auto-generated method stub
        System.out.println("保存用户!!");

    }

    @Override
    public void update() {
        // TODO Auto-generated method stub
        System.out.println("修改用户!!");

    }

    @Override
    public void delete() {
        // TODO Auto-generated method stub
        System.out.println("删除用户!!");

    }

    @Override
    public void find() {
        // TODO Auto-generated method stub
        System.out.println("更新用户!!");

    }

}
复制代码

 

2)通知对象

复制代码
package cn.itcast.advice;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {

    //前置通知
    public void before(){
        System.out.println("这是前置通知!!!");
    }
    
    
    // 后置通知
    public void afterReturning(){
        System.out.println("这是后置通知(出现异常也会调用)!!!");
    }
    
    
    //环绕通知
    public Object around(ProceedingJoinPoint pjPoint) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!!");
        Object proceed = pjPoint.proceed();  //调用目标方法
         System.out.println("这是环绕通知之后的部分!!!");
        return proceed;
    }
    
    
    //异常通知
    public void  afterException() {
        System.out.println("出现异常啦(出现异常不会调用)!!!!");
        
    }
    
    //后置通知
    public void  after() {
        System.out.println("这是后置通知(出现异常不会调用)!!!!");
        
    }
}
复制代码

 

3)spring中aop的相关配置

复制代码
<!-- 准备工作  导入aop命名空间 -->
    
    <!-- 1、配置目标对象  -->
    <bean name="userServiceTarget" class="cn.itcast.service.UserServiceImpl"></bean>
    <!-- 2、配置通知对象  -->
    <bean name="myAdvice" class="cn.itcast.advice.MyAdvice"></bean>
    <!-- 3、配置将通知织入目标对象 -->
    <aop:config>
        <!-- 配置切入点    
        expression:切点表达式    * cn.itcast.service..*ServiceImpl.*(..) 
         *(返回值类型) cn.itcast.service.*ServiceImpl(以ServiceImpl结尾的任何类).*(..) (任何方法-任何参数)
           -->
        <aop:pointcut expression="execution(* cn..itcast.service.*ServiceImpl.*(..))" id="pc"/>
        <aop:aspect ref="myAdvice">
            <!-- 指定myAdvice中的before方法作为前置通知    pointcut-ref:切点的引用  -->
            <aop:before method="before" pointcut-ref="pc"/>
            <aop:after method="afterReturning" pointcut-ref="pc"/>
        </aop:aspect>
    </aop:config>
复制代码

 

4、测试demo

复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/cn/itcast/springaop/applicationContext.xml")  //指定spring的配置文件
public class Test {
    
    //将容器中的 userService 注入到test中
    @Resource(name="userService")
    private UserService us;
    
    
    @org.junit.Test
    public void test1(){
        us.save();
    }
    
}
复制代码

 

clipboard

 

2、spring aop 的注解配置

1)目标对象

同上

 

2)通知对象

复制代码
// 通知类
@Aspect  
// 表示该类是一个通知类
public class MyAdvice {

    //前置通知
    //指定该方法是前置通知 , 并指定切入点
    @Before("execution(* cn..itcast.service.*ServiceImpl.*(..))")
    public void before(){
        System.out.println("这是前置通知!!!");
    }
    
    
    // 后置通知
    @AfterReturning("execution(* cn..itcast.service.*ServiceImpl.*(..))")
    public void afterReturning(){
        System.out.println("这是后置通知(出现异常也会调用)!!!");
    }
    
    
    //环绕通知
    @Around("execution(* cn..itcast.service.*ServiceImpl.*(..))")
    public Object around(ProceedingJoinPoint pjPoint) throws Throwable {
        System.out.println("这是环绕通知之前的部分!!!");
        Object proceed = pjPoint.proceed();  //调用目标方法
         System.out.println("这是环绕通知之后的部分!!!");
        return proceed;
    }
    
    
    //异常通知
    @AfterThrowing("execution(* cn..itcast.service.*ServiceImpl.*(..))")
    public void  afterException() {
        System.out.println("出现异常啦)!!!!");
        
    }
    
    //后置通知
    @After("execution(* cn..itcast.service.*ServiceImpl.*(..))")
    public void  after() {
        System.out.println("这是后置通知(出现异常不会调用)!!!!");
        
    }
}
复制代码

 

3)xml配置

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://www.springframework.org/schema/beans" 
xmlns:context="http://www.springframework.org/schema/context" 
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.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">

    <!-- 准备工作  导入aop命名空间 -->
    
    <!-- 1、配置目标对象  -->
    <bean name="userService" class="cn.itcast.service.UserServiceImpl"></bean>
    <!-- 2、配置通知对象  -->
    <bean name="myAdvice" class="cn.itcast.annotationaop.MyAdvice"></bean>
    
    <!-- 3、开启使用注解完成织入 -->
    <aop:aspectj-autoproxy>
    
    </aop:aspectj-autoproxy>
</beans>
复制代码

 

4)测试demo

复制代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/cn/itcast/annotationaop/applicationContext.xml")  //指定spring的配置文件
public class test1 {
    
    //将容器中的 userService 注入到test中
    @Resource(name="userService")
    private UserService us;
    
    
    @org.junit.Test
    public void test1(){
        us.save();
    }
    
}
复制代码

 

clipboard

 

posted @   青岑  阅读(393)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
历史上的今天:
2019-06-26 PLSQL导入导出数据库
2019-06-26 PLSQL导出表结构和数据的三种方式
点击右上角即可分享
微信分享提示
主题色彩