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

Spring-AOP

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

Spring-AOP

Spring-AOP

介绍AOPAOP的代理方式AOP的使用方式AOP的通知

前置通知

xml版注解版 后置通知

xml版注解版 后置返回增强

xml版注解版 环绕增强

xml版注解版 后置异常增强

xml版注解版:

介绍AOP

是一种面向切面的编程,不需要再方法中修改代码,就可以增加功能。主要目的也是解耦。面向切面编程是将程序抽象成各个切面,即解剖对象的内部,将那些影响了多个类的公共行为抽取到一个可重用模块里,减少系统的重复代码,降低模块间的耦合度,增强代码的可操作性和可维护性。

切面(Aspect):切面的官万抽象定义为“一个天注点的模块化,这个关注点可能会横切多个对象”。切面是由Applicat ionContext中的< aop : aspect>来配置。连接点(JoinPoint):连接点是指程序执行过程中的某一行为,例如MemberService. get的调用或者MemberService. delete拋出异常等行为。程序执行过程中明确的点,如方法的调用。通知(Advice):通知是指切面对于某个连接点所产生的动作。其中,一个切面可以包含多个通知。切入点(Pointcut): 切入点是指匹配连接点的断言,在A0P中通知和一一个切入点表达式关联。切面中的所有通知所关注的连接点都由切入点表达式决定。目标对象(Target Object): 目标对象是指被一个或者多个切面所通知的对象。

AOP的代理方式

AOP代理(AOP Proxy):在Spring AOP中有两种代理方式:

JDK动态代理和CGLib代理。默认情况下,目标对象实现了接口时,采用JDK动态代理采用CGLib代理强制使用CGLib代理需要将< aop :conf ig>的proxy- target-class属性设置为true。 AOP的使用方式

第一步是在xml文件中激活自动扫描组建功能,同时激活自动代理功能第二步是为切面添加注解 AOP的通知


通知分为前置通知和后置通知

前置通知

是指在某个连接点前执行,并不阻止连接点前的代码执行

xml版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

前置增强类:

//切面
public class BeforeAdvice {
    //前置增强
    public void plan(){
        System.out.println("前置增强---Plan!");
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("be.xml");
        Person chinese=ctx.getBean("chinese",Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

   
    

    
    
    
        

            

        
        
    
注解版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
@Component
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

前置增强类:

//切面
@Component
@Aspect
public class BeforeAdvice {
    //前置增强
    @Before(value = "execution(* com.weikun.f.*.*(..))")
    public void plan(){
        System.out.println("前置增强FFFF---Plan!");
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bf.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

    
        
        
    
    
    
后置通知 xml版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

后置增强类:

//切面
public class AfterAdvice {
    //后置增强
    public void plan(){
        System.out.println("后置增强---Plan!");
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bg.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml配置文档:

    
    

    
        
        
            
        
    
注解版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
@Component
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

后置增强类:

//切面
@Component
@Aspect
public class AfterAdvice {
    //后置增强
    @After(value = "execution(* com.weikun.h.*.*(..))")
    public void plan(){
        System.out.println("后置增强HHHHH---Plan!");
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bh.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml配置文档:

    
    

    
    

    
        
            
        
    
    
        
    
    
    
后置返回增强 xml版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

后置返回增强:

//切面
public class AfterReturnAdvice {
    //后置返回增强
    public void plan(Object re){

        System.out.println("后置返回增强---Plan!"+re);
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bi.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml配置文档:

    
    

    
    

    
        
            
        
    
注解版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

@Component
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

后置返回增强类:

@Component
@Aspect
public class AfterReturnAdvice {
    //后置返回增强
    @AfterReturning(returning ="re",pointcut = "execution(* com.weikun.j.*.*(..))")
    public void plan(Object re){
        System.out.println("后置返回增强---Plan!"+re);
    }


    @After(value = "execution(* com.weikun.j.*.*(..))")
    public void make(){
        System.out.println("后置增强---Make!");
    }

    @Before(value="execution(* com.weikun.j.*.*(..))")
    public void bark(){
        System.out.println("前置增强---Bark!");
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bj.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

    
        
    
    
    
环绕增强 xml版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");

    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

环绕增强类:

//切面
public class ArroundAdvice {
    //环绕增强
    public void plan(ProceedingJoinPoint joinPoint){

        try {
            System.out.println("前置环绕增强---Plan!");
            Object o=joinPoint.proceed();//调用连接点
            System.out.println("后置环绕增强---Plan!"+o);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bk.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

    
    

    
    

    
        
            
        
    
注解版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
@Component
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {
        System.out.println("TO!");
        return "中国!";
    }
}

环绕增强类:

//切面
@Component
@Aspect
public class ArroundAdvice {
    //后置返回增强
    @AfterReturning(returning ="re",pointcut = "execution(* com.weikun.l.*.*(..))")
    public void plan(Object re){
        System.out.println("后置返回增强---Plan!"+re);
    }


    @After(value = "execution(* com.weikun.l.*.*(..))")
    public void make(){
        System.out.println("后置增强---Make!");
    }

    @Before(value="execution(* com.weikun.l.*.*(..))")
    public void bark(){
        System.out.println("前置增强---Bark!");
    }

    @Around(value = "execution(* com.weikun.l.*.*(..))")
    public void plan(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("前置环绕增强---Plan!");
            Object o=joinPoint.proceed();//调用连接点
            System.out.println("后置环绕增强---Plan!"+o);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bl.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

    
        
    
    
    
后置异常增强 xml版

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {

        System.out.println("TO!"+1/0);


        return "中国!";
    }
}

后置异常类:

    //后置异常增强
    public void plan(Throwable tr){

        System.out.println("后置抛出异常"+tr.getMessage());

    }

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bm.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

    
    

    
    

    
        
            
        
    
注解版:

Person接口:

public interface Person {
    public void go();
    public String to();
}

Chinese类:

//目标类
@Component
public class Chinese implements Person {

    //连接点
    public void go() {
        System.out.println("GO!");
    }
    //连接点
    public String to() {

        System.out.println("TO!"+1/0);


        return "中国!";
    }
}

后置异常类:

//切面
@Component
@Aspect
public class AfterThrowAdvice {
    //后置异常增强
    @AfterThrowing(throwing = "tr",value = "execution(* com.weikun.n.*.*(..))")
    public void plan(Throwable tr){

        System.out.println("后置抛出异常"+tr.getMessage());

    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("bn.xml");
        Person chinese=ctx.getBean("chinese", Person.class);
        chinese.go();
        chinese.to();

    }
}

xml文档:

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

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

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