📃InitDestroyMethodBean.java(클래스)
※ xyz.itwill04.bean 패키지에 InitDestroyMethodBean.java 클래스 생성
package xyz.itwill04.bean; // public class InitDestroyMethodBean { public InitDestroyMethodBean() { // TODO Auto-generated constructor stub System.out.println("### InitDestroyMethodBean 클래스의 기본 생성자 호출 ###"); } // //객체 생성 후 객체의 초기화 작업(필드의 초기값 설정)을 위해 한번만 자동 호출되는 메소드 public void init() { System.out.println("### InitDestroyMethodBean 클래스의 init() 메소드 호출 ###"); } // //객체 소멸 전 객체의 마무리 작업을 위해 한번만 자동 호출되는 메소드 public void destroy() { System.out.println("### InitDestroyMethodBean 클래스의 destroy() 메소드 호출 ###"); } // public void display() { System.out.println("### InitDestroyMethodBean 클래스의 display() 메소드 호출 ###"); } }
📌init-method 속성 : 스프링 컨테이너에 의해 객체(Spring Bean)가 생성된 후 한번만 한번만 자동 호출되어 생성 객체의 초기화 작업을 실행하기 위한 메소드의 이름을 속성값으로 설정
→ init-method 속성값은 이클립스의 자동 완성 기능을 사용하여 작성 가능
📌destroy-method 속성 : 스프링 컨테이너에 의해 객체(Spring Bean)가 소멸되기 전 한번만 자동 호출되어 객체의 마무리 작업을 실행하기 위한 메소드의 이름을 속성값으로 설정
→ destroy-method 속성값은 이클립스의 자동 완성 기능을 사용하여 작성 가능
〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
📌lazy-init 속성 : false 또는 true 중 하나를 속성값으로 설정
→ false(기본) : 스프링 컨테이너를 초기화할 때 객체(Spring Bean)를 미리 생성
→ true : 스프링 컨테이너로부터 Spring Bean를 제공받기 위해 getBean()메소드를 호출
〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
📢스프링 컨테이너는 Spring Bean Configuration File에 등록된 모든 클래스를 리플렉션 기술을 사용하여 미리 객체(Spring Bean)로 생성
→ 리플렉션 기술을 사용하면 클래스의 접근 지정자에 상관없이 모든 요소에 접근 가능
→ 생성자가 은닉화 선언되어 있어도 스프링 컨테이너는 클래스의 생성자로 객체 생성 가능
📢Spring Bean Configuration File에 등록된 싱글톤 클래슨느 클래스가 메모리에 로딩된 후 정적영역의 명령을 실행하여 객체를 생성하고 스프링 컨테이너에 의해 객체를 다시 생성
→ 싱글톤 클래스에 의해 객체가 2개 생성 - 싱글톤 클래스의 작성 규칙 위반
📌factory-method 속성 : 싱글톤 클래스에서 객체를 반환하는 메소드의 이름을 속성값으로 설정
→ 스프링 컨테이너에 의해 객체를 생성하지 않고 정적영역의 명령으로 객체를 생성하여 사용
〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
📢스프링 컨테이너는 bean 엘리먼트의 선언 순서대로 등록된 클래스를 객체로 생성
📌depends-on 속성 : Spring Bean을 구분하기 위한 식별자(beanName)를 속성값으로 설정
→ bean 엘리먼트에 등록된 클래스를 객체로 생성하기 전에 depends-on 속성값으로 설정된 Spring Bean의 클래스를 객체로 미리 생성
〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
📌scope 속성 : singleton(기본), prototype, request, session 중 하나를 속성값으로 설정
📢singleton 또는 prototype : 객체(Spring Bean)의 생성 갯수를 설정하는 속성값
→ singleton : 스프링 컨테이너가 bean 엘리먼트에 등록된 클래스로 객체를 하나만 생성하여 제공
→ prototype : 스프링 컨테이너가 bean 엘리먼트에 등록된 클래스로 객체를 여러개 생성하여 제공
→ scope 속성값을 [prototype]으로 설정할 경우 lazy-init 속성값을 반드시 [true]로 설정
📢request 또는 session : 객체의 사용 범위를 설정하는 속성값 - 웹프로그램 작성시에만 사용📃04-2_beanAttribute.xml
※ src/main/resources 폴더에 04-2_beanAttribute.xml 생성
코드를 입력하세요
📃BeanAttributeApp.java(클래스)
※ xyz.itwill04.bean 패키지에 BeanAttributeApp.java 클래스 생성
package xyz.itwill04.bean; // import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // public class BeanAttributeApp { public static void main(String[] args) { System.out.println("============== Spring Container 초기화 전 =============="); ApplicationContext context=new ClassPathXmlApplicationContext("04-2_beanAttribute.xml"); System.out.println("============== Spring Container 초기화 후 =============="); //ApplicationContext.getBean(String beanName) : 스프링 컨테이너에서 Spring Bean을 구분하기 위한 식별자(beanName)을 전달하여 객체(Spring Bean)를 반환하는 메소드 //→ Object 타입의 객체를 반환하므로 반드시 명시적 객체 형변환 사용 //InitDestroyMethodBean bean=(InitDestroyMethodBean)context.getBean("initDestroyMethodBean"); // //ApplicationContext.getBean(String beanName, Class<T> clazz) : 스프링 컨테이너에게 Spring Bean를 구분하기 위한 식별자(beanName)와 Class 객체(Clazz)를 // 전달하여 원하는 클래스 타입의 객체(Spring Bean)로 변환하여 반환하는 메소드 InitDestroyMethodBean bean=context.getBean("initDestroyMethodBean", InitDestroyMethodBean.class); // //bean.init(); bean.display(); //bean.destroy(); // System.out.println("========================================================"); context.getBean("lazyInitBean"); System.out.println("========================================================"); ((ClassPathXmlApplicationContext)context).close(); System.out.println("========================================================"); } }
📃LazyInitBean.java(클래스)
※ xyz.itwill04.bean 패키지에 LazyInitBean.java 클래스 생성
package xyz.itwill04.bean; // public class LazyInitBean { public LazyInitBean() { // TODO Auto-generated constructor stub System.out.println("### LazyInitBean 클래스의 기본 생성자 호출 ###"); } }
📃FactoryMethodBean.java(클래스)
※ xyz.itwill04.bean 패키지에 FactoryMethodBean.java 클래스 생성
package xyz.itwill04.bean; // //싱글톤 디자인 패턴을 적용하여 작성된 클래스 - 싱글톤 클래스(Singleton Class) //→ 프로그램에 필요한 객체를 하나만 제공하기 위한 목적의 클래스 작성시 사용하는 디자인 패턴 public class FactoryMethodBean { private static FactoryMethodBean _bean; // public FactoryMethodBean() { // TODO Auto-generated constructor stub System.out.println("### FactoryMethodBean 클래스의 기본 생성자 호출 ###"); } static { _bean=new FactoryMethodBean(); } public static FactoryMethodBean getfaFactoryMethodBean() { System.out.println("*** FactoryMethodBean 클래스의 getfaFactoryMethodBean() 메소드 호출 ***"); return _bean; } }
📃DependsOnOneBean.java(클래스)
※ xyz.itwill04.bean 패키지에 DependsOnOneBean.java 클래스 생성
package xyz.itwill04.bean; // public class DependsOnOneBean { public DependsOnOneBean() { // TODO Auto-generated constructor stub System.out.println("### DependsOnOneBean 클래스의 기본 생성자 호출 ###"); } }📃DependsOnTwoBean.java(클래스)
※ xyz.itwill04.bean 패키지에 DependsOnTwoBean.java 클래스 생성
package xyz.itwill04.bean; // public class DependsOnTwoBean { public DependsOnTwoBean() { // TODO Auto-generated constructor stub System.out.println("### DependsOnTwoBean 클래스의 기본 생성자 호출 ###"); } }
📃ScopeBean.java(클래스)
※ xyz.itwill04.bean 패키지에 ScopeBean.java 클래스 생성
package xyz.itwill04.bean; // public class ScopeBean { public ScopeBean() { // TODO Auto-generated constructor stub System.out.println("### ScopeBean 클래스의 기본 생성자 호출 ###"); } }