AspectJ 使用的一些註解

人生如遊戲---發表於2018-03-10

使用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() {}
}
基本上就這些了

相關文章