Spring AOP_Annotation

使用AOP注解配置

1.导入jiar包

在这里插入图片描述
在这里插入图片描述

2.配置xml文件

beans添加xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation中添加http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

<?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"
       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">

	
    <context:annotation-config/>
    <context:component-scan base-package="com.login"></context:component-scan>
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

扫描项目实现注入或者织入
<context:component-scan base-package="com.login"></context:component-scan>
通过配置织入@Aspectj切面
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

AOP注解使用

织入点语法,普便用法:
execution(public * com.login.dao..*.*(..))
1.@Aspect :切面,放在需要动态代理类上
2. @Before :在被代理对象执行方法前执行。
3. @AfterThrowing:被代理对象获取异常时执行的方法
4. @Around:可以在被代理对象前后添加方法。

@Aspect
@Component
public class LogInterceptor {
	@Before("execution(public * com.login.dao..*.*(..))")
	public void beforeMethod(){
		
		System.out.println("save before...");
	}
	
	@AfterThrowing("execution(public * com.login.dao.*.*(..))")
	public void afterThrow(){
		System.out.println("found exception..");
	}
	
	@Around("execution(public * com.login.dao.*.*(..))")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("save start...");
		pjp.proceed();
		System.out.println("save end...");
	}

	@AfterReturning("execution(public * com.login.dao.*.*(..))")
	public void afterMethod(){
		System.out.println("save afterRetrunning...");
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值