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/

posted @ 2016-02-17 17:13  沙中世界  阅读(260)  评论(0编辑  收藏  举报