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. mongodb3.4 远程连接认证失败

    mongodb开启或者关闭授权功能时还是挺麻烦的,需要新建服务键入mongod --auth.为了方便,我这里是建了两个服务,用到哪个就切换至哪个服务. --需要授权 mongod --logpath ...

  2. 【c++】类中带默认参数的函数

    反思两个问题 1. 带默认参数的函数,为何声明.定义不能同时有参数? 2. 带默认参数的函数, 为何带默认参数的参数靠后站? 上程序 #include <iostream> #includ ...

  3. awk常用命令总结

    awk工具,主要将一行分成“字段”来处理. awk '条件类型1{动作1} 条件类型2{动作2}...‘ filename awk主要是处理每一行的字段内的数据,而默认的字段的分隔符为空格键或[tab ...

  4. JavaScript对象中的constructor属性

    constructor属性始终指向创建当前对象的构造函数. 比如下面的例子: // 等价于 var foo = new Array(1, 56, 34, 12); var arr = [1, 56, ...

  5. C#基础 (一)

    值类型和引用类型 堆和栈 栈存放的数据: (1)某些类型变量的值(2)程序当前的执行环境(3)传递给方法的参数 堆是存放对象的地方 对象类型有两种: 值类型和引用类型,他们的存储方式不同值类型: 只需 ...

  6. axios发送post请求后台接受不到问题

    axios发送post请求后台接受不到问题 1.首先这是前端的问题 2.解决方案不唯一,但这招肯定行 <!DOCTYPE html> <html> <head> & ...

  7. flask_sqlalchemy filter 和filter_by的区别

    1. filter需要通过类名.属性名的方式,类名.属性名==值.filter_by 直接使用属性名=值,可以看源码filter_by需要传一个 **kwargs 2. filter支持> &l ...

  8. so模块加载后数据问题

    lualib-src里面都没有存数据的地方 那么bjm里面的这块数据防全局,再多个虚拟机里require后数据会全局共享吗

  9. JAVA SwingWorkder的使用例

    最近在学习Swing,我们都知道在UI表现线程里面长时间执行操作时,画面会假死,为了能够让费时操作不影响画面表现,就需要用多线程了.首先考虑的就是Swing内部的 SwingWorkder对象,但是网 ...

  10. 洛谷P1024 一元三次方程求解(数学)

    题意 题目链接 Sol 本来是一道好的公式题. 然后输出只要保留两位小数?? 直接上不就赢了嘛.. #include<bits/stdc++.h> #define LL long long ...