【问题标题】:Anyone have a Spring AOP @DeclareParents example?有人有 Spring AOP @DeclareParents 示例吗?
【发布时间】:2011-09-27 20:50:48
【问题描述】:

几天来,我一直在努力让它工作,但一直以失败告终。我似乎无法让我的类正确转换为 Introduced 接口。我正在使用 Spring 3.0.5。

有没有人有一个使用@DeclareParents 的项目的完整工作示例?还是 XML 等价物?我在网上找到了一堆 sn-ps,但也没有成功让它们工作。

这是我第一次尝试 Introductions,但我很难让它发挥作用。我已经阅读了 Spring 文档和 AspectJ 文档以及论坛,但仍然无法使其正常工作。我认为正确地创建了类/接口,但是当我尝试将 Advised 对象转换为接口时,我得到了一个转换错误。

目标:

@实体 @表(名称=“项目”) 公共类项目实现 java.io.Serializable { 私人长ID; 私有整数主类型; 私有整数子类型; 私有字符串品牌名称; 私有字符串项目名称; 私人日期 releaseDate; 私人日期退休日期; 私有字符串文件名; 私有字符串缩略图名称; 私有字符串维度; 私人布尔活动; 私有布尔解锁; 私人整数解锁成本; 私有布尔拥有; 公共项目(){ } ... // 一堆 getter 和 setter ... }

界面:

公共接口 AbstractBaseEntity { /** * @return 自定义属性 */ 公共对象getCustomAttribute(字符串名称); /** * @param customAttributes 要设置的 customAttributes */ public void setCustomAttribute(字符串名称,对象值); }

实施:

公共类 AbstractBaseEntityImpl 实现 AbstractBaseEntity { /** * 可以为特定实体映射的自定义属性的映射 */ @短暂的 protected Map customAttributes = new ConcurrentHashMap(); /** * @return 自定义属性 */ 公共对象getCustomAttribute(字符串名称){ 返回 customAttributes.get(name); } /** * @param customAttributes 要设置的 customAttributes */ 公共无效 setCustomAttribute(字符串名称,对象值){ this.customAttributes.put(name, value); } }

方面:

@DeclareParents(value="com.fwl.domain.model.*+", defaultImpl=AbstractBaseEntityImpl.class) 公共 AbstractBaseEntity 混合;

但是,当我将引入的对象作为 Object 参数传递给方法时,检查它是否是 AbstractBaseEntity 的实例时,它返回 false。

公共无效本地化(对象实体,区域设置语言环境){ 列出缓存字段; 如果(实体 == 空) // 没做什么 返回; // 检查实体是否已经本地化 if(AbstractBaseEntity 的实体实例) // 已经本地化所以无事可做 返回; ... ... }

有没有办法确保介绍的正确进行?无论如何要确定为什么我不能将其转换为 AbstractBaseEntity?

任何帮助将不胜感激。

谢谢,

埃里克

【问题讨论】:

    标签: spring annotations aop


    【解决方案1】:

    我有工作示例,但我知道这个问题很老了。
    基本界面:

    package pl.beans;
    
    public interface Performance {
        public void perform();
    }
    

    性能的实现:

    package pl.beans;
    
    import java.util.Random;
    
    import org.springframework.stereotype.Component;
    
    @Component
    public class Actor implements Performance {
    
    private static final String WHO = "Actor: ";
    
    @Override
    public void perform() {
        System.out.println(WHO+"Making some strange things on scene");
        int result = new Random().nextInt(5);
        if(result == 0) {
            throw new IllegalArgumentException(WHO+"Actor falsified");
        }
    
    }
    

    }

    新界面:

    package pl.introduction;
    
    public interface Crazy {
    
        public void doSomeCrazyThings();
    
    }
    

    新界面的实现:

    package pl.introduction;
    
    public class CrazyActor implements  Crazy {
    
    @Override
    public void doSomeCrazyThings() {
        System.out.println("Ługabuga oooo  'Performer goes crazy!'");
    
        }
    
    }
    

    方面:

    package pl.introduction;
    
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.DeclareParents;
    
    @Aspect
    public class CrazyIntroducer {
    
        @DeclareParents(value="pl.beans.Performance+", defaultImpl=pl.introduction.CrazyActor.class)
        public static Crazy shoutable;
    
    }
    

    Java配置:

    package pl.introduction;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.EnableAspectJAutoProxy;
    
    import pl.beans.Actor;
    import pl.beans.Performance;
    
    @Configuration
    @EnableAspectJAutoProxy
    @ComponentScan
    public class Config {
    
        @Bean
        public CrazyIntroducer introducer () {
            return new CrazyIntroducer();
        }
    
        @Bean
        public Performance performance() {
            return new Actor();
        }
    }
    

    并且测试表明引入工作:

    package pl.introduction;
    
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import pl.beans.Performance;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = pl.introduction.Config.class)
    public class IntroductionTest {
    
    @Autowired
    private Performance performance;
    
    @Test
    public void shoutTest() {
        try {
            performance.perform();
    
        } catch (IllegalArgumentException e) {
            System.out.println(e);
        }
    
        assertTrue(performance instanceof Crazy);
        ((Crazy) performance).doSomeCrazyThings();
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      相关资源
      最近更新 更多