Spring 引入式AOP 实例

Spring 引入式AOP 实例

package cn.com.chujie.spring.springAop;

/**
 * 目标切点类
 */
public interface Performance {
    /**
     * 目标切点方法
     */
    public void perform();
}

package cn.com.chujie.spring.springAop;

import org.springframework.stereotype.Component;

/**
 * 目标切点的实现类
 */
@Component
public class PerformanceImpl implements  Performance {
    @Override
    public void perform() {
        System.out.println("PerformanceImpl.perform()执行");
    }
}

package cn.com.chujie.spring.springAop;

/**
 * 通知类接口
 */
public interface Encoreable {
    void performEncore();
}

package cn.com.chujie.spring.springAop;

/**
 * 通知实现类
 */
public class DefaultEncoreable implements Encoreable {
    @Override
    public void performEncore() {
        System.out.println("DefaultEncoreable.performEncore()");
    }
}

package cn.com.chujie.spring.springAop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;

/**
 * 引入式AOP
 */
@Aspect
public class EncoreableIntroducer {
    @DeclareParents( value = "cn.com.chujie.spring.springAop.Performance+",
            defaultImpl = DefaultEncoreable.class )
    public static Encoreable encoreable;
}

package cn.com.chujie.spring.springAop;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-mvc.xml" , "classpath:spring-aop.xml","classpath:spring-bean.xml"})
public class AopTest {
    @Autowired
    Performance performanceImpl;
    @Test
    public void audience(){
        Assert.assertNotNull(performanceImpl);
        performanceImpl.perform();
        Encoreable encoreable = (Encoreable)performanceImpl;
        encoreable.performEncore();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值