【框架】[Spring]XML配置实现AOP拦截-切点:JdkRegexpMethodPointcut

简介: 转载请注明出处:http://blog.csdn.net/qq_26525215本文源自【大学之旅_谙忆的博客】如果你把此种纯Java方式实现AOP拦截读懂了,理解本篇博客会更容易。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

如果你把此种纯Java方式实现AOP拦截读懂了,理解本篇博客会更容易。
【框架】[Spring]纯Java的方式实现AOP切面(拦截)技术

这篇讲解的是用xml配置文件来实现AOP拦截。
其实也挺简单的,无非是把一些对象通过xml文件配置new出来与初始化里面的一些值。

需要的包什么的就不解释了,直接给个网址:
http://repo.springsource.org/libs-release-local/org/springframework/spring/

项目结构图

直接上代码

1、准备好原型对象:

package cn.hncu.xmlImpl;

public class Person {
    public void run(){
        System.out.println("我在run...");
    }

    public void run(int i){
        System.out.println("我在run"+i+"...");
    }

    public void say(){
        System.out.println("我在say...");
    }

}

2、准备好代理类

代理动作什么的都会写在这里,为了方便,只实现MethodInterceptor接口,里面的invoke是最强的。

package cn.hncu.xmlImpl;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor{
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("前面拦截....");
        Object resObj = invocation.proceed();//放行
        System.out.println("后面拦截.....");
        return resObj;
    }

}

3、配置好xml文件:

把切点和通知配置成 切面的外部bean

取名为:1.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:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切点 -->
    <bean id="cut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
        <property name="pattern" value=".*run.*"></property>
    </bean>

    <!-- 通知-由我们写,实际代理动作 -->
    <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <property name="pointcut" ref="cut"></property>
        <property name="advice" ref="advice"></property>
    </bean>

    <!-- 代理工厂 -->
    <bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 放入原型对象 -->
        <property name="target" ref="person"></property>

        <!-- 放入切面 -->
        <property name="interceptorNames">
            <list>
                <value>advisor</value>
            </list>
        </property>
    </bean>


</beans>

把切点和通知配置成 切面的内部bean

<beans ..>
<!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
        <!-- 切点 -->
        <property name="pointcut">
            <bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
                <property name="pattern" value=".*run.*"></property>
            </bean>
        </property>
        <!-- 通知-由我们写,实际代理动作 -->
        <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
        </property>
    </bean>

    <!-- 代理工厂 -->
    <bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 放入原型对象 -->
        <property name="target" ref="person"></property>

        <!-- 放入切面 -->
        <property name="interceptorNames">
            <list>
                <value>advisor</value>
            </list>
        </property>
    </bean>
</beans>

直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置

<beans...>
<?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!-- 代理前原对象 -->
    <bean id="person" class="cn.hncu.xmlImpl.Person"></bean>

    <!-- 切面 = 切点+通知 -->
    <bean id="advisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <!-- 切点 -->
        <property name="patterns">
            <list>
                <value>.*run.*</value>
            </list>
        </property>
        <!-- 通知-由我们写,实际代理动作 -->
        <property name="advice">
            <bean id="advice" class="cn.hncu.xmlImpl.AroundAdvice"></bean>
        </property>
    </bean>

    <!-- 代理工厂 -->
    <bean id="personProxied" class="org.springframework.aop.framework.ProxyFactoryBean">
        <!-- 放入原型对象 -->
        <property name="target" ref="person"></property>

        <!-- 放入切面 -->
        <property name="interceptorNames">
            <list>
                <value>advisor</value>
            </list>
        </property>
    </bean>


</beans>
</beans>

4、测试方法:

@Test//把切点和通知配置成 切面的外部bean
    public void demo1(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/1.xml");
        Person p =ctx.getBean("personProxied",Person.class);
        p.run();
        p.say();
    }

    @Test//把切点和通知配置成 切面的内部bean
    public void demo2(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/2.xml");
        Person p =ctx.getBean("personProxied",Person.class);
        p.run();
        p.say();
    }

    @Test//直接在切面bean中配置“切点的正则表达式”,省去“切点bean”的配置
    public void demo3(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("cn/hncu/xmlImpl/3.xml");
        Person p =ctx.getBean("personProxied",Person.class);
        p.run();
        p.say();
    }

输出结果:

这是通过定义JdkRegexpMethodPointcut切入点的方式来实现AOP,通过这种编程方式,可以针对业务方法进行包装或者监控。

但是这个JdkRegexpMethodPointcut还不是很好,它拦截的单位是类!而无法细分到方法。不过放心,后面还有3中切点技术~
用AspectJExpressionPointcut切点就可以实现专门对什么方法进行拦截。后面会有专门介绍与实例的。

本文章由[谙忆]编写, 所有权利保留。

转载请注明出处:http://blog.csdn.net/qq_26525215

本文源自大学之旅_谙忆的博客

目录
相关文章
|
存储 Java 文件存储
微服务——SpringBoot使用归纳——Spring Boot使用slf4j进行日志记录—— logback.xml 配置文件解析
本文解析了 `logback.xml` 配置文件的详细内容,包括日志输出格式、存储路径、控制台输出及日志级别等关键配置。通过定义 `LOG_PATTERN` 和 `FILE_PATH`,设置日志格式与存储路径;利用 `&lt;appender&gt;` 节点配置控制台和文件输出,支持日志滚动策略(如文件大小限制和保存时长);最后通过 `&lt;logger&gt;` 和 `&lt;root&gt;` 定义日志级别与输出方式。此配置适用于精细化管理日志输出,满足不同场景需求。
3113 1
|
XML Java 数据库连接
微服务——SpringBoot使用归纳——Spring Boot集成MyBatis——基于 xml 的整合
本教程介绍了基于XML的MyBatis整合方式。首先在`application.yml`中配置XML路径,如`classpath:mapper/*.xml`,然后创建`UserMapper.xml`文件定义SQL映射,包括`resultMap`和查询语句。通过设置`namespace`关联Mapper接口,实现如`getUserByName`的方法。Controller层调用Service完成测试,访问`/getUserByName/{name}`即可返回用户信息。为简化Mapper扫描,推荐在Spring Boot启动类用`@MapperScan`注解指定包路径避免逐个添加`@Mapper`
943 0
|
XML Java 数据格式
Spring从入门到入土(xml配置文件的基础使用方式)
本文详细介绍了Spring框架中XML配置文件的使用方法,包括读取配置文件、创建带参数的构造对象、使用工厂方法和静态方法创建对象、对象生命周期管理以及单例和多例模式的测试。
872 7
Spring从入门到入土(xml配置文件的基础使用方式)
|
Java API 数据安全/隐私保护
(工作经验)优雅实现接口权限校验控制:基于自定义注解、AOP与@ConditionalOnProperty配置开关的通用解决方案
(工作经验)优雅实现接口权限校验控制:基于自定义注解、AOP与@ConditionalOnProperty配置开关的通用解决方案
755 1
|
XML Java 数据格式
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
这篇文章是Spring5框架的实战教程,主要介绍了如何在Spring的IOC容器中通过XML配置方式使用外部属性文件来管理Bean,特别是数据库连接池的配置。文章详细讲解了创建属性文件、引入属性文件到Spring配置、以及如何使用属性占位符来引用属性文件中的值。
Spring5入门到实战------7、IOC容器-Bean管理XML方式(外部属性文件)
|
XML Java 数据格式
【SpringFramework】Spring IoC-基于XML的实现
本文主要讲解SpringFramework中IoC和DI相关概念,及基于XML的实现方式。
329 69
|
XML Java 数据格式
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
本文介绍了在使用Spring框架时,如何通过创建`applicationContext.xml`配置文件来管理对象。首先,在resources目录下新建XML配置文件,并通过IDEA自动生成部分配置。为完善配置,特别是添加AOP支持,可以通过IDEA的Live Templates功能自定义XML模板。具体步骤包括:连续按两次Shift搜索Live Templates,配置模板内容,输入特定前缀(如spring)并按Tab键即可快速生成完整的Spring配置文件。这样可以大大提高开发效率,减少重复工作。
1064 1
使用idea中的Live Templates自定义自动生成Spring所需的XML配置文件格式
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
525 6
|
XML Java 数据格式
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
Spring 第二节内容补充 关于Bean配置的更多内容和细节 万字详解!
829 18
Spring IOC—基于XML配置Bean的更多内容和细节(通俗易懂)
|
XML Java 数据格式
spring复习02,xml配置管理bean
详细讲解了Spring框架中基于XML配置文件管理bean的各种方式,包括获取bean、依赖注入、特殊值处理、属性赋值、集合类型处理、p命名空间、bean作用域及生命周期和自动装配。
spring复习02,xml配置管理bean