SSM-Spring-14:Spring中默认自动代理DefaultAdvisorAutoProxyCreator

Posted 晨曦Dawn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM-Spring-14:Spring中默认自动代理DefaultAdvisorAutoProxyCreator相关的知识,希望对你有一定的参考价值。

 

 

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

 

 

默认自动代理DefaultAdvisorAutoProxyCreator

本处没有什么要讲的,放原代码

  ISomeService接口:

 

package cn.dawn.day17atuo01;

/**
 * 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.day17atuo01;


/**
 * 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");

    }
}

 

  LoggerBefore类,做了前置增强

 

package cn.dawn.day17atuo01;


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.day17atuo01.SomeServiceImpl"></bean>
    <!--增强的内容-->
    <bean id="myadvice" class="cn.dawn.day17atuo01.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=".*select.*"></property>
    </bean>
    <!--默认自动代理-->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean>

</beans>

 

  必须要有顾问,没有不可以,默认自动代理里面不用实现参数,他自动匹配

 

  单测方法:

 

package cn.dawn.day17auto01;


import cn.dawn.day17atuo01.ISomeService;
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
    /*默认自动代理*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day17auto01.xml");
        ISomeService service = (ISomeService) context.getBean("service");

        service.select();

    }
}

 

以上是关于SSM-Spring-14:Spring中默认自动代理DefaultAdvisorAutoProxyCreator的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot 自动配置的 Jackson ObjectMapper 默认不用于 WebFlux WebClient

Spring中Bean的作用域Spring的自动注入在spring配置文件中引入属性文件

在 Spring Boot 应用程序中自动装配 ObjectMapper

Spring-boot,无法自动装配类。未找到默认构造函数引发异常

Spring-ws 自动生成wsdl 修改请求默认Request后缀的问题

Spring boot 梳理 - 显示Springboot默认自动生成的bean