0

I found, that in some situations, bean methods of annotated string configuration is called multiple times, although they should be cached.

How can it be happen?

Being inside one of such method, how can I indentify different nature of calls? May be classloaders or something?

UDPATE

I have checked instance ids of all calls and it appeared the same.

UPDATE 2

code is something like this

@Configuration
class Config {

   @Bean
   MyBean1 myBean1() {
   ....
   // I don't see explicit call for myBean2() here
   }

   @Bean
   MyBean2 myBean2() {
   ....
   // I have explicit call for myBean1() here
   }
}

the problem is that this config works in one environment. But in other environment is happening something strange, including interleaved inifinite calls of these two methods. Probably some error occurs, which causes mutual initialization, which is not happening in first environment.

Also I observe tremendous Spring exception sayin "unable to instantiate myBean1 because was unable to instantiate myBean2 because was unable to instantiate myBean1 ...." and so on, endless! I didn't find the end at least.

Usually I had Spring detecting initialization loops, but here I see it can't by some reason.

Dims
  • 47,675
  • 117
  • 331
  • 600
  • and you found out where? – Eugene Mar 30 '17 at 10:05
  • With annotated you mean @Cacheable? Can you provide an MCVE where you would expect caching behaviour but don't get it? – Jens Hoffmann Mar 30 '17 at 10:06
  • @JensHoffmann isn't `@Bean` annotation sufficient for caching? – Dims Mar 30 '17 at 10:24
  • @Dims sorry I completely misunderstood your question, I thought it was about [Spring Caching](https://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html), however I assume you're talking about beans being **singleton scoped** (which you call "cached") – Jens Hoffmann Apr 24 '17 at 15:00
  • Were you able to make progress on this? If you're still stuck I'm sure providing a MCVE will attract helpful responses. – Jens Hoffmann Apr 24 '17 at 15:03
  • @Dims Just to take a random guess: Have you checked `@Component` vs. `@Configuration` annotation on the Config class? I ask because of [section 7.10.5 in the Spring doc](http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factorybeans-annotations) starting with _The `@Bean` methods in a regular Spring component are processed differently than their counterparts inside a Spring `@Configuration` class_... – Jens Hoffmann Apr 24 '17 at 15:09
  • I solved it somehow. In bean creation method, I threw an exception and immediately catched it with printing stack trace. With this method I found a difference between two call stacks. I forgot exact reason, but it was something like two contexts created. – Dims Apr 24 '17 at 21:11

0 Answers0