1
2 1------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
2
默认自动代理DefaultAdvisorAutoProxyCreator
本处没有什么要讲的,放原代码
ISomeService接口:
1
2
3
4
5
6
7
8
9
10
11
12 1package cn.dawn.day17atuo01;
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.day17atuo01;
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
LoggerBefore类,做了前置增强
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1package cn.dawn.day17atuo01;
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 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.day17atuo01.SomeServiceImpl"></bean>
12 <!--增强的内容-->
13 <bean id="myadvice" class="cn.dawn.day17atuo01.LoggerBefore"></bean>
14 <!--顾问-->
15 <bean id="myadvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
16 <property name="advice" ref="myadvice"></property>
17 <!--<property name="mappedNames" value="do*"></property>-->
18 <!--<property name="pattern" value=".*do.*"></property>-->
19 <property name="patterns" value=".*select.*"></property>
20 </bean>
21 <!--默认自动代理-->
22 <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>
23
24</beans>
25
必须要有顾问,没有不可以,默认自动代理里面不用实现参数,他自动匹配
单测方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 1package cn.dawn.day17auto01;
2
3
4import cn.dawn.day17atuo01.ISomeService;
5import org.junit.Test;
6import org.springframework.context.ApplicationContext;
7import org.springframework.context.support.ClassPathXmlApplicationContext;
8
9/**
10 * Created by Dawn on 2018/3/3.
11 */
12public class test20180312 {
13 @Test
14 /*默认自动代理*/
15 public void t01(){
16 ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day17auto01.xml");
17 ISomeService service = (ISomeService) context.getBean("service");
18
19 service.select();
20
21 }
22}
23