11.Spring之AOP

11.Spring之AOP,第1张

11.Spring之AOP 11、AOP 11.1、什么是AOP

AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可用性,同时提高了开发的效率。

11.2、AOP在Spring中的作用

提供声明式事务;允许用户自定义切面

横切关注点:跨越应用程序多个模块的方法或者功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志,安全,缓存,事务等等…切面(ASPECT):横切关注点被模块化的特殊对象。即,它是一个类通知(Advice):切面必须要完成的工作。即,它是类中的一个方法目标(Target):被通知对象代理(Proxy):向目标对象应用通知之后创建的对象切入点(PointCut):切面通知执行的“地点”的定义连接点(JointPoint):与切入点匹配的执行点

SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:

即AOP在不改变原有代码的情况下,去增加新的功能

11.3、使用Spring实现AOP

【重点】使用 AOP织入,需要导入一个依赖包


    org.aspectj
    aspectjweaver
    1.9.7

方式一:使用Spring的API接口【主要SpringAPI接口实现】

方式二:自定义类实现AOP【主要是切面定义】

方式三:使用注解实现

代码show

代码结构图:

代码步骤:

1.创建新模块:spring-09-aop

2.diy包及类

public class DiyPointCut {
    void before() {
        System.out.println("===========方法执行前=========");
    }

    void after() {
        System.out.println("===========方法执行后=========");
    }
}
//方式三:使用注解方式实现aop
@Aspect//标注这个类是一个切面
public class AnnotationPointCut {
    @Before("execution(* com.gongyi.service.impl.UserServiceImpl.*(..))")
    public void before() {
        System.out.println("--------方法执行前-----------");
    }

    @After("execution(* com.gongyi.service.impl.UserServiceImpl.*(..))")
    public void after() {
        System.out.println("--------方法执行后-----------");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.gongyi.service.impl.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable {
        System.out.println("环绕前");
        Object proceed = jp.proceed();//执行方法
        System.out.println("环绕后");

        

    }
}

3.log包及类

public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    // target:目标对象
    @Override
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName() + "的" + method.getName() + "被执行了");
    }
}
public class AfterLog implements AfterReturningAdvice {
    //returnValue:返回值
    @Override
    public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
        System.out.println("执行了" + method.getName() + "方法,返回结果为:" + returnValue);
    }
}

4.service包及类

public interface UserService {
    void add();
    void delete();
    void update();
    void select();
}
public class UserServiceImpl implements UserService {
    @Override
    public void add() {
        System.out.println("增加了一个用户");

    }

    @Override
    public void delete() {
        System.out.println("删除了一个用户");
    }

    @Override
    public void update() {
        System.out.println("更新了一个用户");
    }

    @Override
    public void select() {
        System.out.println("查询了一个用户");
    }
}

5.资源包-applicationContext.xml




    
    
    
    

    
    
    
    

    
    
    
    
    





6.测试类:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //动态代理代理的是接口:注意点
        UserService userService = (UserService) context.getBean("userService");
        userService.add();
    }
}


代码地址

彩蛋

1.想到AOP就想到代理模式

2.导入AOP约束的方式

1)输入

2)在头部信息中拷贝beans相关的配置,改为aop

3.学习aop的目的

1)了解如何使用

2)面试时有谈资(从AOP讲到动态代理)

4.aspect在idea中的图标

问题记录

1.warning no match for this type name

警告: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: com.gongyi.serivice.UserServiceImpl [Xlint:invalidAbsoluteTypeName]
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: warning no match for this type name: com.gongyi.serivice.UserServiceImpl [Xlint:invalidAbsoluteTypeName]

解决:【和老师schema改一致,老师的是复制beans修改的,我的是alt+enter导入的】

对比老师代码,发现schema导入错了

我的:

老师的:

2.Caused by: org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 17; cvc-complex-type.2.4.c: 通配符的匹配很全面, 但无法找到元素 ‘aop:config’ 的声明。

解决:

https://www.springframework.org/schema/beans/spring-aop.xsd

改为:

https://www.springframework.org/schema/aop/spring-aop.xsd

3.execution中的UserServiceImplements不能点击进去,也不变色

解决:复制进去好了,不要手敲,在notepad++等ide中写好,复制到idea中即可

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5708876.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存