Spring framework aop annotation summary

AOP annotation implementation

One, in the implementation class of the interface

Add @Component//The target object is loaded into the container before the implementation class

/*接口*/
public interface ICalculate
{
	int add(int a,int b);
	int sub(int a,int b);
	int multi(int a,int b);
	int div(int a,int b);
	int mod(int a,int b);
}
/*实现类*/
@Component//目标对象
public class CalculateImpl implements ICalculate
{

	public int add(int a, int b)
	{
		System.out.println("a+b");
		return a+b;
	}

	
	public int sub(int a, int b)
	{
		System.out.println("a-b");
		return a-b;
	}

	@Override
	public int multi(int a, int b)
	{
		System.out.println("a*b");
		return a*b;
	}

	@Override
	public int div(int a, int b)
	{
		if(b==0)
		{
			throw new IllegalArgumentException("除数不能为0");
		}
		System.out.println("a/b");
		return a/b;
	}

	@Override
	public int mod(int a, int b)
	{
		System.out.println("a%b");
		return a%b;
	}
}

  2. Operations in the configuration file

   Turn on AspectJ, which is a popular AOP open source framework

   <aop:aspectj-autoproxy/>

   Configure package scanning base-package: the package containing the annotation class

    <context:component-scan base-package="com.study.proxy"></context:component-scan>

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-4.0.xsd
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
  
   <aop:aspectj-autoproxy/>
  
   <!-- 配置包扫描  base-package:包含注解的类所在的包-->
   <context:component-scan base-package="com.study.proxy"></context:component-scan>
	
</beans>

 

Three, interceptor settings

  Add @Aspect before the class to define an aspect

  Add @Component//Interceptor to load into the container

  Set before method

  //Pre-notification: execute before the target method is executed, intercept the add() method

  @Before("execution (* com.study.proxy.CalculateImpl.add(..))")

  //Post notification: execute after the target method is executed, intercept the sub() method, and get the return value of the target method

  @AfterReturning(pointcut="execution (* com.study.proxy.CalculateImpl.sub(..))",returning="result")

  //Final notice: After the target method is executed, the mutli() method is intercepted and the return value cannot be obtained

@After("execution (* com.study.proxy.CalculateImpl.multi(..))")

//Surround notification: The target method can be executed before and after execution, and it can also control whether to call the target method, and modify the return value of the target method

@Around("execution (* com.study.proxy.CalculateImpl.mod(..))")

//Exception notification: enter when the target method throws an exception

@AfterThrowing(pointcut="execution (* com.study.proxy.CalculateImpl.div(..))",throwing="ex")

@Aspect
@Component//拦截器
public class Inteceptor
{
	//前置通知:目标方法执行前执行,拦截add()方法
	@Before("execution (* com.study.proxy.CalculateImpl.add(..))")
	public void deBeforeMethod()
	{
		System.out.println("前置通知在目标方法执行前执行");
	}
	//后置通知:目标方法执行后执行,拦截sub()方法,可以获取目标方法的返回值
	@AfterReturning(pointcut="execution (* com.study.proxy.CalculateImpl.sub(..))",returning="result")
	public void afterReturning(int result)
	{
		System.out.println("目标方法的返回值:"+result);
		System.out.println("后置通知 在目标方法执行 后 执行");
	}
	//最终通知:目标方法执行后执行,拦截mutli()方法,无法获取返回值
	@After("execution (* com.study.proxy.CalculateImpl.multi(..))")
	public void finalMethod()
	{
		System.out.println("最终通知 在目标方法执行 后 执行,无法获取返回值");
	}
	//环绕通知:目标方法执行前后都可以执行,还能控制是否调用目标方法,还可以修改目标方法的返回值
	@Around("execution (* com.study.proxy.CalculateImpl.mod(..))")
	public Object aroundMethod(ProceedingJoinPoint pjp) throws Throwable 
	{
		Object returnVal = null;
		Object[] params = pjp.getArgs();//获取目标方法的参数
		if(params!=null)
		{
			for (Object object : params)
			{
				System.out.println(object);
			}
		}
		System.out.println("目标方法执行前执行");
		returnVal = pjp.proceed();//调用目标对象的目标方法
		System.out.println("目标方法执行后执行");
		return (Integer)returnVal*10;//修改返回值
	}
	//异常通知:当目标方法抛出异常时进入
	@AfterThrowing(pointcut="execution (* com.study.proxy.CalculateImpl.div(..))",throwing="ex")
	public void afterThrowing(Exception ex)
	{
		System.out.println(ex);
	}
}

Four, test category

Load spring configuration file

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

Get the object of the interface

ICalculate cal = (ICalculate) context.getBean("calculateImpl");

Object call method

cal.add(10, 20)

public class ServiceTest
{
	@Test
	public void testService1() throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		ICalculate cal = (ICalculate) context.getBean("calculateImpl");
		System.out.println(cal.add(10, 20));
	}
	
	@Test
	public void testService2() throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		ICalculate cal = (ICalculate) context.getBean("calculateImpl");
		System.out.println(cal.sub(10, 20));
	}
	
	@Test
	public void testService3() throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		ICalculate cal = (ICalculate) context.getBean("calculateImpl");
		System.out.println(cal.multi(10, 20));
	}
	
	@Test
	public void testService4() throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		ICalculate cal = (ICalculate) context.getBean("calculateImpl");
		System.out.println(cal.mod(10, 20));
	}
	@Test
	public void testService5() throws Exception
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		ICalculate cal = (ICalculate) context.getBean("calculateImpl");
		System.out.println(cal.div(10, 0));
	}
}

5. I use the maven project, so I need to get the jar package. The following is the pom.xml file configuration

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.zhupeng</groupId>
  <artifactId>springaopannotation</artifactId>
  <version>0.0.1-SNAPSHOT</version>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<spring.vesion>4.3.2.RELEASE</spring.vesion>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>javax.servlet.jsp-api</artifactId>
			<version>2.3.1</version>
			<scope>provided</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.zookeeper</groupId>
			<artifactId>zookeeper</artifactId>
			<version>3.4.8</version>
			<exclusions>
				<exclusion>
					<artifactId>log4j</artifactId>
					<groupId>log4j</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
			<exclusions>
				<exclusion>
					<artifactId>spring</artifactId>
					<groupId>org.springframework</groupId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.20.0-GA</version>
		</dependency>

		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.9</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.9</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.3</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
		<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>


		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2</version>
		</dependency>

		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.vesion}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.vesion}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.vesion}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.vesion}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/org.springframework.jdbc -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.vesion}</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-instrument-tomcat</artifactId>
			<version>${spring.vesion}</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.6</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>springaopannotation</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.1</version>
				<configuration>
					<source>1.7</source>
					<target>1.7</target>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Six, finally look at the project structure

Hope to help you, I have just learned this one, and then I will update my summary! 

Guess you like

Origin blog.csdn.net/qq_35340913/article/details/86176100