Spring in an AOP (xml-based manner of aspectj)

aop concept

Aspect Oriented (aspect) programming functions are not implemented by the extended source.
Here Insert Picture Description

aop underlying principle

Original mode: modifying the source code
longitudinal inheritance: inherit parent class.

Here Insert Picture Description
Lateral extraction mechanism: the underlying dynamic proxies manner.
(1) JDK agent (an interface implementation class)
(2) CGLB agent (no class interface)

Here Insert Picture Description
Here Insert Picture Description

Glossary of aop

Here Insert Picture Description
Here Insert Picture Description

The main master:

Aspect,Advice,Pointcut
Here Insert Picture Description

The spring aop operations

1. aop operation not implemented using AspectJ spring inside.
2. AspectJ itself is not part of the spring. And spring bonding operation.

Introduction aspectj

Here Insert Picture Description
Use aspectj aop There are two ways to achieve.
(1) Based on aspectj xml configuration file mode.
(2) Based on the comments aspectj way.

aop Operational Readiness

1. Package guide

Here Insert Picture Description

2. Create a spring core profile, import core constraint.

Here Insert Picture Description

Implementation

  • To enhance the class
//要增强的类
public class Book {
//对book方法增强
   public void book() {
	   System.out.println("大话西游");
   }
}
  • Enhanced class
import org.aspectj.lang.ProceedingJoinPoint;

public class MyBook {
	public void before() {
		System.out.println("前置增强");
	}
	public void after() {//不是最终增强
		System.out.println("后置增强");
	}
	//环绕通知
	public void arround(ProceedingJoinPoint joinpoint) throws Throwable {
		//方法之前
		System.out.println("方法之前。。。");
		joinpoint.proceed();//执行原方法
		System.out.println("方法之后");
	}
	//最终通知
	public void end() {
		System.out.println("最终通知");
	}
	//异常通知
}
  • Xml configuration
  • Configuration entry point using the expression:
    Here Insert Picture Description
    Here Insert Picture Description
  • Implementation steps
    Here Insert Picture Description
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 配置对象 -->
	<bean id="Book" class="com.xu.springannotion.aop.Book"></bean>
	<bean id="mybook" class="com.xu.springannotion.aop.MyBook"></bean>
	<!-- 配置切面 -->
	<aop:config>
		<!-- 配置切入点:要增强的方法 -->
		<aop:pointcut
			expression="execution(* com.xu.springannotion.aop.Book.book(..))"
			id="point1" />
		<!-- 增强(通知) -->
		<aop:aspect ref="mybook">
			<!-- 前置通知 -->
			<aop:before method="before" pointcut-ref="point1" />
			<!-- 后置通知 -->
			<aop:after-returning method="after"
				pointcut-ref="point1" />
				<!-- 这里pointcut-ref后的value是指定你增强的类 -->
			<aop:around method="arround" pointcut-ref="point1" />
			<aop:after method="end" pointcut-ref="point1" />
		</aop:aspect>
	</aop:config>
</beans>

test

import static org.junit.Assert.*;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	@org.junit.Test
	public void testAop() throws Exception {
		ApplicationContext context=new ClassPathXmlApplicationContext("com/xu/springannotion/Aop.xml");
		Book book=(Book) context.getBean("Book");
		book.book();
	}
}

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_42405666/article/details/91557867