栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中的AOP技术Demo的使用(注解和非注解形式实现)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中的AOP技术Demo的使用(注解和非注解形式实现)

目录

1、引入核心依赖

2、给定一个房东类

3、中介类

4.1、基于XML配置形式实现

4.2、基于注解形式实现AOP操作

5、目录结构展示


AOP(Aspect Oriented Programming),面向切面编程,通过预编译方式和运行期间动态代理实现实现在不修改源代码的情况下给程序动态统一添加某种特定功能的一种技术。

利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

以模拟租房业务来讲解AOP技术的两种实现形式

AOP的实现有两种形式:
基于非注解(XML配置)形式实现
基于注解形式实现

1、引入核心依赖


除了AOP依赖,还需要将基础核心依赖引入 

在pom.xml中引入


        UTF-8
        1.7
        1.7
        1.0.0
        
        4.1.7.RELEASE
     

        
            org.springframework
            spring-aop
           ${spring.propety}
        
        
            org.aspectj
            aspectjweaver
            1.7.4
        
        
            aopalliance
            aopalliance
            1.0
        

     
        
            org.springframework
            spring-beans
            ${spring.propety}
        

        
            org.springframework
            spring-core
            ${spring.propety}
        

        
            org.springframework
            spring-context
            ${spring.propety}
        
        
            org.springframework
            spring-expression
            ${spring.propety}
        


2、给定一个房东类
public class Landlord {
    public void service() {
        System.out.println("签合同");
        System.out.println("收钱");
    }
}


3、中介类
public class Broker {

    public void  service1(){
        System.out.println("看房子");
    }

    public void  service2(){
        System.out.println("谈价钱");
    }

    public void  service3(){
        System.out.println("给钥匙");
    }

}


4.1、基于XML配置形式实现

在XML引入AOP的约束(在资源文件夹下面新建一个配置文件AOPSpringContext3.xml,将下面代码放进去)






execution函数介绍


在通知中通过value属性定义切点,通过execution函数,可以定义切点的方法切入
1、切入点:实际增强的方法
2、常用的表达式
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)
(1)execution(* com.tulun.bean.Book.show(..)) 表类里面的某一个方法
(2)execution(* com.tulun.bean.Book.*(..)) 表类某个包里类所有方法
(3)execution(* *.*(..)) 表示所有

例:
-匹配所有类public方法 execution(public *.*(..))
-匹配指定包下所有类方法 execution(* com.tulun.bean.*(..)) (不包含子包)
- execution(* com.tulun.bean..*(..)) (包含包、子包下所有类)
-匹配指定类所有方法 execution(* com.tulun.bean.Book.*(..))
-匹配实现特定接口所有类方法 execution(* com.tulun.bean.Book+.*(..))
-匹配所有com开头的方法 execution(* com*(..))
 

xml配置完整代码:




    
    
    

    
    
        
        

        
        
            
            
        

    


    
    
    
    
    
    
    
    

    
    






增强类型:

aop:around

aop:before

aop:after-throwing

aop:after

aop:after-returning



测试使用:测试类AOPTest

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("AOPSpringContext3.xml");
        Landlord landlord = (Landlord)context.getBean("landlord");
        landlord.service();
        System.out.println(landlord);
    }
}

执行结果:

看房子

签合同

收钱




各种类型增强如下:

            
            
            
            
            
            
            

            
            



4.2、基于注解形式实现AOP操作


在xml配置文件中(AOPSpringContext4.xml)开启扫描注解,开启AOP操作





    
    

    
    


在增强类上添加注解

import org.springframework.stereotype.Component;


//将房东类交给容器进行管理也需要添加注解
@Component
public class Landlord {
    public void service() {
        System.out.println("签合同");
        System.out.println("收钱");
    }
}
@Component
@Aspect
//当前类开启AOP操作
public class Broker {

    //前置增强注解,@Before
    @Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
    public void  service1(){
        System.out.println("看房子");
    }
}

@Aspect 注解添加在类上,表示当前类是增强类
@Before注解添加在方法上,表示前置增强
@Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")


关于增强注解:

//前置增强注解,@Before
    @Before(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
    //后置增强
    @After(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")
//最终增强
    @AfterReturning(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))") 
    @AfterThrowing(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))") //异常增强
    @Around(value = "execution(* com.tulun.Spring.AOP.Landlord.service(..))")//环绕增强

5、目录结构展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/592572.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号