Spring-aop实现切面的四种方式 下篇 (源码)

来源:互联网 发布:java web 七牛云接口 编辑:程序博客网 时间:2024/04/30 11:42

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();
        }
}


1 0