Announcement Announcement Module
Collapse
No announcement yet.
Spring Annotations Lazy Autowired Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Annotations Lazy Autowired

    I'm using Spring annotations and I want to use lazy initialization. I'm running into a problem that when I want to import a bean from another class I am forced to use @Autowired which does not seem to use lazy init. Is there anyway to force this lazy init behaviour?

    In this example I do not want to see "Loading parent bean" ever being printed as I am only loading childBean which has no dependencies on lazyParent.

    Code:
    @Configuration
    public class ConfigParent {
        @Bean
        @Lazy
        public Long lazyParent(){
            System.out.println("Loading parent bean");
            return 123L;
        }
    
    }
    
    @Configuration
    @Import(ConfigParent.class)
    public class ConfigChild {
        private @Autowired Long lazyParent;
        @Bean
        public Double childBean() {
            System.out.println("loading child bean");
            return 1.0;
        }
        @Bean
        @Lazy
        public String lazyBean() {
            return lazyParent+"!";
        }
    }
    
    public class ConfigTester {
        public static void main(String[] args) {
            ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigChild.class);
            Double childBean=ctx.getBean(Double.class);
            System.out.println(childBean);
    
        }
    
    }

  • #2
    You are missing the point of lazy or at least how lazy works with spring. Lazy simply means for spring not eagerly instantiated but only when needed. You are loading the ConfigChild class which needs the Lazy loaded bean (it is autowired) and as such the bean is going to be initialized.

    Comment


    • #3
      Hi, I am only using autowired as this is the only way to import a bean using annotations as far as I am aware.
      My point is that with annotations its impossible to load the childBean without loading the parentBean when I have lazyBean depending on the parentBean.
      Can you suggest another way of loading childBean without instantiating the parentBean?
      Seems to me that when using annoations you are forced to use autowiring which cannot use lazy loading which seems a bit of a limitation.

      Comment


      • #4
        This solved the problem...

        Code:
        @Configuration
        @Import(ConfigParent.class)
        public class ConfigChild {
        
            private @Autowired ConfigParent configParent;
        
            @Bean
            public Double childBean() {
                System.out.println("loading child bean");
                return 1.0;
            }
        
            @Bean
            @Lazy
            public String lazyBean() {
                return configParent.lazyParent() + "!";
            }
        }

        Comment


        • #5
          It has nothing to do with the fact that you use annotations it has to do with the fact that you have a @Autowired annotation on a field of for the type of a bean which instantiates that bean because it is needed.

          Work around is inject the configuration instead of the bean.

          Edit: Which you already figured out yourself

          Comment


          • #6
            that works...thank you.

            Comment

            Working...
            X