zoukankan      html  css  js  c++  java
  • Spring框架AOP使用扩展

    一、什么是AOP

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

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

      3、默认采取的是JDK动态代理,面向接口编程的动态代理。

    二、案例

      接下来我们通过具体案例对AOP进一步了解

        1、创建UserInfo实体类 

    package com.spring.entity;
    
    public class UserInfo {
        private Integer u_id;
        private String u_name;
    
        public Integer getU_id() {
            return u_id;
        }
    
        public void setU_id(Integer u_id) {
            this.u_id = u_id;
        }
    
        public String getU_name() {
            return u_name;
        }
    
        public void setU_name(String u_name) {
            this.u_name = u_name;
        }
    }

      2、创建IUserMapper接口,定义一个添加方法

    package com.spring.mapper;
    
    import com.spring.entity.UserInfo;
    
    /**
     * Dao层接口
     */
    public interface IUserMapper {
        //添加用户
        public int addUser(UserInfo info);
    }

       创建IUserMapperImpl实现层

    package com.spring.mapper.impl;
    
    import com.spring.entity.UserInfo;
    import com.spring.mapper.IUserMapper;
    
    public class IUserMapperImpl implements IUserMapper {
        @Override
        public int addUser(UserInfo info) {
            System.out.println("add success");
            return 0;
        }
    }

      3、创建相对应的IUserMapperService接口,创建相同的方法

    package com.spring.service;
    
    import com.spring.entity.UserInfo;
    
    public interface IUserMapperService {
        //添加用户
        public int addUser(UserInfo info);
    }

      4、创建Service的实现类IUserMapperServiceImpl类

    package com.spring.service.impl;
    
    import com.spring.entity.UserInfo;
    import com.spring.mapper.IUserMapper;
    import com.spring.service.IUserMapperService;
    
    public class IUserMapperServiceImpl implements IUserMapperService {
       //注入Dao层对象
        private IUserMapper iUserMapper;
    
        public IUserMapper getiUserMapper() {
            return iUserMapper;
        }
    
        public void setiUserMapper(IUserMapper iUserMapper) {
            this.iUserMapper = iUserMapper;
        }
    
        @Override
        public int addUser(UserInfo info) {
            int count = iUserMapper.addUser(info);
            return count;
        }
    }

      5、在pom.xml中注入实现AOP的依赖

    <dependency>
       <groupId>org.aspectj</groupId>
       <artifactId>aspectjweaver</artifactId>
       <version>1.7.0</version>
    </dependency>

      6、创建advice增强类MyBeforeAdvice

        //MethodBeforeAdvice表示前置增强,在执行逻辑代码之前执行的操作
        //AfterReturningAdvice表示后置增强,在逻辑代码执行完毕之后进行的操作
    package com.spring.advice;
    
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    public class MyBeforeAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
        
        @Override
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("==============前置增强,日志处理等操作===============");
        }
    
        @Override
        public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {
            System.out.println("==============后置操作,事务处理等操作================");
        }
    }

      7、配置applicationContextUser.xml文件

    <?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"
           xmlns:p="http://www.springframework.org/schema/p"
    
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
    
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">
    
       <!--声明DaoBean  bean的注入的都是实现类-->
        <bean id="iUserMapper" class="com.spring.mapper.impl.IUserMapperImpl"></bean>
        <!--声明Service      class:对应的是你的Service实现类-->
        <bean id="iUserMapperService" class="com.spring.service.impl.IUserMapperServiceImpl">
            <!--setter方法怎么注入:找到Name属性值,将属性值的开头改为大写,然后前缀加上setIUserMapper-->
            <property name="iUserMapper" ref="iUserMapper"></property>
         </bean>
    
        <!--切面:对应增强类-->
        <bean id="myBeforeAdvice" class="com.spring.advice.MyBeforeAdvice"></bean>
        
        <!--增强处理-->
        <aop:config>
            <!--切点:你要对哪一个方法进行增强     -->
            <aop:pointcut id="pointcut" expression="execution(* *..service.*.*(..))"/>
    
            <!--织入:将增强处理和切点表达式符合的方法关联到一起-->
            <aop:advisor advice-ref="myBeforeAdvice" pointcut-ref="pointcut"></aop:advisor>
        </aop:config>
    </beans>

      8、Test测试类 

    package com.spring.Aop;
    
    import com.spring.entity.Student;
    import com.spring.entity.UserInfo;
    import com.spring.service.IUserMapperService;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class UserTest {
        public static void main(String[] args) {
            //加载applicationContext.xml文件
            ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContextUser.xml");
            IUserMapperService user = (IUserMapperService)cxt.getBean("iUserMapperService");
            user.addUser(new UserInfo());
        }
    }

      9、执行结果如下

        

        

        

  • 相关阅读:
    io学习
    asp.net文件上传进度条研究
    asp.net页面中的Console.WriteLine结果如何查看
    谨慎跟随初始目的不被关联问题带偏
    android 按钮特效 波纹 Android button effects ripple
    安卓工作室 日志设置
    安卓工作室 文件浏览器 android studio File browser
    一个新的Android Studio 2.3.3可以在稳定的频道中使用。A new Android Studio 2.3.3 is available in the stable channel.
    新巴巴运动网上商城 项目 快速搭建 教程 The new babar sports online mall project quickly builds a tutorial
    码云,git使用 教程-便签
  • 原文地址:https://www.cnblogs.com/tinghao/p/11747108.html
Copyright © 2011-2022 走看看