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

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();

    }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/107657.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号