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

释放双眼,带上耳机,听听看~!

 

 


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

 

 

名称自动代理生成器:BeanNameAutoProxyCreator

 

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

ISomeService接口:

 


1
2
3
4
5
6
7
8
9
10
11
12
1package cn.dawn.day18auto02;
2
3/**
4 * Created by Dawn on 2018/3/8.
5 */
6public interface ISomeService {
7    public void insert();
8    public void delete();
9    public void select();
10    public void update();
11}
12

 

它的实现类:SomeServiceImpl

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1package cn.dawn.day18auto02;
2
3
4/**
5 * Created by Dawn on 2018/3/8.
6 */
7public class SomeServiceImpl implements ISomeService {
8
9    public void insert() {
10        System.out.println("insert ok");
11    }
12
13    public void delete() {
14        System.out.println("delete ok");
15
16    }
17
18    public void select() {
19        System.out.println("select ok");
20
21    }
22
23    public void update() {
24        System.out.println("update ok");
25
26    }
27}
28

 

IBookService接口:

 


1
2
3
4
5
6
7
8
9
1package cn.dawn.day18auto02;
2
3/**
4 * Created by Dawn on 2018/3/12.
5 */
6public interface IBookService {
7    public void selectAllBooks();
8}
9

 

它的实现类:BookServiceImpl

 


1
2
3
4
5
6
7
8
9
10
11
1package cn.dawn.day18auto02;
2
3/**
4 * Created by Dawn on 2018/3/12.
5 */
6public class BookServiceImpl implements IBookService {
7    public void selectAllBooks() {
8        System.out.println("selectbooks ok");
9    }
10}
11

 

一个增强的

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
1package cn.dawn.day18auto02;
2
3
4import org.springframework.aop.MethodBeforeAdvice;
5
6import java.lang.reflect.Method;
7
8/**
9 * Created by Dawn on 2018/3/5.
10 */
11/*前置增强*/
12public class LoggerBefore implements MethodBeforeAdvice {
13    public void before(Method method, Object[] objects, Object o) throws Throwable {
14        System.out.println("日志记录");
15    }
16}
17

 

xml配置文件中:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1<?xml version="1.0" encoding="UTF-8"?>
2<beans xmlns="http://www.springframework.org/schema/beans"
3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4       xmlns:aop="http://www.springframework.org/schema/aop"
5       xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
6       xsi:schemaLocation="http://www.springframework.org/schema/beans
7       http://www.springframework.org/schema/beans/spring-beans.xsd
8       http://www.springframework.org/schema/aop
9       http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
10    <!--要增强的对象-->
11    <bean id="service" class="cn.dawn.day18auto02.SomeServiceImpl"></bean>
12    <bean id="bookservice" class="cn.dawn.day18auto02.BookServiceImpl"></bean>
13    <!--增强的内容-->
14    <bean id="myadvice" class="cn.dawn.day18auto02.LoggerBefore"></bean>
15    <!--顾问-->
16    <bean id="myadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
17        <property name="advice" ref="myadvice"></property>
18        <!--<property name="mappedNames" value="do*"></property>-->
19        <!--<property name="pattern" value=".*do.*"></property>-->
20        <property name="patterns" value=".*e.*"></property>
21    </bean>
22    <!--名称自动代理生成器-->
23    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
24        <property name="beanNames" value="service,bookservice"></property>
25        <property name="interceptorNames" value="myadvisor"></property>
26    </bean>
27
28</beans>
29

 

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

 

单测方法:

 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
1package cn.dawn.day18auto02;
2
3
4import org.junit.Test;
5import org.springframework.context.ApplicationContext;
6import org.springframework.context.support.ClassPathXmlApplicationContext;
7
8/**
9 * Created by Dawn on 2018/3/3.
10 */
11public class test20180312 {
12    @Test
13    /*aop代理工厂bean异常增强*/
14    public void t01(){
15        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day18auto02.xml");
16        ISomeService service = (ISomeService) context.getBean("service");
17        IBookService bookservice = (IBookService) context.getBean("bookservice");
18
19        service.select();
20        bookservice.selectAllBooks();
21
22    }
23}
24

 

转载于:https://www.cnblogs.com/DawnCHENXI/p/8550837.html

给TA打赏
共{{data.count}}人
人已打赏
安全技术

C++回调函数

2022-1-11 12:36:11

安全经验

Spring-cloud 微服务架构搭建 03 - Hystrix 深入理解与配置使用

2021-11-28 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索