본문 바로가기
Programming/Spring

[Spring] 설정 파일

by NAMP 2016. 4. 18.

Spring 설정 파일

Spring 설정 파일 기본

Spring 설정파일 구조

Bean 설정

속성 설명
id Bean 의 구분을 위한 정보로 해당 bean에 접근하기 위한 key임
class 정의된 Bean의 실제 구현 클래스로 항상 full name 으로 작성

Bean 설정관련 속성

init-method

함수 생성

public void init(){
	System.out.println("초기화");
}

bean 설정

<bean id="s" class="tv.STV" init-method="init"/>

빈이 InitializaingBean 을 구현하고 있다면 afterPropertiesSet() 메소드를 호출한다. 자동호출 된다.

import org.springframework.beans.factory.InitializingBean;

public class STV implements TV, InitializingBean{

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitializingBean: afterPropertiesSet()");
    }
}

실행 결과

STV 생성
InitializingBean: afterPropertiesSet()
초기화

destroy-method 속성

factory-method 속성

Calendar c = Calendar.getInstance();

생성자 함수가 아닌 다른 방법으로 생성

<bean id="c" class="java.util.Calendar" factory-method="getInstance"/>

lazy-init 속성

pre loading 을 막는다.

lazy-init 설정

<bean id="l" class="tv.LTV" lazy-init="true"/>

Bean Scope

TV r1 = (TV) context.getBean("s");
TV r2 = context.getBean("s", TV.class);
System.out.println("default scope => singleton? "+ (r1 == r2));

scope 속성

singleton scope

protytype scope


댓글