Spring AOP简单的配置(注解和xml配置)
注解配置AOP
项目路径:E:\JavaWebSrc\FirstSpringAOP
1:接口代码
接口为 IPerson ,接口不需要写注释
public interface IPerson { public void sayNmae(); public void introduceOneSelf(); }
2:实体类代码
student继承了IPerson接口
//将student实例化。装配到spring容器中
@Component("student") public class Student implements IPerson { @Value(value = "1") private int id; @Value(value = "余文辉") private String name; @Value(value = "男") private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }
3:切面类
这里使用的注解需要导入(aopaliance.jar和 aspectjweaver.jar 两个jar包)
//切面声明 @Aspect //装配到spring容器中 @Component public class LoggingAspect { // 声明切入点 声明切入点表达式 @Pointcut("execution(* com.yuwenhui.annotation.Student.* (..))") public void JoinPointExpecssion(){}; //前置通知 @Before("JoinPointExpecssion()") public void beforMethod(JoinPoint joinPoint){ // 获得方法名 String name = joinPoint.getSignature().getName(); // 获得参数列表,再将参数数组转换成List集合 List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("参数:"+args); } //后置通知 @After("JoinPointExpecssion()") public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("后置通知"); System.out.println("参数:"+args); } // 返回通知,在方法正常结束的时候返回,且 必须要有一个返回值 @AfterReturning(value = "JoinPointExpecssion()",returning = "result") public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); } // 异常通知 ,将在方法抛出异常时触 @AfterThrowing(value = "JoinPointExpecssion()",throwing ="e") public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("异常信息"+e); } }
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:component-scan base-package="com.yuwenhui.annotation"></context:component-scan>
<!-- 声明Aspect配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
测试类
public class TestAop { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); } }
XML配置AOP
1:接口配置
不管是XML配置还是annotation配置,接口类都不需要改变
public interface IPerson { public void sayNmae(); public void introduceOneSelf(); }
2:实体类配置
public class Student implements IPerson { private int id; private String name; private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
3:切面类
public class LoggingAspect { public void JoinPointExpecssion(){}; public void beforMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("参数:"+args); } public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("后置通知"); System.out.println("参数:"+args); } public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); } public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("异常信息"+e); } }
4:测试类
public class TestXml { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-beanxml.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); } }
5:配置文件
需要格外注意
<?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" 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-4.1.xsd"> <!-- 需要注意xml头,不是IDEA自动生成的 --> <!-- 配置实体类,并为相关字段初始化 --> <bean id="student" class="com.yuwenhui.xml.Student"> <property name="name" value="余文辉"></property> <property name="id" value="1"></property> <property name="sex" value="男"></property> </bean> <!-- 声明切面类,这里只是声明为普通的bean类 --> <bean id="loggingAspect" class="com.yuwenhui.xml.LoggingAspect"></bean> <!-- 配置aop --> <aop:config> <!-- 配置切点表达式 --> <aop:pointcut id="pointcut" expression="execution(* com.yuwenhui.xml.Student.* (..))"/> <!-- 这里才是真正的声明切面 --> <aop:aspect ref="loggingAspect"> <!--前置通知--> <aop:before method="beforMethod" pointcut-ref="pointcut"/> <!--后置通知--> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <!--返回通知--> <aop:after-returning method="afterMethodRetuning" pointcut-ref="pointcut" returning="result"/> <!--异常通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> </aop:aspect> </aop:config> </beans>