Spring AOP_Annotation

AOP configuration using annotations

1. Import package jiar

Here Insert Picture Description
Here Insert Picture Description

2. xml configuration file

Add beans xmlns:aop="http://www.springframework.org/schema/aop"
xsi: schemaLocation added inhttp://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>

Achieve implantation or scanned item weaving
<context:component-scan base-package="com.login"></context:component-scan>
by arranging weaving section @Aspectj
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

NOTE Using AOP

Weaving point syntax, then P Usage:
execution(public * com.login.dao..*.*(..))
1. @ Aspect: section, on the need to dynamic proxy class
2. @Before: before performing the proxy object execution method.
3. @AfterThrowing: The method is executed when an abnormality acquired proxy object
4. @Around: may be added before or after the agent object methods.

@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...");
	}
Published 47 original articles · won praise 5 · Views 2017

Guess you like

Origin blog.csdn.net/OVO_LQ_Start/article/details/104362765