zoukankan      html  css  js  c++  java
  • Spring AOP的使用及案例

    一、什么是AOP

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

    二、为什么需要AOP 

      1.分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

      2.减少代码的重复,各个模块的重用性加强。

      3.降低 模块间的耦合度,提高代码的可操作性和可维护性。

    三、AOP原理

      将复杂的需求分解出不同方面,将散布在系统中的公共功能集中解决

      采用代理机制组装起来运行,在不改变原程序的基础上对代码段进行增强处理,增加新的功能。

      

    四、AOP相关术语

       1 增强处理类型

        前置增强:目标方法调用前

        后置增强:目标方法调用后

        环绕增强:前置+后置

        异常抛出增强:只有在目标方法抛出异常时才执行

        最终增强:finally

     

      2 AOP设计单元


    五、AOP案例

      1.创建实体类

        略;

      2.创建接口

        

    public interface IUserMapper {
        public int addUser(IUser iUser);
    }

      3.创建实现类

    public class IUserMapperImpl implements IUserMapper {
    
        @Override
        public int addUser(IUser iUser) {
            System.out.println("add success");
            return 0;
        }
    }

      4.创建切面类

    public class MyAdvice implements MethodBeforeAdvice {
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("前置增强");
        }
    }

      5.编写applicationContext.xml配置文件

    <!--声明MapperBean-->
        <bean id="iuserMapper" class="cn.spring.mapper.impl.IUserMapperImpl"></bean>
       <!--声明serviceBean-->
        <bean id="iuserService" class="cn.spring.service.impl.IUserServiceImpl">
            <property name="iUserMapper" ref="iuserMapper"></property>
        </bean>
    
    
        <!--切面:增强类-->
        <bean id="myAdvice" class="cn.spring.advice.MyAdvice"></bean>
        <!--增强处理-->
        <aop:config>
            <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"/>
            <!--织入:将增强处理和切点表达式符合的方法关联到一起-->
            <aop:advisor advice-ref="myAdvice" pointcut-ref="pointcut"></aop:advisor>
        </aop:config>

      6.编写测试类

     @Test
        public void test1(){
            ApplicationContext contet=new ClassPathXmlApplicationContext("applicationContext.xml");
            IUserService iuserService = (IUserService) contet.getBean("iuserService");
            iuserService.addUser(new IUser());
        }

     五、IOC与AOP交互扩展

       1 使用多种方式实现 IOC

        1.1 构造注入
          

     public Student(Integer stu_id, String stu_name) {
            this.stu_id = stu_id;
            this.stu_name = stu_name;
        }
    
        public Student(){
    
        }
     <!--基于构造注入-->
        <bean id="student" class="cn.spring.student.Student">
            <constructor-arg type="java.lang.Integer" value="15"></constructor-arg>
            <constructor-arg type="java.lang.String"  value="李四"></constructor-arg>
        </bean>

        1.2 p命名空间注入

    xmlns:p="http://www.springframework.org/schema/p"
    public class Student {
        private Integer stu_id;
        private String stu_name;
    
        public Integer getStu_id() {
            return stu_id;
        }
    
        public void setStu_id(Integer stu_id) {
            this.stu_id = stu_id;
        }
    
        public String getStu_name() {
            return stu_name;
        }
    
        public void setStu_name(String stu_name) {
            this.stu_name = stu_name;
        }
    
    }
    <!--p命名空间注入-->
        <bean id="student" class="cn.spring.student.Student" p:stu_id="78" p:stu_name="王五"></bean>
  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/szhhhh/p/11743420.html
Copyright © 2011-2022 走看看