SpringIOC 용기 에서 bean 의 역할 범위 와 생명 주 기 를 상세 하 게 설명 합 니 다.

bean 의 역할 범위:
scope 속성 을 통 해 설정 할 수 있 습 니 다:
  • singleton 단일 사례 의(기본 값)
  • 프로 토 타 입
    웹 응용 요청 범위
  • session 은 웹 응용 세 션 범위 에 작용 합 니 다
  • global-session 은 클 러 스 터 환경의 세 션 범위(전체 세 션 범위)에 작용 합 니 다
  • 테스트:
    
    <!--       (singleton)-->
    <bean id="human" class="com.entity.Human"></bean>
    
    <bean id="human" class="com.entity.Human" scope="singleton"></bean>
    
    @Test
     public void test(){
      //  ClassPathXmlApplicationContext           javabean    spring   
      ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
      //  Spring    bean  ,  id        
      Human human = applicationContext.getBean("human", Human.class);
      Human human1 = applicationContext.getBean("human", Human.class);
      System.out.println(human==human1);
     }
    
    결과:
    在这里插入图片描述
    scope 속성 을 prototype 으로 설정 할 때
    
      <bean id="human" class="com.entity.Human" scope="prototype"></bean>
    결과:
    在这里插入图片描述
    singleton 과 prototype 의 차이
  • bean 속성 이 singleton 으로 설정 되 었 을 때 설정 파일 을 불 러 올 때 대상 이 초기 화 되 었 습 니 다
  • 프로 토 타 입 을 사용 할 때 대상 의 생 성 은 우리 가 언제 bean 을 가 져 올 때 대상 을 만 들 때 입 니 다
  • prototype 으로 설정 되 었 을 때
    在这里插入图片描述
    在这里插入图片描述
    singleton 으로 설정 되 었 을 때
    在这里插入图片描述
    bean 대상 의 생명주기
    단일 개체:
    출생:용기 생 성 시 대상 출생살 아 있 는 용기 만 있 고 대상 은 살아 있다.
    사망:용기 판매 자,대상 사망4.567917.단일 대상 과 용기 의 생명 주기 가 같다.
    테스트:
    속성 init-method 와 destroy-method 를 설정 하고 person 클래스 에 두 가지 방법 을 기록 하여 출력 인쇄 합 니 다.
    
     public void init(){
      System.out.println("   ...");
     }
    
     public void destroy(){
      System.out.println("   ...");
     }
    
    <bean id="person" class="com.entity.Person" scope="singleton" init-method="init" destroy-method="destroy">
       
      </bean>
    테스트 클래스:
    
    @Test
     public void test(){
      //  ClassPathXmlApplicationContext           javabean    spring   
      ClassPathXmlApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
    //  //  Spring    bean  ,  id        
      Person person = Context.getBean("person", Person.class);
      //    
      Context.close();
     }
    결과:
    在这里插入图片描述
    요약:단일 대상 과 용기 수명 주기 가 같 습 니 다.
    속성 이 prototype 여러 예 로 바 뀌 었 을 때
    출생:우리 가 대상 을 사용 할 때 spring 프레임 워 크 를 만들어 줍 니 다4.567917.살 아 있 는 대상 은 사용 하 는 과정 에서 만 살아 있다.
    4.567917.사망:대상 이 장시간 사용 하지 않 고 다른 대상 이 응용 되 지 않 을 때 자바 쓰레기 회수 기 회수 대상테스트 클래스:
    
    @Test
     public void test(){
      //  ClassPathXmlApplicationContext           javabean    spring   
      ClassPathXmlApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml");
    //  //  Spring    bean  ,  id        
      Person person = Context.getBean("person", Person.class);
      //    
      Context.close();
     }
    결과:
    在这里插入图片描述
    결론:Spring 용 기 는 여러 대상 이 언제 사용 되 고 언제 다 쓸 수 있 는 지 모 르 기 때문에 우리 만 알 고 있 기 때문에 대상 을 쉽게 소각 하지 않 습 니 다.자바 쓰레기 회수 기 를 통 해 대상 을 회수 합 니 다.
    SpringIOC 용기 에서 bean 의 역할 범위 와 수명 주기 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 SpringIOC 용기 bean 의 역할 범위 와 수명 주기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 부탁드립니다!

    좋은 웹페이지 즐겨찾기