spring bean 생 성 세부 사항

1) 대상 생 성:
일례    scope = "singleton", 기본 값, 즉 기본 값 은 단일 예 [service / dao / 도구 류] 입 니 다.   scope = "prototype", 여러 예;[액 션 대상]
2) 언제 만 듭 니까?
  scope="prototype"  대상 을 사용 할 때 만 대상 을 만 듭 니 다.  scope="singleton"  시작 (용기 초기 화 전) 에 bean 이 만 들 어 졌 고 전체 응용 프로그램 은 하나 입 니 다.
3) 생 성 지연 여부
 lazy-init="false"  기본 값 은 false, 생 성 지연 없 이 시작 할 때 대상 을 만 듭 니 다. lazy-init="true"   초기 화 지연, 대상 을 사용 할 때 만 대상 을 만 듭 니 다.  (lazy - init 는 하나의 예 에 만 유효 합 니 다)       
4) 대상 생 성 후 초기 화 / 폐기
 init-method="init_user"  [대상 에 대응 하 는 init user 방법, 대상 생 성 후 실행]  destroy-method="destroy_user"  
[용기 대상 의 destriy 방법 을 호출 할 때 실행 합 니 다. (용기 용 구현 클래스), scope = "singleton" 시 유효 합 니 다.]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

	<bean id="userId" class="b_bean.User" scope="singleton" lazy-init="false"
		init-method="init_user" destroy-method="destroy_user"></bean>
		
</beans>      

  
package b_bean;

public class User {
	private int id;
	private String name;

	public User() {
		System.out.println("User    ....");
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void init_user(){
		System.out.println("init_user.....");
	}
	
	public void destroy_user(){
		System.out.println("destroy_user.....");
	}

}
package b_bean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CreateBean {
	
	@Test
	public void test(){
		//ApplicationContext context=new ClassPathXmlApplicationContext("b_bean/applicationContext.xml");
		//    destroy-method,      
		ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b_bean/applicationContext.xml");
		System.out.println("ApplicationContext      ....");
		
		User user1=(User) context.getBean("userId");
		User user2=(User) context.getBean("userId");
		System.out.println(user1);
		System.out.println(user2);
		
		context.destroy();
	}
}

좋은 웹페이지 즐겨찾기