더북(TheBook)

인스턴스 생성 방식을 싱글턴에서 비싱글턴으로 변경하는 것은 간단합니다. 다음은 XML에서 애너테이션이 사용되는 방식의 구성 예제입니다.

예제 3-104 인스턴스 생성 방식을 싱글턴에서 비싱글턴으로 변경([[bean-instantiation-mode]] app-context-xml.xml, Singer.java)

<!-- app-context-xml.xml -->
<beans ...>
    <bean id="nonSingleton" class="com.apress.prospring5.ch3.annotated.Singer"
        scope="prototype" c:_0="John Mayer"/>
</beans>

//Singer.java
package com.apress.prospring5.ch3.annotated;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("nonSingleton")
@Scope("prototype")
public class Singer {

    private String name = "unknown";

    public Singer(@Value("John Mayer") String name) {
        this.name = name;
    }

}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.