gpt4 book ai didi

caching - 如何为动态 bean 内的方法启用缓存

转载 作者:行者123 更新时间:2023-12-02 15:46:58 24 4
gpt4 key购买 nike

我正在使用“AutowireCapableBeanFactory”创建一个动态 bean,如下所示

RegisterFoo.java

@Configuration
public class registerFoo {
@Autowired
ApplicationContext appcontext;

AutowireCapableBeanFactory bf;

@PostConstruct
public void registerFoo() {
bf = appContext.getAutowireCapableBeanFactory();
RootBeanDefinition def = new RootBeanDefinition(Foo.class);
((DefaultListableBeanFactory)bf).registerBean("foo", def);
}
}

RegisterBar.java

@Configuration
public class registerBar {
@Autowired
ApplicationContext appcontext;

AutowireCapableBeanFactory bf;

@PostConstruct
public void registerFoo() {
bf = appContext.getAutowireCapableBeanFactory();
RootBeanDefinition def = new RootBeanDefinition(Bar.class);
Foo foo = (Foo) appContext.getBean("foo");
ConstructorArgumentValues cav = new ConstructorArgumentValues();
cav.add(0, foo.getValue());
def.setArgumentValues(cav);
((DefaultListableBeanFactory)bf).registerBean("bar", def);
}
}

Foo.class

public class Foo {
@Cacheable
public String getValue() {
// return value
}
}

方法 getValue() 每次都会执行其主体。 Spring 没有按预期缓存该值。有什么建议吗?

最佳答案

我认为问题在于,当 spring 注册带有注释的 bean 时,它会由管理 @Cacheable 的 bean 后处理器进行后处理

当您手动注册时,后处理可能无法完成。

暂时无法验证,但这是我首先要查看的地方。

希望这有帮助。

关于caching - 如何为动态 bean 内的方法启用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37117464/

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