千家信息网

SpringAOP在web应用中怎么用

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章给大家分享的是有关SpringAOP在web应用中怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。一、以声明的方式配置AOP(就是使用xml配置文件)1.使用P
千家信息网最后更新 2026年11月01日SpringAOP在web应用中怎么用

这篇文章给大家分享的是有关SpringAOP在web应用中怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

一、以声明的方式配置AOP(就是使用xml配置文件)

1.使用ProxyFactoryBean的方式:

ProxyFactoryBean类是FactoryBean的一个实现类,它允许指定一个bean作为目标,并且为该bean提供一组通知和顾问(这些通知和顾问最终会被合并到一个AOP代理中)它和我们之前的ProxyFactory都是Advised的实现。

以下是一个简单的例子:一个学生和一个老师,老师会告诉学生应该做什么。

public class Student { public void talk() { System.out.println("I am a boy"); } public void walk() { System.out.println("I am walking"); } public void sleep() { System.out.println("I want to sleep"); }}

老师类

public class Teacher { private Student student; public void tellStudent(){ student.sleep(); student.talk(); } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; }}

我们创建一个通知类,这个和之前是一样的SpringAOP中的通知类型以及创建

package cn.lyn4ever.aop;import org.aspectj.lang.JoinPoint;public class AuditAdvice implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, @Nullable Object o) throws Throwable { System.out.println("这个方法被通知了" + method.getName()); }}

然后就使用spring的IOC来管理这个通知类,在xml配置文件中声明如下:

advice

测试类

public static void main(String[] args) { GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load("application1.xml"); context.refresh(); Teacher teacher = (Teacher) context.getBean("teacherOne"); teacher.tellStudent(); }

运行结果没有问题

以上是通过直接创建通知的方式,接下来我们试一个创建一个切入点(因为以上是对类中所有方法都进行通知,这时我们使用切入点只对其中部分方法进行通知),在xml配置文件中添加如下。

advice advisor

上图中的那个aspectj表达式写错了,在代码中有正确的

2.使用aop名称空间

在xml中引入如下的名称空间,为了不被影响,我册了其他多余的名称空间。然后很普通地注入我们之前那三个bean

在这个配置中,我们还可以配置其他类型的通知,但是这个method属性一定要写我们自定义的那个通知类中的方法

在aop:pointcut中写expression时还支持如下语法:

3.使用@AspectJ样式注解方式

虽然是通过注解的方式来声明注解类,但是还是需要在xml中配置一点点内容(通过注解的方式也可以配置,但是在springboot中要使用的话有更方便的方式)

为了方便,就只写了一个HighStudent,而且直接调用它的方法,不依赖于外部的teacher实例来调用

package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.springframework.stereotype.Component;/** * 声明这是一个SpringBean,由Spring来管理它 */@Componentpublic class HighStudent { public void talk() { System.out.println("I am a boy"); } public void walk() { System.out.println("I am walking"); } /** * 这个方法添加一个teacher来做为参数,为了配置后边切入点中的args() * @param teacher */ public void sleep(Teacher teacher) { System.out.println("I want to sleep"); }}

创建切面类

package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;/** * 声明切面类,也就是包括切点和通知 */@Component //声明交由spring管理@Aspect //表示这是一个切面类public class AnnotatedAdvice { /* 创建切入点,当然也可以是多个 */ @Pointcut("execution(* talk*(..))") public void talkExecution(){} @Pointcut("bean(high*)")//这里为什么是high,因为我们这回测试bean是highStudent public void beanPoint(){} @Pointcut("args(value)") public void argsPoint(Teacher value){} /* 创建通知,当然也可以是多个 这个注解的参数就是上边的切入点方法名,注意有的还带参数 这个通知方法的参数和之前一样,榀加JoinPoint,也可不加 */ @Before("talkExecution()") public void doSomethingBefore(JoinPoint joinPoint){ System.out.println("before: Do Something"+joinPoint.getSignature().getName()+"()"); } /** * 环绕通知请加上ProceedingJoinPoint参数 ,它是joinPoint的子类 * 因为你要放行方法的话,必须要加这个 * @param joinPoint * @param teacher */ @Around("argsPoint(teacher) && beanPoint()") public Object doSomethindAround(ProceedingJoinPoint joinPoint, Teacher teacher) throws Throwable { System.out.println("Around: Before Do Something"+joinPoint.getSignature().getName()+"()"); Object proceed = joinPoint.proceed(); System.out.println("Around: After Do Something"+joinPoint.getSignature().getName()+"()"); return proceed; }}

xml中配置开启扫描注解

使用Java注解配置的方式配置扫描注解

@Configuration //声明这是一个配置类@ComponentScan("cn.lyn4ever.aop.aspectj")@EnableAspectJAutoProxy(proxyTargetClass = true)//相当于xml中的public class BeanConfig {}

测试方法

package cn.lyn4ever.aop.aspectj;import cn.lyn4ever.aop.aopconfig.Teacher;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.context.support.GenericApplicationContext;import org.springframework.context.support.GenericXmlApplicationContext;public class AspectMain { public static void main(String[] args) {// xmlConfig(); javaConfig(); } private static void javaConfig() { GenericApplicationContext context = new AnnotationConfigApplicationContext(BeanConfig.class); HighStudent student = (HighStudent) context.getBean("highStudent"); student.sleep(new Teacher());//应该被环绕通知 System.out.println(); student.talk();//前置通知 System.out.println(); student.walk();//不会被通知 System.out.println(); } private static void xmlConfig(){ GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load("application_aspect.xml"); context.refresh(); HighStudent student = (HighStudent) context.getBean("highStudent"); student.sleep(new Teacher());//应该被环绕通知 System.out.println(); student.talk();//前置通知 System.out.println(); student.walk();//不会被通知 System.out.println(); }}

感谢各位的阅读!关于"SpringAOP在web应用中怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

配置 方法 方式 注解 切入点 参数 内容 切面 名称 文件 空间 老师 这是 测试 管理 应用 多个 学生 就是 更多 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 小米移动互联网科技园 南京欣网互联网络科技有限公司 蓝思科技飞鸽互联网 互联网科技的发展 北京禾田雨橡互联网科技 互联网高科技企业 互联网科技股午盘股票涨跌 互联网科技产品 欧洲十大科技互联网公司 苏州铭阳互联网科技有限公司 成都互联网科技公司工作好不好 迪庆互联网科技招生 如何创新互联网科技 乌鲁木齐新华智原互联网科技学校 互联网金融科技元年 互联网第三次科技革命特征 下一代互联网重大应用科技园 中国科技和美国互联网 玉溪新华互联网科技怎么选 10位科技互联网大佬 百单网互联网科技有限公司怎么样 互联网 的科技创新 苏州互联网科技贷款公司 互联网公司会议桌创意科技软装饰 互联网之后的科技是什么 北京大学互联网科技怎么样 互联网与医疗科技 天津腾达互联网科技有限公司 上海京穆互联网科技有限公司 科技发展第三阶段互联网时代
0