• 0809 aop


    aop思想: 横向重复 纵向抽取

    连接点(Joinpoint):service层的增删改查方法,即将给事务配的方法

    切入点(Pointcut):已经配上的方法  

    通知/增强(Advice):方法执行以前调的方法

    目标对象(Target):哪个类里加增强代码

    植入(Weaving):将开启、提交事务配在这个方法的过程

    代理(Proxy):用代理去调被加强的方法

    切面(aspect):切入点和通知的结合

    步骤

    先导包

    注意

    3.0几乎是java所有的包  4.2里面是Spring专用的包

     新建 接口  Service 并封装 

    package com.oracle.service;
    
    public interface UserService {
    //定义4个抽象方法 增删改查
    public abstract void get(); public abstract void update(); public abstract void insert(); public abstract void delete(); }

    新建个实现类 UserServiceImp

     

    package com.oracle.service;
    //定义一个接口并实现
    public class UserServiceImp implements UserService{
        @Override
        public void get() {
            System.out.println("查询用户");        
        }
    
        @Override
        public void update() {
            System.out.println("修改用户 ");
        }
    
        @Override
        public void insert() {
            System.out.println("新增用户");
        }
    
        @Override
        public void delete() {
            System.out.println("删除用户");
        }    
    }

     new 一个 通知类 advice ,定义为 MyAdvice

    package com.oracle.advice;
    import org.aspectj.lang.ProceedingJoinPoint;
    //新建通知类
    public class MyAdvice {
    //    前置通知
        public void before(){
            System.out.println("这是前置通知");
        }
    //    后置通知
        public void afterReturning(){
            System.out.println("这是后置通知");
        }
    //    环绕通知 返回值是个obj类型
        public Object around(ProceedingJoinPoint pjp) throws Throwable{
            System.out.println("环绕通知之前调用");
            Object obj=pjp.proceed();
            System.out.println("环绕之后调用");
            return pjp;        
        }
    //    异常通知
        public void afterException(){
            System.out.println("异常后调用 ");
        }
    //    后置通知(不管是否异常都调用 )
        public void after(){
            System.out.println("后置通知目标方法执行后调用");
        }
    }

    然后将它们联系起来 配置一下applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd ">
        <!-- 配置注解扫描 -->
        <context:component-scan base-package="com.oracle.pojo"></context:component-scan>
        <bean name="car2" class="com.oracle.pojo.Car"></bean>
        <!-- 配置目标对象 -->
        <bean name="userService" class="com.oracle.service.UserServiceImp"></bean>
        <!-- 配置通知对象 -->
        <bean name="myadvice" class="com.oracle.advice.MyAdvice"></bean>
        <aop:config>
        <!--配置切入点
            public void com.oracle.service.UserServiceImp.save()
            void com.oracle.service.UserServiceImp.save()
            * com.oracle.service.UserServiceImp.save()
            * com.oracle.service.UserServiceImp.*()
            
            * com.oracle.service.*ServiceImp.*(..)                
          -->
          <aop:pointcut expression="execution(* com.oracle.service.*ServiceImp.*(..))" id="pc"/>
          <aop:aspect ref="myadvice">
                  <!--指定名为before的方法作为前置通知  -->
                  <aop:before method="before" pointcut-ref="pc"/>
                  <!--后置  -->
                  <aop:after-returning method="afterReturning" pointcut-ref="pc"/>
                  <!--环绕通知  -->
                  <aop:around method="around" pointcut-ref="pc"/>
                  <!--异常拦截通知  -->
                  <aop:after-throwing method="afterException" pointcut-ref="pc"/>
                  <!--后置  -->
                  <aop:after method="after" pointcut-ref="pc"/>          
          </aop:aspect>
    </aop:config>
    </beans>

    最后建个demo测试

    package com.oracle.test;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.oracle.service.UserService;
    //@表示注解,将约束注解到容器
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:applicationContext.xml")
    public class Demo01 {
        @Autowired
        private UserService userService;
        @Test
        public void get(){
            userService.get();
        }
    }

    运行结果

     PM

    .

  • 相关阅读:
    5、Android Service测试
    javassist示例
    HeaderExchangeClient
    dubbo 心跳
    javassist和jdk动态代理
    dubbo为consumer创建代理
    线程同步知识点
    SynchronousQueue类
    Executors类的newFixedThreadPool, newCachedThreadPool, newScheduledThreadPool
    eclipse设置条件断点
  • 原文地址:https://www.cnblogs.com/zs0322/p/11326413.html
Copyright © 2020-2023  润新知