Introduction:
If is used the getBeanDefinitionNames()
method such as:
for(String beanName : ctx.getBeanDefinitionNames()) {
System.out.println(" " + beanName);
}
Is possible retrieve all the beans created. Until here all fine. If in the same report is need to know if each bean is singleton or not, is possible use either isSingleton(String name)
or isPrototype(String name)
(declared in the BeanFactory
interface) and taking as preference the former according with the indication of the javadoc of the latter, therefore:
for(String beanName : ctx.getBeanDefinitionNames()) {
System.out.println(" " + beanName + " is Singleton?: " + ctx.isSingleton(beanName));
}
Again all work fine as expected
Case:
Now, about @Lazy
- when a relation of dependency between two beans, both marked with @Lazy - I did realize there is no a isLazy(String name)
to be used in the report mentioned above. Therefore having the following:
@Service
@Profile("p1")
class LibroP1ServiceImpl implements LibroService {
@Autowired
private LibroRepository libroRepository;
@Lazy
@Autowired
private CacheService<Libro> cacheService;
and
@Lazy
@Service
class SimpleCacheServiceImpl implements CacheService<Libro> {
private final Map<Integer,Libro> cache;
public SimpleCacheServiceImpl() {
System.out.println("[[[SimpleCacheServiceImpl Constructor]]]");
cache = new LinkedHashMap<>();
}
Current Approach
My current solution is use in the LibroP1ServiceImpl
class:
@PostConstruct
private void postConstruct() {
System.out.println(" postConstruct:");
System.out.println(" LibroRepository: " + libroRepository.getClass());
System.out.println(" CacheService: " + cacheService.getClass());
}
Where if the lazy dependency was not called yet, the .getClass()
method prints a pattern including the Proxy
term such as:
class jdk.proxy3.$Proxy40
Other attempt was calling the report as:
for(String beanName : ctx.getBeanDefinitionNames()) {
System.out.println(" " + beanName);
if(beanName.equals("simpleCacheServiceImpl")) {
System.out.println(" CacheService: " + ctx.getBean(CacheService.class).getClass());
}
}
before and after of the use of the lazy dependency. But the first call (before) triggers the creation of the lazy object. So it does not work as expected.
Therefore I want to know if a bean was marked from the beginning as Lazy or not, therefore the isLazy(...)
would be useful.
Thanks for your understanding.
Comment From: quaff
Have you tried BeanDefinition::isLazyInit
?
Comment From: snicoll
Thanks @quaff
@manueljordan the methods you've referred to are actually on the BeanFactory
and exposed on the ApplicationContext
hierarchy for convenience. We don't mean to expose every attribute of the bean definition this way so if you need to get such information, I'd recommend to get the bean definition regardless, e.g.
for (String beanName : context.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = context.getBeanDefinition(beanName);
System.out.println(beanDefinition.isSingleton());
System.out.println(beanDefinition.isPrototype());
System.out.println(beanDefinition.isLazyInit());
}
Comment From: manueljordan
Thanks for the feedback to you both.
@snicoll just being curious and respecting your job, why the isSingleton
and isPrototype
methods and seem others are declared twice through BeanFactory
and BeanDefinition
? Thanks for your understanding.