• Spring AOP面向切面编程的实现


    1、涉及到的几个概念

      切面类、被切对象、切入点、切入时间、切入内容;(自己命的名,好理解点)

    2、看配置文件

    <?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"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop 
               http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> 
        
        <bean id="audience" class="com.spring.test.aop.Audience"/>
        
        <bean id="sax" class="com.spring.test.setter.Saxophone"/>
        <bean id="kenny" class="com.spring.test.setter.Instrumentalist">
             <property name="song" value="Jingle Bells" />
             <property name="age" value="25" />
             <property name="instrument" ref="sax"/>
        </bean>
        
        <aop:config proxy-target-class="true">
            <aop:aspect ref="audience">
                    <aop:pointcut id="performance" expression="execution(* com.spring.test.setter.Instrumentalist.*(..))"/>
                <!--<aop:pointcut id="performance1" expression="execution(* com.spring.test.action1.Performer.perform(..))"/>-->
                
                <aop:before pointcut-ref="performance" method="takeSeats"/>
                <aop:before pointcut-ref="performance" method="turnOffCellPhones"/>
                <aop:after-returning pointcut-ref="performance" method="applaud"/>
                <aop:after-throwing pointcut-ref="performance" method="demandRefund"/>
                
            </aop:aspect>
        </aop:config>
    </beans>
    AOP配置文件

    3、需要引入的jar包

    4、代码

    package com.spring.test.action1;
    
    public interface Performer {
        void perform() ;
    }
    package com.spring.test.setter;
    
    import com.spring.test.action1.Performer;
    /**
     * 被切面类,(一般是业务类)
     * @author Administrator
     *
     */
    public class Instrumentalist implements Performer{
        private String song;
        private int age;
        private Instrument instrument;
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getSong() {
            return song;
        }
        public void setSong(String song) {
            this.song = song;
        }
        public Instrument getInstrument() {
            return instrument;
        }
        public void setInstrument(Instrument instrument) {
            this.instrument = instrument;
        }
        public Instrumentalist(){}
        public Instrumentalist(String song,int age,Instrument instrument){
            this.song = song;
            this.age = age;
            this.instrument = instrument;
        }
        public void perform() {
            System.out.println("Instrumentalist age:"+age);
            System.out.print("Playing "+song+":");
            instrument.play();
        }
        public void say(){
            System.out.println("this is saying !!!!!");
        }
    }
    package com.spring.test.setter;
    
    public interface Instrument {
        public void play();
    }
    package com.spring.test.setter;
    
    public class Saxophone implements Instrument {
        public Saxophone(){}
        public void play() {
            System.out.println("TOOT TOOT TOOT");
        }
    }
    package com.spring.test.aop;
    /**
     * 切面类
     * @author Administrator
     *
     */
    public class Audience {
        public void takeSeats(){
            System.out.println("The audience is taking their seats.");
        }
        public void turnOffCellPhones(){
            System.out.println("The audience is turning off their cellphones");
        }
        public void applaud(){
            System.out.println("CLAP CLAP CLAP");
        }
        public void demandRefund(){
            System.out.println("Boo! We want money back");
        }
    }
    package com.spring.test.setter;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class test {
        public static void main(String[] args)  {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
            
            Instrumentalist performer = (Instrumentalist)ctx.getBean("kenny");
            performer.perform();
            performer.say();
            
        }
    }

    5、关于切点表达式 详见:

    http://www.cnblogs.com/yanjunwu/p/3996570.html

    切面编程优点:有利于系统维护、升级、优化改造;便于将业务逻辑分离;

    参考:www.cnblogs.com/xing901022/

  • 相关阅读:
    118. Pascal's Triangle
    697. Degree of an Array
    1013. Partition Array Into Three Parts With Equal Sum
    167. Two Sum II
    ol7 禁用mysql 自启动
    pgsql常用命令
    清空history 命令记录
    pgsql启动报错
    在rhel 7.4中安装glibc-devel-2.17-196.el7.i686包的过程详录
    postgresql-9.2 install
  • 原文地址:https://www.cnblogs.com/tengpan-cn/p/5196009.html
Copyright © 2020-2023  润新知