AOP实现方式一

<?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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
            http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd     
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd    
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd     
              http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd 
              ">

<!-- 把那个类注入进来 -->
    <bean id="sleepHelper" class="com.test.aop.SleepHelper"/>
    <bean id="human" class="com.test.aop.Human"/>

<!-- 配置一个切点 -->
    <!-- 配置切点有好几种表达方式 1、配置aspectJ 2.使用正则表达式, 以下使用的是正则表达式 -->
    <!-- pattern属性指定了正则表达式,它匹配所有的sleep方法 -->
    
    <bean id="spleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*sleep" />
    </bean>

<!-- 切点仅仅是定义了故事发生的地点,还有故事发生的时间以及最重要的故事的内容,就是通知了,我们需要把通知跟切点结合起来,我们要使用的通知者是: -->
    
    <bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="advice" ref="sleepHelper" />
        <property name="pointcut" ref="spleepPointcut" />
    </bean>
    <!-- 切入点和通知都配置完成,接下来该调用ProxyFactoryBean产生代理对象了 -->
    <!-- ProxyFactoryBean是一个代理,我们可以把它转换为proxyInterfaces中指定的实现该interface的代理对象: -->
    <bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target" ref="human" />
        <property name="interceptorNames" value="sleepHelperAdvisor" />
        <property name="proxyInterfaces" value="com.test.aop.Sleepable" />
    </bean>

</beans>

//实现接口

package com.test.aop;

public class Human implements Sleepable {

@Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("睡觉啦,哈哈");
    }
    
    
}

//定义一个接口实现jdk代理

package com.test.aop;
public interface Sleepable {
    
    void sleep();
}

//实现前后通知
package com.test.aop;

import Java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

@Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
        niaho();
    }

@Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        enen();
    }
    public void niaho(){
        System.out.println("调用前,你应该做的是");
    }
    public void enen(){
        System.out.println("调用后你应该做的事");
    }

}

测试创建ioc容器
package com.test.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Tests {

public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop1.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");
        sleeper.sleep();
        }
}

AOP实现方式二

<?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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
            http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd     
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd    
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd     
              http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd 
              ">

<bean id="sleepHelper" class="com.test.aop2.SleepHelper" />

<bean id="sleepAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="sleepHelper"></property>
        <property name="pattern" value=".*sleep"></property>
    </bean>

<bean id="human" class="com.test.aop2.Human" />

<bean
        class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>

package com.test.aop2;
public class Human implements Sleepable {

@Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("睡觉啦,哈哈");
    }
}

package com.test.aop2;
public interface Sleepable {
    
    void sleep();
}

package com.test.aop2;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

@Override
    public void afterReturning(Object returnValue, Method method,
            Object[] args, Object target) throws Throwable {
        niaho();
    }

@Override
    public void before(Method method, Object[] args, Object target)
            throws Throwable {
        enen();
    }
    public void niaho(){
        System.out.println("调用前,你应该做的是");
    }
    public void enen(){
        System.out.println("调用后你应该做的事");
    }

}

package com.test.aop2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Tests {

public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop2.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("human");
        sleeper.sleep();
        }
}

AOP实现方式三

<?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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
            http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd     
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd    
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd     
              http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd 
              ">
<!--               自动扫描@Aspect注解 -->
        <aop:aspectj-autoproxy/>
        <context:component-scan base-package="com.test.aop3"/>
    
</beans>

package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {

@Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("睡觉啦,哈哈");
    }
}

package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public interface Sleepable {
    
    void sleep();
}

package com.test.aop3;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class SleepHelper{

public SleepHelper(){

}
    @Pointcut("execution(* *.sleep())")
    public void sleeppoint(){}

@Before("sleeppoint()")
    public void beforeSleep(){
        System.out.println("睡觉前需要脱衣服");
    }
    @After("sleeppoint()")
    public void aftersleep(){
        System.out.println("睡醒了要睁眼");
    }

}

package com.test.aop3;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Tests {

public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop3.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("human");
        sleeper.sleep();
        }
}

AOP实现方式四

<?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" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"

xsi:schemaLocation="
            http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd     
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd    
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd     
              http://www.springframework.org/schema/aop
               http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd 
              ">
<!--               自动扫描@Aspect注解 -->
        <context:component-scan base-package="com.test.aop4"/>
        
    <aop:config>
        <aop:aspect ref="SleepHelper">
            <aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>
            <aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>
        </aop:aspect>
    </aop:config>
    
</beans>

package com.test.aop4;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {
    @Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("睡觉啦,哈哈");
    }
}

package com.test.aop4;
import org.springframework.stereotype.Component;
@Component
public interface Sleepable {
    
    void sleep();
}

package com.test.aop3;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable {

@Override
    public void sleep() {
        // TODO Auto-generated method stub
        System.out.println("睡觉啦,哈哈");
    }
}

package com.test.aop4;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Tests {
    public static void main(String[] args){
        ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring/applicationContext-aop4.xml");
        Sleepable sleeper = (Sleepable)appCtx.getBean("human");
        sleeper.sleep();
        }
}

Spring-aop实现切面的四种方式 (2)的更多相关文章

  1. Spring-aop实现切面的四种方式(1)

    Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程 ...

  2. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  3. 【websocket】spring boot 集成 websocket 的四种方式

    集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...

  4. Spring AOP 中 advice 的四种类型 before after throwing advice around

    spring  AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...

  5. spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入

    三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...

  6. Spring通过构造方法注入的四种方式

    通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...

  7. Spring中配置数据源的四种方式

    1.spring自带的数据源 <bean id="dataSource" class="org.springframework.jdbc.datasource.Dr ...

  8. Spring事务管理的四种方式(以银行转账为例)

    Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败.   原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...

  9. 普通java类加入spring容器的四种方式

    今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...

随机推荐

  1. springboot+自定义注解实现灵活的切面配置

    利用aop我们可以实现业务代码与系统级服务例如日志记录.事务及安全相关业务的解耦,使我们的业务代码更加干净整洁. 最近在做数据权限方面的东西,考虑使用切面对用户访问进行拦截,进而确认用户是否对当前数据 ...

  2. 4、在Shell程序中的使用变量

    学习目标变量的赋值变量的访问变量的输入 12-4-1 变量的赋值在Shell编程中,所有的变量名都由字符串组成,并且不需要对变量进行声明.要赋值给一个变量,其格式如下:变量名=值.注意:等号(=)前后 ...

  3. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

  4. springboot入门记

    在idea中直接新建: 选择web: 在idea中启动“项目名称”Application中的main即可: 或者在项目目录下运行:mvn spring-boot:run: 或者: mvn instal ...

  5. jq访问网络接口实例

    最近需要在app生活频道上,需要添加一些类目,这就需要用到一些公用的开放接口,ajax其实调用并不复杂,但是结合jquery则显得更简洁一些,下面一起来看看jquery调用后台api. 代码如下: & ...

  6. Go语言备忘录(2):反射的原理与使用详解

    本文内容是本人对Go语言的反射原理与使用的备忘录,记录了关键的相关知识点,以供翻查. 文中如有错误的地方请大家指出,以免误导!转摘本文也请注明出处:Go语言备忘录(2):反射的原理与使用详解,多谢! ...

  7. 打开usb调试的方法

    方法一: settings --> about tablet --> build number(疯狂点击)  -->回退 developer options --> USB d ...

  8. 巧用CheckedTextView完成自定义radiobutton的listview

    因为要用自定义图片的radiobutton的listview,最开始想自己重新写BaseAdapter,重新定义BaseAdapter中的每个list的item.总之android提供了太多方便的控件 ...

  9. 在 Azure 上创建和链接 MySQL 数据库

    本快速入门介绍了如何使用 Azure 门户创建并连接 MySQL 数据库.在本教程中完成的所有操作均符合 1 元试用条件. 开始之前如果您还没有 Azure 账户,可以申请 1 元试用账户 步骤1:创 ...

  10. JqueryEasyUI $.Parser

    Parser(解析器) 对象的属性和方法: 使用: <link href="~/jquery-easyui-1.5.2/themes/bootstrap/easyui.css" ...