스프링 프레임워크

스프링 컨테이너에서 미리 객체를 생성해두고 요청시 가져와 사용하는 시스템

xml파일에 클래스를 등록해 객체를 생성하도록 하고,

자바 파일에서는 직접 new 키워드로 객체를 생성하지 않고 바로 사용할 수 있다

 


xml파일이 여러개일 경우, 배열로 관리한다

String [] configLocation = new String[] {"applicationContext.xml","app.xml"};

 


p네임스페이스, c네임스페이스

property태그를 대체하여 p를,

constructor-arg태그를 대체하여 c네임스페이스를 사용할 수 있다

p:필드명="값"의 형태로 쓴다

이때 -ref는 객체가 참조된다는 의미이다

<!--xml 환경설정파일-->

<!--p네임스페이스-->
<bean id="car" class="spring.Car" p:periodTime="10" p:w-ref="wheel">
<!-- 	<property name="periodTime"> <value>10</value> </property>
	<property name="w"> <ref bean="wheel"/></property> 	 -->
</bean>
<bean id="wheel" class="spring.Wheel" />

<!--c네임스페이스-->
<bean id="tv" class="spring.Tv" c:count="10" c:m-ref="moniter">
<!-- 	<constructor-arg value="10" />
	<constructor-arg><ref bean="moniter" />
	</constructor-arg> -->
</bean>
<bean id="moniter" class="spring.Moniter" />

id값은 클래스명과 같게, 첫 글자는 소문자로 설정한다

 


scope="prototype"

기본적으로는 싱글톤 객체로,

scope를 설정하지 않으면 scope="singleton"으로 지정된다

scope="prototype"의 경우 매번 새로운 객체가 생성된다

 

<!--xml 환경설정파일-->
<bean id="tv" class="spring.Tv" p:periodTime="30" scope="prototype">
	<property name="m">
		<ref bean="moniter"/>
	</property> 
</bean>

 

 

 

+ Recent posts