package cn.cutter.start.bean;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component; /**
*
* @author xiaof
*
*/
@Component
public class AroundTestBean { private static final Log logger = LogFactory.getLog(AroundTestBean.class); public void method1() {
logger.info("AroundTestBean 执行方法1");
} public void method2(String param1) {
logger.info("AroundTestBean 执行方法1" + param1);
} public void method3() {
logger.info("AroundTestBean 执行方法1");
} public void method4() {
logger.info("AroundTestBean 执行方法1");
} }
package cn.cutter.start.aop;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; /**
* 环绕拦截
* @author xiaof
*
*/
@Component
@Aspect
public class AroundAspect { private static final Log logger = LogFactory.getLog(AroundAspect.class); @Pointcut("execution(* cn.cutter.start.bean.AroundTestBean.method1(..))")
public void pointcut1() {} @Pointcut("execution(* cn.cutter.start.bean.AroundTestBean.method2(..))")
public void pointcut2() {} @Pointcut("execution(* cn.cutter.start.bean.AroundTestBean.method3(..))")
public void pointcut3() {} @Pointcut("execution(* cn.cutter.start.bean.AroundTestBean.method4(..))")
public void pointcut4() {} /**
* 注意环绕拦截,必须有参数ProceedingJoinPoint joinPoint,而且必须是第一个
* @param joinPoint
*/
@Around("pointcut1()")
public void around1(ProceedingJoinPoint joinPoint) {
//环绕拦截,拦截 cn.cutter.start.bean.AroundTestBean.method1 这个方法
try {
logger.info("AroundAspect 环绕拦截的try中开始方法,然后执行对应的方法");
Object o = joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
} finally {
logger.info("AroundAspect 环绕拦截的最终方法!");
} } @Around("pointcut2() && args(taskName)")
public void around2(ProceedingJoinPoint joinPoint, String taskName) {
//环绕拦截,拦截 cn.cutter.start.bean.AroundTestBean.method1 这个方法
try {
logger.info("AroundAspect 环绕拦截的try中开始方法,然后执行对应的方法,参数是:" + taskName);
Object o = joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
} finally {
logger.info("AroundAspect 环绕拦截的最终方法!");
} } }
@Test
public void testAop6() {
ApplicationContext ctx = this.before(); AroundTestBean att = (AroundTestBean) ctx.getBean("aroundTestBean"); att.method2("param test"); }

@Introduction

我们先首先明确一点,这个注解是用来给原来的对象添加新的行为逻辑的

说白了就是给类扩展自定义方法

我们定义个类,作为添加方法的目标

package cn.cutter.start.bean;

import org.springframework.stereotype.Component;

@Component
public class IntroductionTestBean { }

好的,现在如果我们想给这个空的对象,添加一个,或者许多方法,怎么办呢???

不急,我们先把要进行添加的方法准备好

package cn.cutter.start.introduction;

/**
* 用来使用spring的扩展增强,@introduction
* 用来给对象进行添加方法
* @author xiaof
*
*/
public interface TestIntroduction1 { public void dosomething(); }

这里是一个接口,里面有个dosomething的方法,我们可以实现多种方式,然后添加的效果也可以多种多样

我们实现两个类

package cn.cutter.start.introduction.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import cn.cutter.start.introduction.TestIntroduction1; public class TestIntroduction1Impl implements TestIntroduction1 { private final Log logger = LogFactory.getLog(TestIntroduction1Impl.class); @Override
public void dosomething() {
// TODO Auto-generated method stub
logger.info("introduction 操作 dosomething!!!");
} }
package cn.cutter.start.introduction.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import cn.cutter.start.introduction.TestIntroduction1; public class TestIntroduction1Impl2 implements TestIntroduction1 { private final Log logger = LogFactory.getLog(TestIntroduction1Impl2.class); @Override
public void dosomething() {
logger.info("这里是另外一个实现接口的方法"); } }

好,要添加的目标有了,要添加的配料也有了,是时候炒一盘了

package cn.cutter.start.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component; import cn.cutter.start.introduction.TestIntroduction1;
import cn.cutter.start.introduction.impl.TestIntroduction1Impl;
import cn.cutter.start.introduction.impl.TestIntroduction1Impl2; @Component
@Aspect
public class IntroductionAspect { @DeclareParents(
value="cn.cutter.start.bean.IntroductionTestBean",
defaultImpl=TestIntroduction1Impl.class)
public TestIntroduction1 testIntroduction1; // @DeclareParents(
// value="cn.cutter.start.bean.IntroductionTestBean",
// defaultImpl=TestIntroduction1Impl2.class)
// public TestIntroduction1 testIntroduction2; }

看到了么,这里@DeclareParents中的两个参数,一个是目标,一个是配置调料

Value是目标,defaultImpl是调料

测试一波:

@Test
public void testAopIntroduction() {
ApplicationContext ctx = this.before(); IntroductionTestBean att = (IntroductionTestBean) ctx.getBean("introductionTestBean");
// Object obj = (IntroductionTestBean) ctx.getBean("introductionTestBean"); TestIntroduction1 test = (TestIntroduction1) att;
test.dosomething();
//
// TestIntroduction1 test2 = (TestIntroduction1Impl2) obj;
// test2.dosomething(); }

【sping揭秘】17、@Around,@Introduction的更多相关文章

  1. 【sping揭秘】25、Spring远程方案

    分化:RMI,EJB,Hessian Spring有 Rmi,http,hessian,burlap 基于rmi的remoting方案 RMI要求远程类对象包路径和本地一致 基于HTTP的轻量级rem ...

  2. 【sping揭秘】24、Spring框架对JMS的集成(无环境版,以后学MQ的时候再隆重介绍)& 任务调度和线程池

    这个我也不是很了解,那么这个需要好好学习一下了 JMS有2种消息域类型 1. point to point 点对点模式 2.发布订阅模式  publish/subscribe Pub/Sub 模式 传 ...

  3. 【sping揭秘】23、Spring框架内的JNDI支持

    JndiTemplate 经过jdbctemplate,transactionTemplate...的洗礼,想必大家看到template就知道是个什么尿性了吧 一样的,我们只需要调用jnditempl ...

  4. 【sping揭秘】22、事务管理

    有关事务的楔子 什么是事务??? 事务就是以可控的方式对数据资源进行访问的一组操作. 事务本身持有四个限定属性 原子性,一致性,隔离性,持久性 事务家族 Resource Manager  RM,负责 ...

  5. 【sping揭秘】21、Spring动态数据源的切换

    对于多个数据源的时候,我们如何切换不同的数据源进行数据库的操作呢? 当然我们可以直接定义2个DataSource,然后在每次获取connection的时候,从不同的DataSource中获取conne ...

  6. 【sping揭秘】20、spring的orm

    面向对象的操作方式,spring统一定义在org.springframework.jdbc.object以RdbmsOperation作为顶层抽象定义 Spring对各种ORM的集成 Spring的集 ...

  7. 【sping揭秘】19、关于spring中jdbctemplate中的DataSource怎么来呢

    我们这是可以正好借助之前学的factorybean类,自己吧jdbctemplate加载到spring容器中,我们可以封装多个这种对象,那么可以实现针对不同的数据库的jdbctemplate 首先我们 ...

  8. 【sping揭秘】18、使用spring访问数据

    统一的数据访问异常层次体系 基于基本的jdbc封装dao层访问接口,封装不论是访问,csv文件,关系数据库(RDBMS),ladp都可以封装成一个个DAO对象来进行访问 抛出问题 可是对于我们忽略了一 ...

  9. 【sping揭秘】16、@After(finally) 但是这个实在afterturning之前执行

    package cn.cutter.start.bean; import org.apache.commons.logging.Log; import org.apache.commons.loggi ...

随机推荐

  1. 服务器&linux

    linux vsftp查看端口占用:netstat -natp |grep 21如果有占用21端口进程,kill它 ,或者remove它.安装:yum -y install vsftpduseradd ...

  2. 算法练习LeetCode初级算法之数组

    删除数组中的重复项 官方解答: 旋转数组 存在重复元素 只出现一次的数     官方解答:  同一个字符进行两次异或运算就会回到原来的值 两个数组的交集 II import java.util.Arr ...

  3. Java比较两个时间的前后

    public static int compare_date(String DATE1, String DATE2) { DateFormat df = new SimpleDateFormat(&q ...

  4. (sealed)密封类及密封方法优缺点

    1. 密封类防止被继承 (有利于代码优化, 由于密封类的不被继承性, 代码在搜索此方法时可以直接定位, 不需要一层层的找继承关系) 只有本程序集可以使用 2. 密封类中不需要再写密封方法(一般密封方法 ...

  5. yum提示Another app is currently holding the yum lock

    使用yum grouplis列举系统中以组安装的包,结果提示: # yum grouplist Loaded plugins: fastestmirror, refresh-packagekit, s ...

  6. Mouse Detected Problem

    通常分三种情形: 鼠标完全不工作鼠标工作一段时间后不工作鼠标的按钮或者滚轮不工作 必要提交信息:1.鼠标的具体厂商和型号2.鼠标连接PC方式:串口.PS/2.USB或无线USB等:3.鼠标的工作机制: ...

  7. HDU 3666.THE MATRIX PROBLEM 差分约束系统

    THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Minimum Domino Rotations For Equal Row LT1007

    In a row of dominoes, A[i] and B[i] represent the top and bottom halves of the i-th domino.  (A domi ...

  9. wamp环境搭建(apache安装,mysql安装,php安装)

    1.软件安装说明 WAMP:Window操作系统+Apache软件+PHP解析器+MySQL软件 2.Apache执行流程 用户向服务器端发送请求àDNS解析àIP地址à端口àApache服务 Apa ...

  10. M1卡知识点描述