SpringIOC 용기 에서 bean 의 역할 범위 와 생명 주 기 를 상세 하 게 설명 합 니 다.
scope 속성 을 통 해 설정 할 수 있 습 니 다:
웹 응용 요청 범위
<!-- (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 의 차이
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 의 역할 범위 와 수명 주기 내용 은 우리 의 이전 글 을 검색 하거나 아래 의 관련 글 을 계속 조회 하 시기 바 랍 니 다.앞으로 많은 응원 부탁드립니다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
SpringIOC 프레임 워 크 의 간단 한 실현 절차두 가지 실현 방법 이 있 습 니 다.하 나 는 xml 파일 을 설정 하여 실현 하 는 것 입 니 다.다른 하 나 는 매개 변 수 를 통 해 이 루어 지 는 것 입 니 다.간단 한 예 를 찾 아 우리 가 손 을 써 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.