AOP之基于@Aspect 注解与Schema(xml)区别

基于@Aspect 注解

基于@Aspect 注解的AOP开发主要需要目标类、切面、配置文件。
调用关系如图:
这里写图片描述

首先引入jar包:
下载jar
目标类(HelloWorld):

package com.demo.spring.aop;

public class Helloworld {
    protected String message;//定义String 类型变量

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void execute(){
        System.out.println("Hello"+getMessage()+"!");
    }

}

切面:

package com.demo.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

    /**
     * 定义切入点函数
     */
    @Pointcut("execution(* com.demo.spring.aop.*.execute*(..))")
    void pointcut(){

    }
    /**
     * 使用@Before声明前置通知
     * @param thisJoinPoint
     */
    @Before("pointcut()")
    public void beforeExecute(JoinPoint thisJoinPoint){
        System.out.println("start to say:");
    }
    /**
     * 使用@Around 注解声明环绕通知
     * @param thisJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
        System.out.println("before 之前");
        Object va=thisJoinPoint.proceed();
        System.out.println("after 之前");
        return va;
    }
    /**
     * 使用@After 注解声明后置通知
     * @param thisJoinPoint
     */
    @After("pointcut()")
    public void afterExecute(JoinPoint thisJoinPoint){
        System.out.print("end!");
    }
}

applicationContext.xml配置文件:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <bean id="Helloworld" class="com.demo.spring.aop.Helloworld">
        <property name="message">
            <value>World</value>    
        </property>
    </bean>
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    <bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean>

</beans>

客户端调用:
package com.demo.spring.aop;

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

public class Test {

public static void main(String[] args) {
    ApplicationContext ctx=new FileSystemXmlApplicationContext("WebRoot/WEB-INF/applicationContext.xml");
    Helloworld hello=(Helloworld) ctx.getBean("Helloworld");
    hello.execute();
}

}
这中方式主要是用Spring的注解方式开发AOP,还有一种是通过Schema(xml)的方式

基于Schema(xml)

与@Aspect注解相比不同之处主要在切面类与配置文件上,切面类不再用注解,而是在配置文件中配置增强
切面类:

package com.demo.spring.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;

/**
 * 切面类
 * @author dandan
 *
 */
@Aspect
public class MyAspect {
    /**
     * 前置通知
     * @param thisJoinPoint
     */
    public void beforeExecute(JoinPoint thisJoinPoint){
        System.out.println("start to say:");
    }
    /**
     * 环绕通知类
     * @param thisJoinPoint
     * @return
     * @throws Throwable
     */
    public Object userOperate(ProceedingJoinPoint thisJoinPoint) throws Throwable{
        return thisJoinPoint.proceed();
    }
    /**
     * 后置通知类
     * @param thisJoinPoint
     */
    public void  afterExecute(JoinPoint thisJoinPoint){
        System.out.println("end!");
    }
}

配置文件:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    <bean id="HelloWorld" class="com.demo.spring.aop.HelloWorld">
        <property name="message">
            <value>World</value>
        </property>
    </bean>
    <bean id="myAspect" class="com.demo.spring.aop.MyAspect"></bean>
    <aop:config>
        <!-- 定义切面 -->
        <aop:aspect id="aopAspect" ref="myAspect">
            <!-- 定义切点 -->
            <aop:pointcut id="pointcut" 
                    expression="execution(* execute(..)) and target(com.demo.spring.aop.HelloWorld)"/>
            <!-- 定义通知 -->
            <aop:before pointcut-ref="pointcut" method="beforeExecute"/>
            <aop:around pointcut-ref="pointcut" method="userOperate"/>
            <aop:after  pointcut-ref="pointcut" method="afterExecute"/>
        </aop:aspect>
    </aop:config>
</beans>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值