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

    }
}

 

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 阿里游戏大数据sesson2_RF&amp;GBRT(上)

    阿里游戏大数据sesson2_RF&amp;GBRT(上)

    2022年1月1日
    63
  • 经典的20道AJAX面试题[通俗易懂]

    经典的20道AJAX面试题[通俗易懂]1、什么是AJAX,为什么要使用Ajax(请谈一下你对Ajax的认识)什么是ajax:AJAX是“AsynchronousJavaScriptandXML”的缩写。他是指一种创建交互式网页应用的网页开发技术。Ajax包含下列技术:基于web标准(standards-basedpresentation)XHTML+CSS的表示;使用DOM(DocumentObjectM…

    2022年8月27日
    8
  • Linux tar 打包[通俗易懂]

    Linux tar 打包[通俗易懂]转载:https://www.cnblogs.com/lijc1990/p/3545503.html范例一:将整个/etc目录下的文件全部打包成为/tmp/etc.tar[root@linux~]#tar-cvf/tmp/etc.tar/etc&lt;==仅打包,不压缩![root@linux~]#tar-zcvf/tmp/etc.tar.gz/etc&l…

    2022年6月1日
    29
  • vue获取浏览器cookie_新型冠状病毒肺炎

    vue获取浏览器cookie_新型冠状病毒肺炎读取Cookie可以通过document.cookie直接读取cookie的内容:varstrCookie=document.cookie; 此时,strCookie是一个由该域名下的所有cookie的名/值对所组成的字符串,名/值对间以“分号加空格”分隔。为了方便查看,可以使用split()方法将cookie中的名/值对解析出来,得到一个cookie的列表。然后,再使用相应的解码方式,把c…

    2025年5月27日
    2
  • spring boot 系列之七:SpringBoot整合Mybatis

    springboot已经很流行,但是它仍需要搭配一款ORM框架来实现数据的CRUD,之前已经分享过JdbcTemplete和JPA的整合,本次分享下Mybatis的整合。对于mybatis的使用,需

    2022年2月16日
    38
  • 在pycharm中如何新建Python文件?_github下载的python源码项目怎么用

    在pycharm中如何新建Python文件?_github下载的python源码项目怎么用问题最近想把本地python项目提交到github,在网上找很多教程,都是如何在pycharm设置操作,但是这些人只讲了一部分,对于小白来说,需要从头到尾彻底了解一下。如果想把项目提交到github有多种方法,最常用的还是使用git,当然也可以下载githubDesktop这种GUI界面的工具,直接点点鼠标就可以提交项目。git下载地址:https://git-scm.com/downloads…

    2022年8月29日
    8

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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