Java SpringAOP技术之注解方式详解

网友投稿 320 2022-08-26


Java SpringAOP技术之注解方式详解

目录1.配置xml扫描注解 2.配置注解3.配置文件中开启自动代理4.通知类型注解5.测试类6.结果 总结

1.配置xml扫描注解

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

2.配置注解

package com.qcby;

import org.springframework.stereotype.Component;

@Component(value = "user")

public class User {

//连接点/切入点

public void add(){

System.out.println("add......");

}

}

给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明

@Component

@Aspect //生成代理对象

public class UserProxy {

}

3.配置文件中开启自动代理

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context.xsd http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop.xsd">

4.通知类型注解

@Before -- 前置通知

@AfterReturing -- 后置通知

@Around -- 环绕通知(目标对象方法默认不执行的,需要手动执行)

@After -- 最终通知

@AfterThrowing -- 异常抛出通知

package com.qcby;

import org.aspectj.lang.ProceedingJoinPoint;

import org.aspectj.lang.annotation.*;

import org.springframework.stereotype.Component;

@Component

@Aspect //生成代理对象

public class UserProxy {

//增强/通知 ---》前置通知

@Before(value = "execution(public void com.qcby.User.add())")

public void before(){

System.out.println("前置通知.............");

}

// 环绕通知

@Around(value = "execution(public void com.qcby.User.add())")

public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

System.out.println("环绕前置.............");

// 执行被增强的方法

proceedingJoinPoint.proceed();

System.out.println("环绕后置.............");

}

// 最终通知

@After(value = "execution(public void com.qcby.User.add())")

public void after() {

System.out.println("最终通知.............");

}

//后置通知

@AfterReturning(value = "execution(public void com.qcby.User.add())")

public void afterReturning() {

System.out.println("后置通知.............");

}

//异常通知

@AfterThrowing(value = "execution(public void com.qcby.User.add())")

public void afterThrowing() {

System.out.println("出错了.............");

}

}

5.测试类

package com.qcby.test;

import com.qcby.User;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserTest {

@Test

public void aopTest1(){

http:// ApplicationContext applicationContext = new ClassPathXmlApplicationContext("SpringConfig.xml");

User user = (User) applicationContext.getBean("user");

user.add();

}

}

6.结果

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Python 实战TCP的客户端 - 胖子老板,再来一包烟(python下载安装教程)
下一篇:python3中的unicode_escape(python3中的str类型没有decode方法)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~