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. mybatis-generator 覆盖新增XML

    参考文章:https://www.cnblogs.com/xxoome/p/10068780.html 1.添加依赖(版本1.3.7) plugin> <groupId>org.my ...

  2. 语义分割之Dual Attention Network for Scene Segmentation

    Dual Attention Network for Scene Segmentation 在本文中,我们通过 基于自我约束机制捕获丰富的上下文依赖关系来解决场景分割任务.       与之前通过多尺 ...

  3. 数组中出现次数超过一半的数字(python)

    题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...

  4. matlab--二维画图及三维画图的相关命令

    clc;clear all;close all; % plot3是最简单的三维图% mesh是网格图% surf是曲面图%plot3-------------------------t1=0:pi/3 ...

  5. Yii2增删改查

    Controller <?php namespace frontend\controllers; use frontend\models\User; use yii\data\Paginatio ...

  6. Linux安装redis服务器

    Linux安装redis服务器 初次接触,这里简单的说下我遇到的情况以及安装方法,当然也是参考了诸位大神的. 确定虚拟机的主机IP. 1)首先需要一个linux虚拟机,确定虚拟机的ip ,输入命令:# ...

  7. vue 需求 data中的数据之间的调用

    我遇到过这种情况  就是在我的data中 会有数据调用data中的其他数据 如图  我的alertInfoType需要拿到screeningCondition中type的值 用过vue的都知道 我是不 ...

  8. Linux配置ntp时间服务器(全)

    时间服务器作用: 大数据产生与处理系统是各种计算设备集群的,计算设备将统一.同步的标准时间用于记录各种事件发生时序, 如E-MAIL信息.文件创建和访问时间.数据库处理时间等. 大数据系统内不同计算设 ...

  9. quick-cocos2d-x lua框架解析(一)对UI进行操作的UiUtil脚本

    最近一段时间接手了一个cocos游戏项目,由于我是U3D开发入门,所以花了一段时间来钻研cocos2d的使用与项目架构.与U3D相比,cocos2d的开发界面实在做的不咋地.不过在看过源码之后,源码跑 ...

  10. ActiveMQ_5死信队列

    activemq死信队列 DLQ-死信队列(Dead Letter Queue)用来保存处理失败或者过期的消息. 出现以下情况时,消息会被redelivered: A transacted sessi ...