zoukankan      html  css  js  c++  java
  • SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------

    名称自动代理生成器:BeanNameAutoProxyCreator

    为了更好的测试,我放了俩个接口,俩个实现类:

      ISomeService接口:

    package cn.dawn.day18auto02;
    
    /**
     * Created by Dawn on 2018/3/8.
     */
    public interface ISomeService {
        public void insert();
        public void delete();
        public void select();
        public void update();
    }

      它的实现类:SomeServiceImpl

    package cn.dawn.day18auto02;
    
    
    /**
     * Created by Dawn on 2018/3/8.
     */
    public class SomeServiceImpl implements ISomeService {
    
        public void insert() {
            System.out.println("insert ok");
        }
    
        public void delete() {
            System.out.println("delete ok");
    
        }
    
        public void select() {
            System.out.println("select ok");
    
        }
    
        public void update() {
            System.out.println("update ok");
    
        }
    }

      IBookService接口:

    package cn.dawn.day18auto02;
    
    /**
     * Created by Dawn on 2018/3/12.
     */
    public interface IBookService {
        public void selectAllBooks();
    }

      它的实现类:BookServiceImpl

    package cn.dawn.day18auto02;
    
    /**
     * Created by Dawn on 2018/3/12.
     */
    public class BookServiceImpl implements IBookService {
        public void selectAllBooks() {
            System.out.println("selectbooks ok");
        }
    }

      一个增强的

    package cn.dawn.day18auto02;
    
    
    import org.springframework.aop.MethodBeforeAdvice;
    
    import java.lang.reflect.Method;
    
    /**
     * Created by Dawn on 2018/3/5.
     */
    /*前置增强*/
    public class LoggerBefore implements MethodBeforeAdvice {
        public void before(Method method, Object[] objects, Object o) throws Throwable {
            System.out.println("日志记录");
        }
    }

      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" xmlns:context="http://www.springframework.org/schema/context"
           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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        <!--要增强的对象-->
        <bean id="service" class="cn.dawn.day18auto02.SomeServiceImpl"></bean>
        <bean id="bookservice" class="cn.dawn.day18auto02.BookServiceImpl"></bean>
        <!--增强的内容-->
        <bean id="myadvice" class="cn.dawn.day18auto02.LoggerBefore"></bean>
        <!--顾问-->
        <bean id="myadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
            <property name="advice" ref="myadvice"></property>
            <!--<property name="mappedNames" value="do*"></property>-->
            <!--<property name="pattern" value=".*do.*"></property>-->
            <property name="patterns" value=".*e.*"></property>
        </bean>
        <!--名称自动代理生成器-->
        <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames" value="service,bookservice"></property>
            <property name="interceptorNames" value="myadvisor"></property>
        </bean>
    
    </beans>

      此处没有顾问是不行的,名称自动代理生成器BeanNameAutoProxyCreator中的beanNames的值是String类型的数组,所以可以放多个目标对象,用逗号隔开即可

      单测方法:

    package cn.dawn.day18auto02;
    
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Created by Dawn on 2018/3/3.
     */
    public class test20180312 {
        @Test
        /*aop代理工厂bean异常增强*/
        public void t01(){
            ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day18auto02.xml");
            ISomeService service = (ISomeService) context.getBean("service");
            IBookService bookservice = (IBookService) context.getBean("bookservice");
    
            service.select();
            bookservice.selectAllBooks();
    
        }
    }
  • 相关阅读:
    双击导航栏自动滑动ListView到顶部
    及时取消代码中的AsyncTask
    Nubia Z9 mini使用体验
    特殊情况特殊处理
    SharePreferences的DB实现
    时下手机和p2p理财的共同点
    小米空气净化器体验
    消息框架的一种实现
    刷了MIUI的手机在OSX下连接USB调试的方法
    16个最佳响应式HTML5框架
  • 原文地址:https://www.cnblogs.com/DawnCHENXI/p/8550837.html
Copyright © 2011-2022 走看看