I create a dynamic bean using "AutowireCapableBeanFactory" as follows
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() {
The getValue () method executes its body every time. Spring does not cache the value as expected. Any suggestions?
spring-boot caching guava
sag
source share