본문 바로가기
Web Programming/Spring

Spring/지시서 작성하기/Bean Configuration File

by hyeon-H 2021. 5. 17.
728x90
반응형

패키지를 우클릭해서 Bean Configuration File을 생성하면,

Bean Configuration File클릭

 

XML파일이 만들어진다.

메인클래스에서 Exam exam = new NewlecExam() 라고 클래스를 생성했다면,

XML 지시서에서는 <bean>을 사용하여 클래서 생성을 다음처럼 지시한다.

<?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.xsd">
	
	<bean id="exam" class="spring.di.entity.NewlecExam"></bean>
	<bean id="console" class="spring.di.ui.GridExamConsole">
    		<property name="exam" ref="exam"></property>
   	</bean>
</beans>

id : 생성하는 객체를 어떠한 이름으로 사용 할 것인지, 즉 변수명이다.
class : 어떠한 클래스를 객체화 할 것인지를 작성하는데,
         동명의 객체가 있을 수도 있으니, 꼭 패키지명도 함께 알려준다.

이렇게 부품이 되는 객체를 생성했다면, 결합을 시켜줘야한다.

메인클래스에서 console.setExam(exam)이라고 결합을 시켜줬다면,
스프링에서는 <property>를 사용한다.
<property>에서 Setter함수를 호출한다고 생각하면 된다.


<property name="setExam(exam)"></property>이라는 라고 볼 수 있는데,

<property name="exam" ref="exam"></property> 이렇게 변형되는 것이다.


규칙이라고 할수있는데 setExam(exam)에서 set은 때고 대문자는 소문자로 변형해서 exam으로 바꾸고
매개변수는 int, Sring 등의 자료형이라면 value="매개변수명"으로 작성하고,
클래스의 인스턴스가 들어가므로 ref="클래스명"으로 작성해준다.

	<bean id="exam" class="spring.di.entity.NewlecExam"></bean>
	<bean id="console" class="spring.di.ui.GridExamConsole">
    		<property name="exam" ref="exam"></property>
   	</bean>
728x90
반응형