spring                
            봄 게으른 초기화
        
        
            
    수색…
구성 클래스에서 지연 초기화
@Configuration
// @Lazy - For all Beans to load lazily
public class AppConf {
    @Bean
    @Lazy
    public Demo demo() {
        return new Demo();
    }
}
구성 요소 스캐닝 및 자동 배선용
@Component
@Lazy
public class Demo {
    ....
    ....
}
@Component
public class B {
    @Autowired
    @Lazy // If this is not here, Demo will still get eagerly instantiated to satisfy this request.
    private Demo demo;
    .......
 }
Spring에서의 Lazy Init의 예
 @Lazy 를 사용하면 IOC 컨테이너에 Bean 초기화 지연을 지시 할 수 있습니다. 기본적으로 Bean은 IOC 컨테이너가 생성되는 즉시 인스턴스화됩니다. @Lazy 사용하면이 인스턴스화 프로세스를 변경할 수 있습니다. 
Spring의 lazy-init은 bean 태그의 속성입니다. lazy-init의 값은 true와 false입니다. lazy-init가 true이면 bean에 대한 요청이있을 때 해당 bean이 초기화됩니다. Spring 컨테이너가 초기화 될 때이 빈은 초기화되지 않을 것이다. lazy-init가 false 인 경우, bean 컨테이너는 스프링 컨테이너 초기화로 초기화되고 이는 기본 동작입니다.
app-conf.xml
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
 
<bean id="testA" class="com.concretepage.A"/>
<bean id="testB" class="com.concretepage.B" lazy-init="true"/>
A.java
package com.concretepage;
public class A {
public A(){
    System.out.println("Bean A is initialized");
   }
}
B.java
package com.concretepage;
public class B {
public B(){
    System.out.println("Bean B is initialized");
   }
} 
SpringTest.java
package com.concretepage;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
     ApplicationContext context = new ClassPathXmlApplicationContext("app-conf.xml");
     System.out.println("Feth bean B.");
     context.getBean("testB");
   }
}
산출
Bean A is initialized
Feth bean B.
Bean B is initialized
Modified text is an extract of the original Stack Overflow Documentation
        아래 라이선스 CC BY-SA 3.0
        와 제휴하지 않음 Stack Overflow