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. document.getElementById(“id”)与$("#id")的区别

    document.getElementById("id")可以直接获取当前对象, jQuery利用$("#id")获取的是一个[object Object],需 ...

  2. $nextTick 的作用

    文档:深入响应式原理 Vue 实现响应式并不是数据发生变化之后 DOM 立即变化,而是按一定的策略进行 DOM 的更新. $nextTick 是在下次 DOM 更新循环结束之后执行延迟回调,在修改数据 ...

  3. 第一个只出现一次的字符字符(python)

    题目描述 在一个字符串(0<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).   # -*- codin ...

  4. ucore-lab1-练习5report

    实验5--实现函数调用堆栈跟踪函数 需要完成kdebug.c中函数print_stackframe的实现,可以通过函数print_stackframe来跟踪函数调用堆栈中记录的返回地址. 一.函数堆栈 ...

  5. http://ctf.bugku.com/challenges#Mountain%20climbing:bugku--Mountain-Climbing

      分析这道题,爽,能够结合IDA和ollydbg分析代码,美滋滋.但如果以后能直接根据汇编容易地看懂逻辑那就更好了. 参考链接: https://blog.csdn.net/cossack9989/ ...

  6. E. Segment Sum(数位dp)

    题意:求一个区间内满足所有数位不同数字个数小于K的数字总和.比如:k=2   1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足. 我们可以使用一个二进制的数 ...

  7. 5. Longest Palindromic Substring 返回最长的回文子串

    [抄题]: Given a string s, find the longest palindromic substring in s. You may assume that the maximum ...

  8. 微软Office Online服务安装部署(三)

    现在开始配置两台服务器,两台服务器的IP: Server: 10.1.3.89 Client:  10.1.3.92 1.在Client中,.打开网络属性,找到ipv4的配置,将dns 改成域控制器的 ...

  9. 配置apache虚拟域名(phpStudy2016)

    以前也一个个的配置过apache.php和mysql,现在嫌麻烦,就用的phpStudy. 装好之后,发现127.0.0.1可以访问,但是localhost就不可以访问.大概是因为apache没有配置 ...

  10. CentOS6.5在虚拟机中安装

    只有一点,先建虚拟机,再选择iso镜像安装,注意,安装路径不能有中文空格之类的. CentOS6.5 64位下载链接 链接:https://pan.baidu.com/s/1d6zp5LtKtkL8I ...