码迷,mamicode.com
首页 > 编程语言 > 详细

SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator

时间:2018-03-12 20:54:06      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:let   col   str   多个   text   pos   day   工厂   test   

 

 

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

 

 

名称自动代理生成器: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();

    }
}

 

SSM-Spring-15:Spring中名称自动代理生成器BeanNameAutoProxyCreator

标签:let   col   str   多个   text   pos   day   工厂   test   

原文地址:https://www.cnblogs.com/DawnCHENXI/p/8550837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!