AOP的三种开发方式 bean.xml 配置 、注解+配置 、全注解

第一章 Aop 切面编程的三种配置

使用aop切面编程的准配的jar

com.springsource.net.sf.cglib-2.1.3.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.2.jar
spring-aop-5.2.7.RELEASE.jar
junit-4.12.jar
hamcrest-core-1.3.jar
spring-aop-5.2.7.RELEASE.jar
spring-aspects-5.2.7.RELEASE.jar
spring-beans-5.2.7.RELEASE.jar
spring-context-5.2.7.RELEASE.jar
spring-core-5.2.7.RELEASE.jar
spring-expression-5.2.7.RELEASE.jar

 

第一种 bean.xml

1.1 创建 一个javaBean Book.class ,在dept类中 写入一个测试的方法 buy(),

package com.ntandon.aopxml;
​
public class Book {
    public  void buy(){
        System.out.println("buy最初的方法");
    }
}

1.2 再创建一个Book.class 的代理类(增强类)BookProxy.class 同样再类中也写一个方法 buy()

package com.ntandon.aopxml;
​
public class BookProxy {
    public  void buy(){
        System.out.println("buy增强的方法");
    }
}

1.3 配置bean.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
    <!--配置javaBean-->
    <bean id="book" class="com.ntandon.aopxml.Book"></bean>
    <bean id="bookProxy" class="com.ntandon.aopxml.BookProxy"></bean>
​
    <!--配置切入信息-->
    <aop:config>
        <!--配置被切入点-->
        <aop:pointcut id="p" expression="execution(* com.ntandon.aopxml.Book.buy(..))"/>
        <!--配置增强方法的类型,这里配置的是增强的before 在被增强的方法之前执行-->
        <aop:aspect ref="bookProxy">
            <aop:before method="buy" pointcut-ref="p"/>
        </aop:aspect>
    </aop:config>
</beans>

1.4 编写测试类

@Test
public  void buy(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
    Book book = context.getBean("book", Book.class);
    book.buy();
}

测试结果

 

第二种 注解+bean.xml

2.1 创建 一个javaBean Dept.class ,在dept类中 写入一个测试的方法 add(),


@Component 注解作用是将被标注的类加入javaBean 交给spring容器代理(或者说将被标注的类注册成组件)

@Component
public class Dept {
    public  void add(){
        System.out.println("被增强的方法..");
    }
}

2.2 再创建一个Dept.class 的代理类(增强类)DeptProxy.class 同样再类中也写一个方法 add();这里的方法测试了增强的几种类型

@Aspect 注解的作用告诉spring容器这是一个增强类,@Order(3) 注解告诉spring容器的加载优先级,数字越小优先级越高(先执行),数字为整型

package com.ntandon.aopanno;
​
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
​
@Component
@Aspect
@Order(3)
public class DeptProxy {
​
    /**
     * 公共切入点抽取
     */
    @Pointcut(value = "execution(* com.ntandon.aopanno.Dept.add(..))")
    public void pointCut(){}
​
    /**
     * 注意表达式的写法: execution(* com.ntandon.aopanno.Dept.add(..))
     * 在默认权限访问修饰符 * 要和包路经有空格才可以 否则会报方法传参的错误
     */
    @Before(value = "pointCut()")
    public void before() {
        System.out.println("before。。。。。");
    }
​
    /**
     * 被增强方法之后执行
     */
    @After(value = "execution(* com.ntandon.aopanno.Dept.add(..))")
    public void after() {
        System.out.println("after。。。。。");
    }
​
    /**
     * 返回通知
     */
    @AfterReturning(value = "execution(* com.ntandon.aopanno.Dept.add(..))")
    public void afterReturning() {
        System.out.println("afterReturning。。。。。");
    }
​
    /**
     * 后置通知
     */
    @AfterThrowing(value = "execution(* com.ntandon.aopanno.Dept.add(..))")
    public void afterThrowing() {
        System.out.println("AfterThrowing。。。。。");
    }
      /**
     * 环绕通知
     */
    @Around(value = "execution(* com.ntandon.aopanno.Dept.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("环绕之前");
        proceedingJoinPoint.proceed();
        System.out.println("环绕之后");
    }
}

2.3 再创建一个Dept.class 的代理类(增强类)PerSonProxy.class 同样再类中也写一个方法 add();这里的方法测试了增强的几种类型,编写PerSonProxy 主要是为了测试Order的优先级

package com.ntandon.aopanno;
​
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
​
@Component
@Aspect
@Order(1)
public class PerSonProxy {
    @Pointcut(value = ("execution(* com.ntandon.aopanno.Dept.add(..))"))
    public void pointCut(){
​
    }
    @Before(value =("pointCut()") )
    public void Before(){
        System.out.println("person  before 增强方法执行。。。");
    }
    @After(value = ("pointCut()"))
    public void after(){
        System.out.println("person after 增强方法执行。。。");
    }
​
    @Around(value = ("pointCut()"))
    public void Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
        System.out.println("person  Around 1增强方法执行。。。");
        proceedingJoinPoint.proceed();
        System.out.println("person  Around 2增强方法执行。。。");
    }
​
​
}

2.4 配置bean.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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">
​
    <!--指定扫描包的路径-->
    <context:component-scan base-package="com.ntandon.aopanno"></context:component-scan>
    <!--开启动态代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

2.5 编写测试代码(单元测试)

@org.junit.Test
public void test(){
    ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
    Dept dept = context.getBean("dept", Dept.class);
    dept.add();
​
}

测试结果

 

第三种 全注解开发AOP

全注解开发 相对第二种的 注解+bean.xml 不同的是,将bean的配置文件写成了配置类,以下只展示配置类和测试单元的代码

3.1 配置类的代码

package com.ntandon.config;
​
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
​
@Configuration   //开启配置
@Component  // 将当前类注册为组件类型
@ComponentScan(basePackages = {"com.ntandon"})  //设置包的扫描路径
@EnableAspectJAutoProxy(proxyTargetClass = true)  //开启aop代理
public class Config {
}

3.2 测试单元的代码

public class TestAopConfig {
    @Test
    public void test(){
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(Config.class);
        Dept dept = annotationConfigApplicationContext.getBean("dept", Dept.class);
        dept.add();
    }
}

因为当前的开发是全注解开发,将配置信息写在了配置类里,所以需要使用获取配置文件的方法变了

使用 new AnnotationConfigApplicationContext 或java 标注的Configuration的配置类

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值