加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring(十一)之AOP

发布时间:2020-12-15 07:12:38 所属栏目:Java 来源:网络整理
导读:Spring 框架的一个关键组件是 面向方面的编程 (AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为 横切关注点 ,这些横切关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好的方

Spring 框架的一个关键组件是面向方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为横切关注点,这些横切关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好的方面的例子,如日志记录、审计、声明式事务、安全性和缓存等。

在 OOP 中,关键单元模块度是类,而在 AOP 中单元模块度是方面。依赖注入帮助你对应用程序对象相互解耦和 AOP 可以帮助你从它们所影响的对象中对横切关注点解耦。AOP 是像编程语言的触发物,如 Perl,.NET,Java 或者其他。

Spring AOP 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你可以在方法执行之前或之后添加额外的功能。

?

?

Spring中基于AOP的XML架构

示例:

(1)编写Logging .java

package com.tutorialspoint;
public class Logging {
   /** 
    * This is the method which I would like to execute
    * before a selected method execution.
    */
   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.
     afterAdvice(){
      System.out.println("Student profile has been setup." 
    * This is the method which I would like to execute
    * when any method returns.
     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.
     AfterThrowingAdvice(IllegalArgumentException ex){
      System.out.println("There has been an exception: " + ex.toString());   
   }  
}

?

(2)编写Student.java

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

(3)编写MainApp.java

import org.springframework.context.ApplicationContext;
 org.springframework.context.support.ClassPathXmlApplicationContext;

 MainApp {
     static  main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("Beans.xml");
          Student student = (Student) context.getBean("student");
          student.getName();
          student.getAge();      
          student.printThrowException();
       }
}

?

(4)编写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(* com.tutorialspoint.*.*(..))"/>
         <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="com.tutorialspoint.Student">
      <property name="name"  value="Zara" />
      <property name="age"  value="11"/>      
   </bean>

   <!-- Definition for logging aspect -->
   <bean id="logging" class="com.tutorialspoint.Logging"/> 
   
</beans>

?

(5)运行MainApp.java中的main方法

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读