도담도담

xml 기본 틀 본문

IT 공부/KH 정보교육원

xml 기본 틀

Zinisang 2021. 6. 13. 22:37

기본 생성 틀

<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.xsd">

</beans>


이거는 xml 초반에 써줘야할 기본 코드, 이 내부에 bean 객체들을 작성해준다.


Case

설정 파일을 통한 DI (의존성 주입)


1. 생성자를 통한 DI 기본 방식

<bean ~~>
	<constructor-arg></constructor-arg>
</bean>


만약 생성자를 통해 객체를 주입할 경우

<constructor-arg ref="객체명"></constructor-arg>

또는

<constructor-arg>
	<ref bean="객체명" />
</constructor-arg>


생성자를 통해 일반 데이터를 주입할 경우

<constructor-arg value="데이터"></constructor-arg>

또는

<constructor-arg>
	<value>데이터</value>
</constructor-arg>


데이터 타입을 지정할 경우

<constructor-arg>
	<value type="데이터타입">데이터</value>
</constructor-arg>


생성자를 통해 여러개의 데이터를 전달할 경우(생성자의 매개변수 순서대로...)

<constructor-arg value="데이터"></constructor-arg>
<constructor-arg value="데이터"></constructor-arg>
<constructor-arg ref="객체"></constructor-arg>

 




2. private 변수의 setter를 통해 주입할 경우

<bean ~~~>
	<property name="멤버변수명" ref="객체명" />
</bean>


여러개의 private 멤버변수에게 전달할 경우

<bean ~~~>
	<property name="멤버변수명" ref="객체명" />
	<property name="멤버변수명" ref="객체명" />
	<property name="멤버변수명" value="데이터" />
</bean>

 



3. <property ~~/> 를 사용하지 않고, xmlns(xml namespace)를 이용하여 지정할 경우

<bean ~~~ 태그 내부에 xmlns:p="~~~~"를 추가 >
	<bean ~~~ p:변수명-ref="객체명">
	</bean>
</bean>

 



4. 의존성 주입되는 객체를 1회 사용할 경우

<bean ~~~ >
	<property ~~~>
		<bean class="~~~"></bean>  <!-- 주입될 객체 -->
	</property>
</bean>


이 떄 주입되는 객체는 이름(즉, 객체명)이 beans 에 등록되지 않기 때문에..
이외의 <bean ~~~></bean> 에서는 사용할 수 없다!!!(id를 설정해도 마찬가지!!)

Comments