Results 1 to 6 of 6

Thread: Spring Annotations Lazy Autowired

  1. #1
    Join Date
    Jan 2012
    Posts
    9

    Default 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. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2012
    Posts
    9

    Default

    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.

  4. #4
    Join Date
    Jan 2012
    Posts
    9

    Default

    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() + "!";
        }
    }

  5. #5
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,624

    Default

    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
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  6. #6
    Join Date
    Jan 2012
    Posts
    9

    Default

    that works...thank you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •