有人举过Spring AOP@DeclareParents的例子吗?

有人举过Spring AOP@DeclareParents的例子吗?,spring,annotations,aop,Spring,Annotations,Aop,几天来,我一直在努力让它发挥作用,但一直失败得很惨。我似乎无法将我的类正确地转换为引入的接口。我正在使用Spring3.0.5 有没有人有使用@DeclareParents的项目的完整工作示例?还是XML等价物?我在网上找到了一堆代码片段,但也没有成功地让它们发挥作用 这是我第一次尝试介绍,但是我很难做到这一点。我已经阅读了Spring文档、AspectJ文档以及论坛,但仍然无法让它发挥作用。我认为创建的类/接口是正确的,但是当我尝试将建议的对象强制转换到接口时,我得到了一个强制转换错误 目标:

几天来,我一直在努力让它发挥作用,但一直失败得很惨。我似乎无法将我的类正确地转换为引入的接口。我正在使用Spring3.0.5

有没有人有使用@DeclareParents的项目的完整工作示例?还是XML等价物?我在网上找到了一堆代码片段,但也没有成功地让它们发挥作用

这是我第一次尝试介绍,但是我很难做到这一点。我已经阅读了Spring文档、AspectJ文档以及论坛,但仍然无法让它发挥作用。我认为创建的类/接口是正确的,但是当我尝试将建议的对象强制转换到接口时,我得到了一个强制转换错误

目标:

@Entity @Table(name = "item") public class Item implements java.io.Serializable { private long id; private Integer mainType; private Integer subType; private String brandName; private String itemName; private Date releaseDate; private Date retireDate; private String fileName; private String thumbnailName; private String dimension; private boolean active; private boolean unlocked; private Integer unlockCost; private boolean owned; public Item() { } ... // bunch of getters and setters ... } @实体 @表(name=“item”) 公共类项实现java.io.Serializable{ 私人长id; 私有整数类型; 私有整数子类型; 私有字符串品牌名称; 私有字符串itemName; 私人日期发布日期; 私人退休日期; 私有字符串文件名; 私有字符串thumbnailName; 私有字符串维数; 私有布尔活动; 私有布尔解锁; 私有整数解锁成本; 私人所有; 公共项目(){ } ... //一群能手和能手 ... } 接口:

public interface AbstractBaseEntity { /** * @return the customAttributes */ public Object getCustomAttribute(String name); /** * @param customAttributes the customAttributes to set */ public void setCustomAttribute(String name, Object value); }
package pl.beans;

public interface Performance {
    public void perform();
}
公共接口AbstractBaseEntity{ /** *@返回自定义属性 */ 公共对象getCustomAttribute(字符串名称); /** *@param customAttributes要设置的customAttributes */ public void setCustomAttribute(字符串名称、对象值); } 实施:

public class AbstractBaseEntityImpl implements AbstractBaseEntity { /** * Map of custom attributes that can be mapped for the specific entity */ @Transient protected Map customAttributes = new ConcurrentHashMap(); /** * @return the customAttributes */ public Object getCustomAttribute( String name ) { return customAttributes.get(name); } /** * @param customAttributes the customAttributes to set */ public void setCustomAttribute(String name, Object value) { this.customAttributes.put(name, value); } } 公共类AbstractBaseEntityImpl实现AbstractBaseEntity{ /** *可以为特定实体映射的自定义属性的映射 */ @短暂的 protected Map customAttributes=new ConcurrentHashMap(); /** *@返回自定义属性 */ 公共对象getCustomAttribute(字符串名称){ 返回customAttributes.get(name); } /** *@param customAttributes要设置的customAttributes */ public void setCustomAttribute(字符串名称、对象值){ this.customAttributes.put(名称、值); } } 方面:

@DeclareParents(value="com.fwl.domain.model.*+", defaultImpl=AbstractBaseEntityImpl.class) public AbstractBaseEntity mixin;
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;

}
@DeclareParents(value=“com.fwl.domain.model.*+”,defaultImpl=AbstractBaseEntityImpl.class) 公共实体混合; 但是,当我将引入的对象作为对象参数传递给方法,检查它是否是AbstractBaseEntity的实例时,它返回false

public void localize(Object entity, Locale locale) { List cachedFields; if (entity == null) // do nothing return; // check if the entity is already localized if( entity instanceof AbstractBaseEntity) // already localized so nothing to do return; ... ... } 公共void本地化(对象实体、区域设置){ 列出缓存字段; if(实体==null) //无所事事 返回; //检查实体是否已本地化 if(AbstractBaseEntity的实体实例) //已经本地化了,所以没什么可做的 返回; ... ... } 是否有任何方法来确保正确完成介绍?确定为什么我不能将其转换为AbstractBaseEntity

任何帮助都将不胜感激

谢谢


Eric

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

public interface AbstractBaseEntity { /** * @return the customAttributes */ public Object getCustomAttribute(String name); /** * @param customAttributes the customAttributes to set */ public void setCustomAttribute(String name, Object value); }
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;

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!'");

    }

}
方面:

@DeclareParents(value="com.fwl.domain.model.*+", defaultImpl=AbstractBaseEntityImpl.class) public AbstractBaseEntity mixin;
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;

}
JavaConfig:

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();

    }
}