有人有Spring AOP @DeclareParents示例吗?

Eri*_* B. 5 aop spring annotations

我一直在努力使它工作几天,却一直在失败。我似乎无法将我的课程正确投射到Introduced接口。我正在使用Spring 3.0.5。

有没有人有使用@DeclareParents的项目的完整工作示例?还是等效的XML?我在网上找到了很多片段,但是也没有成功地使它们起作用。

这是我第一次尝试“简介”,但是要使其工作起来会遇到很多困难。我已经阅读了Spring文档,AspectJ文档以及论坛,但仍然无法正常工作。我以为正确创建了类/接口,但是当我尝试将Advised对象转换为接口时,出现转换错误。

目标:

@实体
@Table(name =“ item”)
公共类Item实现java.io.Serializable {

    私人长号;
    私有Integer mainType;
    私有Integer子类型;
    私有String brandName;
    私有字符串itemName;
    私有Date releaseDate;
    私人Date retireDate;
    私有字符串fileName;
    私有字符串thumbnailName;
    私有String维度;
    私有布尔活动;
    私人布尔值已解锁;
    私有Integer unlockCost;
    私有布尔值;

    公共Item(){
    }

    ...
    //一堆吸气剂和二传手
    ...
}

接口:

公共接口AbstractBaseEntity {

    / **
     * @返回customAttributes
     * /
    公共对象getCustomAttribute(String name);

    / **
     * @param customAttributes要设置的customAttributes
     * /
    公共无效setCustomAttribute(字符串名称,对象值);

}

实现方式:

公共类AbstractBaseEntityImpl实现AbstractBaseEntity {

    / **
     *可以为特定实体映射的自定义属性的映射
     * /
    @短暂的
    受保护的地图customAttributes = new ConcurrentHashMap();

    / **
     * @返回customAttributes
     * /
    公共对象getCustomAttribute(字符串名称){
        返回customAttributes.get(name);
    }

    / **
     * @param customAttributes要设置的customAttributes
     * /
    public void setCustomAttribute(字符串名称,对象值){
        this.customAttributes.put(名称,值);
    }

}

方面:

    @DeclareParents(value =“ com.fwl.domain.model。* +”,defaultImpl = AbstractBaseEntityImpl.class)
    公共AbstractBaseEntity mixin;

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

    public void localize(对象实体,区域设置语言环境){
        列出cachedFields;
            如果(实体==空)
                // 没做什么
                返回;

            //检查实体是否已经本地化
            if(AbstractBaseEntity的实体instance)
                //已本地化,因此无需执行任何操作
                返回;

...
...
}

是否有任何方法可以确保正确进行介绍?无论如何要确定为什么我不能将其转换为AbstractBaseEntity?

任何帮助将不胜感激。

谢谢,

埃里克

DBa*_*ura 5

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

package pl.beans;

public interface Performance {
    public void perform();
}
Run Code Online (Sandbox Code Playgroud)

性能的实现:

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");
    }

}
Run Code Online (Sandbox Code Playgroud)

}

新界面:

package pl.introduction;

public interface Crazy {

    public void doSomeCrazyThings();

}
Run Code Online (Sandbox Code Playgroud)

新接口的实现:

package pl.introduction;

public class CrazyActor implements  Crazy {

@Override
public void doSomeCrazyThings() {
    System.out.println("?ugabuga oooo  'Performer goes crazy!'");

    }

}
Run Code Online (Sandbox Code Playgroud)

方面:

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;

}
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

并测试表明该介绍有效:

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

    }
}
Run Code Online (Sandbox Code Playgroud)