Alex_TD

Spring---面向切面编程(AOP)

IOC  AOP  Spring    不管什么的学习都要先搭建环境  先搭建环境 先搭建环境Spring 对JDK代理 和CGlib代理都支持,先JDK代理,后CGLIB代理

  1.导入依赖  导入依赖

<dependency>
<groupId>aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>

1.aop在spring中的作用:代理对象    被代理接口的实现类      代理的方法   方法执行前 后   即前又后     

  1.AOP在spring中的API:  *Advise()  增加的功能

       第一种:实现接口   对象的方法的拦截功能

        1.创建拦截功能:创建要在代理对象方法之前 之后   之前和之后  要增加的功能  继承 **Advise后缀的接口

        2.创建接口的实现类:

        3.创建XML配置文件: 

                1.增加支持AOP的头部 约束 链接。

                2.增加实现类的bean 

        4.在XML配置中配置AOP:

                

<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
         https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="users" class="com.moon.pojo.UserService"/>
    <bean id="after" class="com.moon.pojo.Log"/>
<!--    <bean id="before" class="com.moon.pojo.UserBeforeAdvise"/>-->
    <aop:config>
        //定义一个带拦截的位置 ,表达式扩展 <aop:pointcut id="p" expression="execution(* com.moon.pojo.UserService.*(..))"/>
        //将增强方法 应用到拦截位置。 <aop:advisor advice-ref="after" pointcut-ref="p"/> </aop:config>
</beans>

  

切记:  搭建环境  导入必要的包        

               

 

        

     

posted on 2020-02-20 18:10  Alex_TD  阅读(118)  评论(0编辑  收藏  举报