AspectJ 使用的一些注解

使用AspectJ时需要导入aspectjrt-1.8.13.jar和aspectjweaver-1.8.13.jar这两个jar包。

Annotation方式使用AOP

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"
    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"/>
    <aop:aspectj-autoproxy/>
	

</beans>

切入类定义

@Aspect  //定义这个类为切入类
@Component
public class LoginInterceptorAnnotation {
	@Before("doPoint()")
	public void doQian(JoinPoint joinPoint) {
		System.out.println("方法开始前我执行了!");
	}
	
	@After("execution(* com.samrtian.service.*.*(..))")
	public void doHou() {
		System.out.println("方法开始后我执行了!");
	}
	
	@Around("doPoint()")  
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("前");
		Object retVal = pjp.proceed();
		System.out.println("后");
        return retVal;
	}
	
	//定义切入哪些方法
	@Pointcut("execution(* com.samrtian.service.*.*(..))")
	public void doPoint() {}
}

@Aspect 定义类为切入类

@Pointcut 声明一个切入策略供 @Before @After @ 选择

@Before 被切入方法执行前执行

@After 被切入方法执行后执行

@Around 被切入方法前后都可以加入一些逻辑

@AfterReturning 被切入方法返回时执行

JoinPoint 加入这个参数可以获取被切入方法的名称和参数

XML方式使用AOP

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"
    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"/>
<!-- 	<aop:aspectj-autoproxy/> -->

	<bean id="loginInterceptorAnnotation" class="com.samrtian.aop.LoginInterceptorAnnotation"/>
	<aop:config>
		<aop:aspect id="logInterceptor" ref="loginInterceptorAnnotation">
			<aop:around method="doAround" pointcut="execution(* com.samrtian.service.*.*(..))"/>
		</aop:aspect>
	</aop:config>

</beans>

代码

public class LoginInterceptorAnnotation {
//	@Before("doPoint()")
	public void doQian(JoinPoint joinPoint) {
		System.out.println("方法开始前我执行了!");
	}
	
//	@After("execution(* com.samrtian.service.*.*(..))")
	public void doHou() {
		System.out.println("方法开始后我执行了!");
	}
	
//	@Around("doPoint()")  
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("前");
		Object retVal = pjp.proceed();
		System.out.println("后");
        return retVal;
	}
	
	//定义切入哪些方法
	@Pointcut("execution(* com.samrtian.service.*.*(..))")
	public void doPoint() {}
}
基本上就这些了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值