我正在使用"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时,它将由bean后处理器进行后处理,该处理器将管理@Cacheable
手动注册时,可能无法完成后处理。
暂时无法核实,但这是我首先要看的地方。
希望得到帮助。