有没有Spring AOP @DeclareParents的例子?

时间:2011-07-04 03:47:21

标签: spring annotations aop

我一直试图让这个工作几天,而且一直在惨遭失败。我似乎无法将我的类正确地转换为Introduced接口。我正在使用Spring 3.0.5。

有没有人有一个使用@DeclareParents的项目的完整工作示例?或XML等价?我在网上找到了一堆片段,但是还没有成功地让这些片段工作。

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

目标:

@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
    ...
}

接口:

public interface AbstractBaseEntity {

    /**
     * @return the customAttributes
     */
    public Object getCustomAttribute(String name);

    /**
     * @param customAttributes the customAttributes to set
     */
    public void setCustomAttribute(String name, Object value);

}

实现:

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

}

方面:

    @DeclareParents(value="com.fwl.domain.model.*+", defaultImpl=AbstractBaseEntityImpl.class)
    public AbstractBaseEntity mixin;

但是,当我将引入的对象作为Object参数传递给方法时,检查它是否是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;

...
...
}

是否有确保正确完成介绍?无论如何要确定为什么我不能将它作为AbstractBaseEntity投射?

非常感谢任何帮助。

谢谢,

埃里克

1 个答案:

答案 0 :(得分: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;

}

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

    }
}