Spring-aop实现切面的四种方式 (2)
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)的更多相关文章
- Spring-aop实现切面的四种方式(1)
		
Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程 ...
 - Spring中依赖注入的四种方式
		
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...
 - 【websocket】spring boot 集成 websocket 的四种方式
		
集成 websocket 的四种方案 1. 原生注解 pom.xml <dependency> <groupId>org.springframework.boot</gr ...
 - Spring AOP 中 advice 的四种类型 before after throwing advice around
		
spring AOP(Aspect-oriented programming) 是用于切面编程,简单的来说:AOP相当于一个拦截器,去拦截一些处理,例如:当一个方法执行的时候,Spring 能够拦截 ...
 - spring的依赖注入的四种方式,数组与集合注入;引用注入;内部bean注入
		
三种注入方式 第一种: 基于构造函数 hi.java (bean) package test_one; public class hi { private String name; public hi ...
 - Spring通过构造方法注入的四种方式
		
通过构造方法注入,就相当于给构造方法的参数传值 set注入的缺点是无法清晰表达哪些属性是必须的,哪些是可选 的,构造注入的优势是通过构造强制依赖关系,不可能实例化不 完全的或无法使用的bean. Me ...
 - Spring中配置数据源的四种方式
		
1.spring自带的数据源 <bean id="dataSource" class="org.springframework.jdbc.datasource.Dr ...
 - Spring事务管理的四种方式(以银行转账为例)
		
Spring事务管理的四种方式(以银行转账为例) 一.事务的作用 将若干的数据库操作作为一个整体控制,一起成功或一起失败. 原子性:指事务是一个不可分割的工作单位,事务中的操作要么都发生,要么都不 ...
 - 普通java类加入spring容器的四种方式
		
今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...
 
随机推荐
- TSM_ISSUE_123
			
dsmc 命令详解http://www-ik.fzk.de/~apel/html/adsm_manual.htmlTSM InfoCenterhttp://www-01.ibm.com/support ...
 - selenium+junit4实现参数化自动化测试
			
业务场景:在www.1905.com电影网中实现两个用户的登陆操作. 代码如下: package com.m1905.junit; import java.util.Arrays; import ja ...
 - mac下 mysql 插入中文乱码解决
			
搞了好几个小时,终于搞定了 乱码根本原因还是编码方式不同造成的,只要编码方式统一就没问题 1. 进入 mysql 命令行, 用 show variables like 'character_set_% ...
 - 手动修改user-agent
			
1. 在浏览器地址栏输入 about:config.弹出对话框:
 - bzoj 4540: [Hnoi2016]序列
			
Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称 ...
 - [转]Oracle 初始化参数之cursor_sharing
			
本文转自:http://www.cnblogs.com/Richardzhu/archive/2013/01/21/2869837.html 一.Cursor_sharing简介: 这个参数是用来告诉 ...
 - CentOS7 一键安装KMS服务【整理】
			
KMS,是 Key Management System 的缩写,也就是密钥管理系统.这里所说的 KMS,毋庸置疑就是用来激活 VOL 版本的 Windows 和 Office 的 KMS 啦.经常能在 ...
 - javascript下的arguments,caller,callee,call,apply示例及理解
			
(参考:http://justcoding.iteye.com/blog/589111) Arguments 该对象代表正在执行的函数和调用它的函数的参数. [function.]arguments ...
 - Gradle 学习(一)
			
引入插件 apply plugin: 'java' apply plugin: 'war' apply plugin: 'jetty' 如果希望使用jar来启动项目, 可以这样修改项目和插件属性. a ...
 - C#项目””是针对”.NETFramework,Version=v4.5.2”但此计算机没有,需要修改为v4.5.1.
			
每次下载别人的代码都会出现这样的问题,以为是没有安装.NETFramework,就下载安装了,但是每次安装都会出现已安装高版本的4.6(Win10自带),无需下次安装,但是每次VS中都会显示有问题,而 ...