Spring 的default-lazy-init="false" 如果没有xml使用@Configuration类,该怎么配置。
答案:1 悬赏:60
解决时间 2021-02-21 14:10
- 提问者网友:城市野鹿
- 2021-02-20 23:55
Spring 的default-lazy-init="false" 如果没有xml使用@Configuration类,该怎么配置。
最佳答案
- 二级知识专家网友:酒者煙囻
- 2021-02-21 00:28
在@Configuration类上使用注解@Lazy.
下面是@Lazy的 JavaDoc:
Indicates whether a bean is to be lazily initialized.
May be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.
If this annotation is not present on a @Component or @Bean definition, eager initialization will occur. If present and set to true, the @Bean or @Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.
If Lazy is present on a @Configuration class, this indicates that all @Bean methods within that @Configuration should be lazily initialized. If @Lazy is present and false on a @Bean method within a @Lazy-annotated @Configuration class, this indicates overriding the 'default lazy' behavior and that the bean should be eagerly initialized
大义是:@Lazy是用来控制懒加载的,可以直接或间接地用在@Compent 和@Bean 上;如果设置为 true, 则被调用时才会被加载,设置为 false, 则启动时加载;@Configuration 的@Lazy为默认@Lazy, 但是@Bean 上的@Lazy 可以覆盖@Configuration 的@Lazy
欢迎追问和评论
下面是@Lazy的 JavaDoc:
Indicates whether a bean is to be lazily initialized.
May be used on any class directly or indirectly annotated with @Component or on methods annotated with @Bean.
If this annotation is not present on a @Component or @Bean definition, eager initialization will occur. If present and set to true, the @Bean or @Component will not be initialized until referenced by another bean or explicitly retrieved from the enclosing BeanFactory. If present and set to false, the bean will be instantiated on startup by bean factories that perform eager initialization of singletons.
If Lazy is present on a @Configuration class, this indicates that all @Bean methods within that @Configuration should be lazily initialized. If @Lazy is present and false on a @Bean method within a @Lazy-annotated @Configuration class, this indicates overriding the 'default lazy' behavior and that the bean should be eagerly initialized
大义是:@Lazy是用来控制懒加载的,可以直接或间接地用在@Compent 和@Bean 上;如果设置为 true, 则被调用时才会被加载,设置为 false, 则启动时加载;@Configuration 的@Lazy为默认@Lazy, 但是@Bean 上的@Lazy 可以覆盖@Configuration 的@Lazy
欢迎追问和评论
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯