Spring Aop之使用DeclareParents

DeclareParents可以用来给被代理对象添加一些方法,虽然不常用,但是在分析SpringAop源码的时候,对这里一头雾水,所以使用该文章记录下。目前SpringAop也给我们提供了两种实现方式。

定义NoMethodAspectBean

/**
 * @author 周宁
 * @Date 2019-07-23 16:43
 */
public class NoMethodAspectBean {
}

定义被代理类需要新增方法

/**
 * @author 周宁
 * @Date 2019-07-23 16:33
 */
public interface IDeclareParent {

    void isANewMethod();
}
/**
 * @author 周宁
 * @Date 2019-07-23 16:34
 */
public class IDeclareParentImpl implements IDeclareParent{
    @Override
    public void isANewMethod() {
        System.out.println("恭喜你喜提新方法一枚");
    }
}

1.使用@Aspect实现DeclareParents

定义AspectDeclareParent.java

/**
 * @author 周宁
 * @Date 2019-07-23 16:34
 */
@Aspect
public class AspectDeclareParent {

    @DeclareParents(value = "org.springframework.study.day13.NoMethodAspectBean", defaultImpl = IDeclareParentImpl.class)
    private IDeclareParent iDeclareParent;
}

配置aspectDeclareTest.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"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

	<aop:aspectj-autoproxy/>
	<bean id="noMethodAspectBean" class="org.springframework.study.day13.NoMethodAspectBean"/>
	<bean id="aspectDeclareParent" class="org.springframework.study.day13.AspectDeclareParent"/>
</beans>

测试

@Test
    public void testAspectDeclare(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("aspectDeclareTest.xml");
        NoMethodAspectBean aspectBean = ac.getBean(NoMethodAspectBean.class);
        IDeclareParent iDeclareParent = (IDeclareParent) aspectBean;
        iDeclareParent.isANewMethod();
    }

2.使用aspect:declare-parents实现

添加aspectDeclareXmlTest.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="noMethodAspectBean" class="org.springframework.study.day13.NoMethodAspectBean"/>

    <aop:config>
        <aop:aspect>
            <aop:declare-parents implement-interface="org.springframework.study.day13.IDeclareParent"
                                 types-matching="org.springframework.study.day13.NoMethodAspectBean"
                                 default-impl="org.springframework.study.day13.IDeclareParentImpl"/>
        </aop:aspect>
    </aop:config>
</beans>

编写测试类

@Test
    public void testAspectDeclareXml(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("aspectDeclareXmlTest.xml");
        NoMethodAspectBean aspectBean = ac.getBean(NoMethodAspectBean.class);
        IDeclareParent iDeclareParent = (IDeclareParent) aspectBean;
        iDeclareParent.isANewMethod();
    }

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring AOP 是一个用于实现面向切面编程的框架。它可以通过配置来实现横切关注点的模块化,并将其应用到程序的不同部分。Spring AOP 使用 AspectJ 切入点指示符来定义切入点表达式,用于匹配方法执行连接点。Spring AOP 支持以下 AspectJ 切入点指示符: 1. execution:用于匹配方法执行连接点。这是使用 Spring AOP 时要使用的主要切入点指示符。 2. within:限制匹配以连接某些类型中的点(使用 Spring AOP 时在匹配类型中声明的方法的执行)。 3. this:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中 Bean 引用(Spring AOP 代理)是给定类型的实例。 4. target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中目标对象(正在代理的应用程序对象)是给定类型的实例。 5. args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中参数是给定类型的实例。 6. @target:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中执行对象的类具有给定类型的注释。 7. @args:限制匹配到连接点(使用 Spring AOP 时方法的执行),其中传递的实际参数的运行时类型具有给定类型的注释。 8. @within:限制匹配以连接具有给定注释的类型中的点(使用 Spring AOP 时在具有给定注释的类型中声明的方法的执行)。 9. @annotation:限制匹配到连接点的主题(在 Spring AOP 中运行的方法)具有给定注释的连接点。 在使用 Spring AOP 时,需要引入 Spring AOPSpring Context 相关的包,并在配置文件中进行相应的配置。可以通过 Maven 或其他构建工具来引入相关依赖。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值