需要用到的配置文件
bean.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:util="http://www.springframework.org/schema/util"
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/util http://www.springframework.org/schema/util/spring-util.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"
default-lazy-init="true">
<!--
Aop的注意事项
切面类和目标类都需要交给ioc容器来管理
在spring的配置文件中设置<aop:aspectj-autoproxy/>
切面类不必须通过@Aspect注解表示为一个切面类
-->
<context:component-scan base-package="com.xiaonan.srping"></context:component-scan>
<!--开启基于注解的aop -->
<aop:aspectj-autoproxy/>
</beans>
maven中jar包
<dependencies>
<!-- Spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.3.1</version>
</dependency>
<!-- Spring dao依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- Spring test依赖:方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
创建一个接口文件 Calculator
public interface Calculator {
int add(int i,int j);
int sub(int i,int j);
int mul(int i,int j);
int div(int i,int j);
}
实现接口文件 CalculatorImpl
import org.springframework.stereotype.Component;
@Component
public class CalculatorImpl implements Calculator{
public int add(int i, int j) {
int result=i+j;
System.out.println("方法内部reslut"+result);
return result;
}
public int sub(int i, int j) {
int result=i-j;
System.out.println("方法内部reslut"+result);
return result;
}
public int mul(int i, int j) {
int result=i*j;
System.out.println("方法内部reslut"+result);
return result;
}
public int div(int i, int j) {
int result=i/j;
System.out.println("方法内部reslut"+result);
return result;
}
}
通知类 LoggerAspect
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Component;
import java.lang.reflect.Array;
import java.util.Arrays;
@Component
@Aspect //将当前的组件标识为切面
public class LoggerAspect {
//切入点表达式,设置在表示通知的注解的value属性中
//exexecution()
//* com.xiaonan.srping.CalculatorImpl.*(..))
//第一个* 表示任意的访问修饰符和返回值类型
//第二个* 表示类中任意的方法
//..表示任意的参数列表
//获取链接点的信息
//使用signature
//使用公告的切入点表达式
//如果使用这个公共的切入点表示式之后
//只需要 比如: @After("pointCut()");
//把原来的execution换为Pointcut
@Pointcut("execution(* com.xiaonan.srping.CalculatorImpl.*(..))")
public void pointCut(){}
//标识为前置方法
//@Before("execution(public int com.xiaonan.srping.CalculatorImpl.add(int,int))")
@Before("execution(* com.xiaonan.srping.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint){
System.out.println("前置通知");
//获取连接点所对应的方法名
Signature signature=joinPoint.getSignature();
//获取连接点所对应的方法名
System.out.println("方法"+signature.getName());
//获取链接点所对应方法的参数
Object[] args=joinPoint.getArgs();
System.out.println("参数"+ Arrays.toString(args));
}
@After("pointCut()")
public void afterMethod(){
System.out.println("测试后置方法");
}
//在返回通知中若要获取目标对象的返回值,只需要通过returning注解的returning属性
//就可以将通知方法的某个参数指定为接受目标对象方法的返回值参数
@AfterReturning(value = "pointCut()",returning = "result")
public void AfterReturningMethod(JoinPoint joinPoint,Object result){
//在目标对象方法完成之后才执行本方法
System.out.println("返回值通知");
System.out.println("获取到的返回值结果为"+result);
}
//异常通知
//在返回通知中若要获取目标对象的返回值,只需要通过throwing注解的throwing属性
//就可以将通知方法的某个参数指定为接受目标对象方法的返回值参数
@AfterThrowing(value = "pointCut()",throwing = "ex")
public void AfterThrowingmethod(JoinPoint joinPoint,Exception ex){
System.out.println("异常通知");
System.out.println("异常原因为"+ex);
}
}
测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestClass {
public static void main(String[] args) {
ApplicationContext ioc = new ClassPathXmlApplicationContext("bean.xml");
Calculator calculator=ioc.getBean(Calculator.class);
//异常通知测试
//calculator.div(10,0);
calculator.div(10,2);
}
}
文章全部是本人原创,请勿转发,谢谢配合,版权所有-南香香-你会喜欢我吗