如何在网络应用中使用Spring AOP

  介绍

这篇文章将为大家详细讲解有关如何在网络应用中使用Spring AOP,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

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

<强> 1。使用ProxyFactoryBean的方式:

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

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

public  class  Student  {      ,public  void 谈话(),{   System.out.println才能(“小姐:am  a  boy");   ,}      ,public  void 走(),{   System.out.println才能(“小姐;am  walking");   ,}      ,public  void 睡眠(),{   System.out.println才能(“小姐:want 用sleep");   ,}   }

老师类

public  class  Teacher  {      ,private  Student 学生;      ,public  void  tellStudent () {   student.sleep才能();   student.talk才能();   ,}      ,public  Student  getStudent (), {   return 才能;学生;   ,}      ,public  void  setStudent (Student 学生),{   时间=this.student 才能;学生;   ,}   }

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

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

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

& lt; ? xml  version=?.0“,编码=癠TF-8" ?比;   http://www.springframework.org/schema/beans" & lt; beans  xmlns=?;   ,,xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance"   xmlns:才能util=癶ttp://www.springframework.org/schema/util", xmlns: p=癶ttp://www.springframework.org/schema/p"   ,xsi: schemaLocation=癶ttp://www.springframework.org/schema/beans   ,http://www.springframework.org/schema/beans/spring-beans.xsd   ,http://www.springframework.org/schema/util   ,https://www.springframework.org/schema/util/spring-util.xsd"才能的在      ,& lt; !——注入学生——比;   ,& lt; bean  name=皊tudent",类=癱n.lyn4ever.aop.aopconfig.Student"比;   ,& lt;/bean>      ,& lt; !——注入老师——比;   ,& lt; bean  name=皌eacher",类=癱n.lyn4ever.aop.aopconfig.Teacher"比;   & lt;才能!——注意,这个学生的属性要是上边的代理类,而不是能学生——比;   & lt;才能!——& lt; property  name=皊tudent", ref=皊tudent"/祝辞——比;   & lt;才能property  name=皊tudent", ref=皃roxyOne"/比;   ,& lt;/bean>      ,& lt; !——注入我们创建的通知类——比;   ,& lt; bean  id=癮dvice",类=癱n.lyn4ever.aop.aopconfig.AuditAdvice"祝辞& lt;/bean>      ,& lt; !——创建代理类,使用前边写的通知进行通知,这样会使这个类上的所有方法都被通知——比;   ,& lt; bean  name=皃roxyOne",类=皁rg.springframework.aop.framework.ProxyFactoryBean", p: target-ref=皊tudent"   ,,p: interceptorNames-ref=癷nterceptorNames"比;   & lt;才能!——因为interceptorNames的属性是一个可变参数,也就是一个列表——比;   ,& lt;/bean>      ,& lt; !——在上边引入了跑龙套的名称空间,简化了书写——比;   ,& lt; util: list  id=癷nterceptorNames"比;   & lt;才能value> advice</value>   ,& lt;/util: list>   & lt;/beans>

测试类

, public  static  void  main (String [], args), {   GenericXmlApplicationContext 才能;context =, new  GenericXmlApplicationContext ();   context.load才能(“application1.xml");   context.refresh才能();      Teacher 才能;Teacher =,(老师),context.getBean (“teacherOne");   teacher.tellStudent才能();      以前,}

运行结果没有问题

如何在网络应用中使用Spring AOP