Spring @Aspect进行类的接口扩展

Spring @Aspect进行类的接口扩展:

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">
    <!-- 这个声明会创建AnnotationAwareAspectJAutoProxyCreator,进行切面Bean的代理 -->
    <aop:aspectj-autoproxy />
    <!-- 必须将切面类声明为一个Bean -->
    <bean id="introduceAspect" class="com.stono.sprtest3.IntroduceAspect"></bean>
    <bean id="singer" class="com.stono.sprtest3.Singer"></bean>
</beans>

 

AppBean:

package com.stono.sprtest3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppBeans13 {
    public static void main(String[] args) {
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("appbeans13.xml");
        // 有接口必须使用接口,使用Singer类会出现ClassCastException;
        Performer bean = (Performer) context.getBean("singer");
        IntroduceI introduceI = (IntroduceI) bean;
        introduceI.introduce();
    }
}

 

Aspect:

package com.stono.sprtest3;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
@Aspect
public class IntroduceAspect {
    // 为Performer接口扩展增加IntroduceI接口;
    @DeclareParents(value = "com.stono.sprtest3.Performer+", defaultImpl = DefaultIntro.class)
    public static IntroduceI introduceI;
}

 

POJO:

package com.stono.sprtest3;
public interface Performer {
    void perform();
}
package com.stono.sprtest3;
public class Singer implements Performer {
    public void perform() {
        System.out.println("com.stono.sprtest3.Singer.perform()");
    }
}
package com.stono.sprtest3;
public interface IntroduceI {
    void introduce();
}

package com.stono.sprtest3;
public class DefaultIntro implements IntroduceI {
    @Override
    public void introduce() {
        System.out.println("this is default introduce");
    }
}

 

转载于:https://www.cnblogs.com/stono/p/4860494.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring框架中,代理可以通过两种方式来实现:JDK动态代理和CGLIB动态代理。 1. JDK动态代理: JDK动态代理是基于接口的代理模式,它要求目标必须实现至少一个接口。JDK动态代理通过`java.lang.reflect.Proxy`和`java.lang.reflect.InvocationHandler`接口来实现。在运行时,通过`Proxy.newProxyInstance()`方法创建一个代理对象,并通过实现InvocationHandler接口来处理代理方法的调用。JDK动态代理只能对接口进行代理,无法对进行代理。 2. CGLIB动态代理: CGLIB动态代理是基于继承的代理模式,它可以对进行代理,无需目标实现接口。CGLIB动态代理通过继承目标的方式来创建代理对象,并通过重写父的方法来实现对目标方法的拦截和增强。CGLIB动态代理使用的是字节码增强库`cglib`,需要引入相关的依赖。 Spring框架中的代理通常由AOP(面向切面编程)模块使用。AOP可以通过切面(Aspect)和通知(Advice)来定义横切关注点,并将其应用到目标的方法上。在AOP中,代理对象会拦截目标方法的调用,并在调用前后执行相应的增强逻辑。 需要注意的是,Spring中的代理通常是由Spring容器自动创建和管理的,开发者无需手动创建代理对象。使用Spring的代理机制可以实现事务管理、日志记录、性能监控等功能,提高系统的可维护性和扩展性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值