gpt4 book ai didi

spring - 有人有 Spring AOP @DeclareParents 示例吗?

转载 作者:行者123 更新时间:2023-12-04 15:15:04 34 4
gpt4 key购买 nike

几天来,我一直试图让它工作,但一直失败得很惨。我似乎无法让我的类正确转换为 Introduced 接口(interface)。我正在使用 Spring 3.0.5。

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

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

目标:

@实体
@表(名称=“项目”)
公共(public)类项目实现 java.io.Serializable {

私有(private)长ID;
私有(private)整数主类型;
私有(private)整数子类型;
私有(private)字符串品牌名称;
私有(private)字符串项目名称;
私有(private)日期 releaseDate;
私有(private)日期退休日期;
私有(private)字符串文件名;
私有(private)字符串缩略图名称;
私有(private)字符串维度;
私有(private) bool 事件;
私有(private) bool 解锁;
私有(private)整数解锁成本;
私有(private) bool 拥有;

公共(public)项目(){
}

...
//一堆 getter 和 setter
...
}

界面:

公共(public)接口(interface) AbstractBaseEntity {

/**
* @return 自定义属性
*/
公共(public)对象getCustomAttribute(字符串名称);

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

}

执行:

公共(public)类 AbstractBaseEntityImpl 实现 AbstractBaseEntity {

/**
* 可以为特定实体映射的自定义属性的映射
*/
@短暂的
protected Map customAttributes = new ConcurrentHashMap();

/**
* @return 自定义属性
*/
公共(public)对象getCustomAttribute(字符串名称){
返回 customAttributes.get(name);
}

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

}

方面:

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

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

公共(public)无效本地化(对象实体,区域设置语言环境){
列出缓存字段;
如果(实体 == 空)
//没做什么
返回;

//检查实体是否已经本地化
if(AbstractBaseEntity 的实体实例)
//已经本地化所以无事可做
返回;

...
...
}

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

任何帮助将不胜感激。

谢谢,

埃里克

最佳答案

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

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

}

新接口(interface)的实现:
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();

}
}

关于spring - 有人有 Spring AOP @DeclareParents 示例吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6567106/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com