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

转载于:https://www.cnblogs.com/zhabayi/p/6479791.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP (Aspect-Oriented Programming) 的四个主要切面通常包括:通知(Advice)、切入点(Pointcut)、连接点(Join Point)和切面(Aspect)。下面是它们的简要介绍: 1. **通知(Advice)** - 是AOP的核心,它是定义在切面中的行为,可以是前置通知(Before)、后置通知(After)、返回通知(After Returning)或异常通知(AfterThrowing)。通知可以在特定的连接点执行,如方法调用前后。 ```java @Before("execution(* com.example.service.*(..))") // 前置通知 public void beforeAdvice(JoinPoint joinPoint) { // 业务逻辑... } ``` 2. **切入点(Pointcut)** - 定义了通知应该何时执行,它描述了一个或者一组方法签名。Spring AOP使用Spring Expression Language (SpEL) 来编写表达式来匹配方法或类型。 3. **连接点(Join Point)** - 是AOP的核心概念,它是指程序执行过程中的一个特定时刻,比如方法调用、构造函数调用等。通知就是在这些连接点上插入的。 4. **切面(Aspect)** - 是一个模块化的、可重用的代码单元,它包含了一组相关的通知。切面由一个或多个通知组成,它们共享相同的切入点。 例如,一个简单的切面可能如下定义: ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*(..))") public void logBefore(JoinPoint joinPoint) { // 打印日志... } // 其他通知... } ``` 这里,`LoggingAspect`是一个切面,它定义了一个`logBefore`前置通知,会在`com.example.service`包下的所有方法执行之前被调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值