Spring aop:decare-parent 为类增加新的方法

Spring aop:decare-parent 为类增加新的方法:

使用XML配置的方式:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
    <context:component-scan base-package="com.stono.sprtest2"></context:component-scan>
    <aop:config>
        <aop:aspect>
            <aop:declare-parents types-matching="com.stono.sprtest2.Singer" implement-interface="com.stono.sprtest2.IntroduceI" default-impl="com.stono.sprtest2.IntroduceImpl" />
            <aop:declare-parents types-matching="com.stono.sprtest2.Singer" implement-interface="com.stono.sprtest2.Introduce2I" delegate-ref="intro2" />
        </aop:aspect>
    </aop:config>
    <bean id="intro2" class="com.stono.sprtest2.Introduce2Impl"></bean>
</beans>

AppBean:

package com.stono.sprtest2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppBeans10 {
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("appbeans10.xml");
        Singer singer = (Singer) context.getBean("singer");
        IntroduceI introduceI = (IntroduceI) singer;
        introduceI.intro();
        Introduce2I introduce2i = (Introduce2I) singer;
        introduce2i.intro2();
    }
}

Bean:

package com.stono.sprtest2;
import org.springframework.stereotype.Component;
@Component
public class Singer {
    public void perform() {
        System.out.println("com.stono.sprtest2.Singer.Perform()");
    }
}

Interface and Implements:

其中的方法名称不能一致,可能会引起混淆,会把第二个Impl的方法误认为第一个Impl的方法。

package com.stono.sprtest2;
public interface IntroduceI {
    void intro();
}
package com.stono.sprtest2;
public class IntroduceImpl implements IntroduceI {
    @Override
    public void intro() {
        System.out.println("this is default introduce!");
    }
}
package com.stono.sprtest2;
public interface Introduce2I {
    // 这个方法名称不能是intro(),否则可能把这个方法误认为是IntroduceI的方法;
    void intro2();
}
package com.stono.sprtest2;
public class Introduce2Impl implements Introduce2I {
    @Override
    public void intro2() {
        System.out.println("this is default introduce2 impl.");
    }
}
原文地址:https://www.cnblogs.com/stono/p/4857391.html