언어/SPRING
[SPRING] 싱글턴 만들기
Sime
2016. 11. 8. 09:54
xml에서 싱글턴을 bean에 등록 하려면 싱글턴 클래스 하나 만들고
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package di04_02; public class Singleton { private static Singleton instance; private Singleton(){} public static Singleton getInstance(){ if(instance == null) instance = new Singleton(); return instance; } } | cs |
xml에 factory-method 사용해서 객체 생성
factory-method는 기본 생성자도 없고 다른 생성자도 없는 상황에서 객체를 생성해줌
1 2 3 4 5 6 7 8 9 10 11 | <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> <bean id="singleton" class="di04_02.Singleton" factory-method="getInstance"></bean> </beans> | cs |
반응형