当前位置: 首页 > 文档资料 > Spring 中文教程 >

基于 XML Schema(XML Schema based)

优质
小牛编辑
133浏览
2023-12-01

要使用本节中描述的AOP命名空间标记,您需要按照描述导入springAOP模式 -

<?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-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
   <!-- bean definition & AOP specific configuration -->
</beans>

您还需要在应用程序的CLASSPATH上使用以下AspectJ库。 这些库位于AspectJ安装的“lib”目录中,否则您可以从Internet下载它们。

  • aspectjrt.jar
  • aspectjweaver.jar
  • aspectj.jar
  • aopalliance.jar

宣布一个方面

使用《aop:aspect》元素声明《aop:aspect》 ,并使用ref属性引用支持bean,如下所示 -

<aop:config>
   <aop:aspect id = "myAspect" ref = "aBean">
      ...
   </aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
   ...
</bean>

这里将配置“aBean”并像其他任何Spring bean一样注入依赖项,如前面章节所述。

宣布切入点

pointcut有助于确定要用不同建议执行的感兴趣的连接点(即方法)。 在使用基于XML Schema的配置时,切入点将定义如下 -

<aop:config>
   <aop:aspect id = "myAspect" ref = "aBean">
      <aop:pointcut id = "businessService" 
         expression = "execution(*com.xyz.myapp.service.*.*(..))"/>
         ...
   </aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
   ...
</bean>

以下示例定义了一个名为“businessService”的切入点,该切入点将与cn.xnip包下的Student类中可用的getName()方法的执行相匹配 -

<aop:config>
   <aop:aspect id = "myAspect" ref = "aBean">
      <aop:pointcut id = "businessService" 
         expression = "execution(*cn.xnip.Student.getName(..))"/>
         ...
   </aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
   ...
</bean>

声明建议

您可以使用下面给出的元素声明中的五个建议中的任何一个 -

<aop:config>
   <aop:aspect id = "myAspect" ref = "aBean">
      <aop:pointcut id = "businessService"
         expression = "execution(* com.xyz.myapp.service.*.*(..))"/>
      <!-- a before advice definition -->
      <aop:before pointcut-ref = "businessService" method = "doRequiredTask"/>
      <!-- an after advice definition -->
      <aop:after pointcut-ref = "businessService" method = "doRequiredTask"/>
      <!-- an after-returning advice definition -->
      <!--The doRequiredTask method must have parameter named retVal -->
      <aop:after-returning pointcut-ref = "businessService"
         returning = "retVal" method = "doRequiredTask"/>
      <!-- an after-throwing advice definition -->
      <!--The doRequiredTask method must have parameter named ex -->
      <aop:after-throwing pointcut-ref = "businessService"
         throwing = "ex" method = "doRequiredTask"/>
      <!-- an around advice definition -->
      <aop:around pointcut-ref = "businessService" method = "doRequiredTask"/>
      ...
   </aop:aspect>
</aop:config>
<bean id = "aBean" class = "...">
  ...
</bean>

您可以对不同的建议使用相同的doRequiredTask或不同的方法。 这些方法将被定义为方面模块的一部分。

基于XML Schema的AOP示例

为了理解与基于XML Schema的AOP相关的上述概念,让我们编写一个实现很少建议的示例。 要编写我们的示例并提供一些建议,让我们使用一个可用的Eclipse IDE并执行以下步骤来创建Spring应用程序 -

描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包cn.xnip
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3 在项目中添加Spring AOP特定库aspectjrt.jar, aspectjweaver.jaraspectj.jar
4cn.xnip包下创建Java类LoggingStudentMainApp
5src文件夹下创建Beans配置文件Beans.xml
6 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是Logging.java文件的内容。 这实际上是方面模块的一个示例,它定义了在各个点调用的方法。

package cn.xnip;
public class Logging {
   /** 
      * This is the method which I would like to execute
      * before a selected method execution.
   */
   public void beforeAdvice(){
      System.out.println("Going to setup student profile.");
   }
   /** 
      * This is the method which I would like to execute
      * after a selected method execution.
   */
   public void afterAdvice(){
      System.out.println("Student profile has been setup.");
   }
   /** 
      * This is the method which I would like to execute
      * when any method returns.
   */
   public void afterReturningAdvice(Object retVal) {
      System.out.println("Returning:" + retVal.toString() );
   }
   /**
      * This is the method which I would like to execute
      * if there is an exception raised.
   */
   public void AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }
}

以下是Student.java文件的内容

package cn.xnip;
public class Student {
   private Integer age;
   private String name;
   public void setAge(Integer age) {
      this.age = age;
   }
   public Integer getAge() {
	  System.out.println("Age : " + age );
      return age;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      System.out.println("Name : " + name );
      return name;
   }
   public void printThrowException(){
	   System.out.println("Exception raised");
      throw new IllegalArgumentException();
   }
}

以下是MainApp.java文件的内容

package cn.xnip;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      Student student = (Student) context.getBean("student");
      student.getName();
      student.getAge();
      student.printThrowException();
   }
}

以下是配置文件Beans.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:aop = "http://www.springframework.org/schema/aop"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
   <aop:config>
      <aop:aspect id = "log" ref = "logging">
         <aop:pointcut id = "selectAll" 
            expression = "execution(* cn.xnip.*.*(..))"/>
         <aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/>
         <aop:after pointcut-ref = "selectAll" method = "afterAdvice"/>
         <aop:after-returning pointcut-ref = "selectAll" 
            returning = "retVal" method = "afterReturningAdvice"/>
         <aop:after-throwing pointcut-ref = "selectAll" 
            throwing = "ex" method = "AfterThrowingAdvice"/>
      </aop:aspect>
   </aop:config>
   <!-- Definition for student bean -->
   <bean id = "student" class = "cn.xnip.Student">
      <property name = "name" value = "Zara" />
      <property name = "age" value = "11"/>      
   </bean>
   <!-- Definition for logging aspect -->
   <bean id = "logging" class = "cn.xnip.Logging"/> 
</beans>

完成源和bean配置文件的创建后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 -

Going to setup student profile.
Name : Zara
Student profile has been setup.
Returning:Zara
Going to setup student profile.
Age : 11
Student profile has been setup.
Returning:11
Going to setup student profile.
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content

上面定义的选择cn.xnip包下定义的所有方法。 让我们假设,您希望在特定方法之前或之后执行建议,您可以通过将切入点定义中的星号(*)替换为实际的类和方法名称来定义切入点以缩小执行范围。 以下是一个修改后的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:aop = "http://www.springframework.org/schema/aop"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
   http://www.springframework.org/schema/aop 
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
   <aop:config>
      <aop:aspect id = "log" ref = "logging">
         <aop:pointcut id = "selectAll" 
            expression = "execution(* cn.xnip.Student.getName(..))"/>
         <aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/>
         <aop:after pointcut-ref = "selectAll" method = "afterAdvice"/>
      </aop:aspect>
   </aop:config>
   <!-- Definition for student bean -->
   <bean id = "student" class = "cn.xnip.Student">
      <property name = "name" value = "Zara" />
      <property name = "age" value = "11"/>      
   </bean>
   <!-- Definition for logging aspect -->
   <bean id = "logging" class = "cn.xnip.Logging"/> 
</beans>

如果您使用这些配置更改执行示例应用程序,它将打印以下消息 -

Going to setup student profile.
Name : Zara
Student profile has been setup.
Age : 11
Exception raised
.....
other exception content