ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring aop中的六种通知的基本使用(基于注解)

2021-01-08 15:31:23  阅读:188  来源: 互联网

标签:六种 Spring void System bean aop println public out


请先了解: 基于xml的基本使用

接口:CustomerService.java

public interface CustomerService {
    //保存
    public void customerSave();

    //查询
    public int customerFind();
}

实现类:CustomerServiceImpl.java

@Service("customerService")	//【相当于spring容器中定义:<bean id="customerService" class="com.alilaoye.···.CustomerServiceImpl"/>】
public class CustomerServiceImpl implements CustomerService {
    @Override
    public void customerSave() {
        System.out.println("客户保存...");
    }

    @Override
    public int customerFind() {
        System.out.println("客户查询...");
        return 0;
    }
}

类:ProductService.java

@Service("productService") //【相当于spring容器中定义:<bean id="productService" class="com.alilaoye.···.productService"/>】
public class ProductService {
    public void productSave() {
        System.out.println("商品保存...");

    }

    public int productFind() {
        System.out.println("商品查询...");
        return 0;
    }
}

类:AfterThrowingService.java

@Service("afterThrowing")
public class AfterThrowingService {
    public void throwing() {
        System.out.println("准备异常...");
        int num=1/0;
    }
}

类:MyAspect.java

@Component("myAspect")
@Aspect //将该类标识为切面类(这里面有方法进行增强),相当于<aop:aspect ref=”myAspect”>
public class MyAspect {
    @Before("bean(*Service)")
    public void before1(JoinPoint joinPoint){
        System.out.println("=======前置通知1=======");
    }

    //自定义切入点
    //方法名就是切入点的名字
    //相当于<aop:pointcut expression="bean(*Service)" id="myPointcut"/>
    @Pointcut("bean(customerService)")
    private void myPointcut(){}
    //自定义切入点
    //方法名就是切入点的名字
    //相当于<aop:pointcut expression="bean(*Service)" id="myPointcut2"/>
    @Pointcut("bean(productService)")
    private void myPointcut2(){}
    //前置通知
    //相当于:<aop:before method="before" pointcut-ref="myPointcut"/>
    //相当于:<aop:before method="before" pointcut-ref="myPointcut2"/>
    @Before("myPointcut()||myPointcut2()")
    public void before2(JoinPoint joinPoint){
        System.out.println("=======前置通知2=======");
    }


    //后置通知 对customerService
    @AfterReturning(value="bean(customerService)",returning="returnVal")
    public void afterReturning(JoinPoint joinPoint,Object returnVal){
        System.out.println("=======后置通知=======");
    }

    //环绕通知 对productService
    @Around("bean(productService)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("=======环绕通知=======前");
        Object object = proceedingJoinPoint.proceed();
        System.out.println("=======环绕通知=======后");
        return object;
    }

    //抛出通知
    @AfterThrowing(value="bean(afterThrowing)",throwing="ex")
    public void afterThrowing(JoinPoint joinPoint ,Throwable ex){
        System.out.println("=======抛出通知======="+"抛出的异常信息:"+ex.getMessage());
    }

    //最终通知
    //拦截所有以ing结尾的bean
    @After("bean(*ing)")
    public void after(JoinPoint joinPoint){
        System.out.println("=======最终通知=======");
    }
}

配置文件:applicationContext-annotation.xml

<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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       					   http://www.springframework.org/schema/beans/spring-beans.xsd
						   http://www.springframework.org/schema/context
						   http://www.springframework.org/schema/context/spring-context.xsd
						   http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 1.确定目标 -->
    <!-- 扫描bean组件 -->
    <context:component-scan base-package="com.alilaoye.demo13_annotation_advice"/>

    <!-- 3.配置aop的aspectj的自动代理:
			自动扫描bean组件中,含有@Aspect的bean,将其作为aop管理,开启动态代理    -->
    <aop:aspectj-autoproxy/>
</beans>

测试类:SpringTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-annotation.xml")
public class SpringTest {
    //注入要测试bean
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ProductService productService;
    @Autowired
    private AfterThrowingService afterThrowingService;

    //测试
    @Test
    public void test(){
        //基于接口
        customerService.customerSave();
        customerService.customerFind();
        //基于类的
        productService.productSave();
        productService.productFind();

        afterThrowingService.throwing();
    }
}

效果:
在这里插入图片描述

标签:六种,Spring,void,System,bean,aop,println,public,out
来源: https://blog.csdn.net/weixin_42396239/article/details/112364098

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有