ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Spring Bean XML 설정 방법
    Spring 2025. 8. 12. 18:52

    XML 기본 포맷은 아래와 같다. beans 태그 안에 스프링 빈 설정을 작성한다.

    <?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 
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    </beans>
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           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 
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:annotation-config/>
    </beans>
    • xmlns:context="<http://www.springframework.org/schema/context>" : 빈 등록은 XML, 빈 설정은 자바 애노테이션으로 할 경우 이 포맷을 사용
    • <context:annotation-config/> : 빈 설정을 XML 파일이 아닌 빈 클래스의 애노테이션으로 설정하고자 할 때 사용

    빈 설정

    <bean id="studentDao" class="ems.member.dao.StudentDao" />
    
    • <bean/> 태그를 이용해 빈을 정의
    • id: 빈 이름 지정
    • class: 빈 타입 설정
    • scope: 빈의 scope 설정 (singleton/prototype)
    • primary: true를 지정하여 빈을 사용할 때 객체가 생성되도록 설정
    • init-method: 빈 객체가 생성될 때 호출할 메서드 설정
    • destroy-method: 빈 객체가 소멸될 때 호출할 메서드 설정

    자동 주입 설정 - autowire 설정

    클래스의 참조 타입 필드에 한해서 autowire 속성을 사용해 자동 주입되도록 설정할 수 있다.

    <bean id='obj2' class='com.atoz_develop.beans.TestBean1' autowire="byName"/>
    
    • autowire="byName" : 빈의 참조 타입 필드를 이용한 자동 주입
    • autowire="byType" : 빈의 타입을 이용한 자동 주입
    • autowire="byConstructor" : 빈의 생성자를 이용한 자동 주입

    DI(Dependency Injection) 설정

    [생성자 주입]

    <bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>
    
    <bean id="registerService" class="ems.member.service.StudentRegisterService">
        <constructor-arg ref="studentDao" />
    </bean>
    
    • 생성자가 여러 개인 경우 type 속성을 사용해서 어떤 생성자를 사용해서 빈 객체를 만들 것인지 구체적으로 명시할 수 있다.
      • 지정하지 않으면 기본적으로 String 타입의 파라미터를 갖는 생성자를 사용하게 된다.
    <bean id="obj1" class="com.atoz_develop.beans.TestBean">
    	<constructor-arg value="100" type="int"/>
    </bean>
    
    <bean id="obj2" class="com.atoz_develop.beans.TestBean">
    	<constructor-arg value="11.11" type="double"/>
    </bean>
    
    <bean id="obj3" class="com.atoz_develop.beans.TestBean">
    	<constructor-arg value="문자열" type="java.lang.String"/>
    </bean>
    

    객체 주입 시 <constructor-arg> 에 ref 속성을 사용하는 것 외에도 <constructor-arg> 하위에 <bean> 을 사용해서 빈을 직접 정의할 수 있다.

    <bean id="obj" class="com.atoz_develop.beans.TestBean">
    	<constructor-arg>
    		<bean class="com.atoz_develop.beans.DataBean"/>
    	</constructor-arg>
    	<constructor-arg>
    		<bean class="com.atoz_develop.beans.DataBean"/>
    	</constructor-arg>
    </bean>
    

    프로퍼티 주입

    <bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
    	<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
    	<property name="userId" value="scott" />
    	<property name="userPw" value="tiger" />
    </bean>
    
    • <property/> : value 속성을 사용해서 값을 설정
    • value 값이 길 경우 <property> 의 하위에 <value> 태그를 사용해서 값을 설정할 수도 있다.
    • 생성자 주입과 마찬가지로 ref 속성을 사용해서 설정할 수 있다.

    프로퍼티 주입 - List 타입

    <bean id="informationService" class="ems.member.service.EMSInformationService">
    	<property name="developers">
    		<list>
    			<value>Cheney.</value>
    			<value>Eloy.</value>
    			<value>Jasper.</value>
    			<value>Dillon.</value>
    			<value>Kian.</value>
    		</list>
    	</property>
    </bean>
    
    • List 타입은 <property>,<list> 태그를 사용한다.
    • <value type=’int’>: type 속성으로 타입을 지정해주는 것이 좋다.
    • 제네릭이 Class인 List의 주입은 다음과 같이 설정한다.
    <bean id='dataBean' class='com.atoz_develop.beans.DataBean' scope='prototype'/>
    
    <bean id='t1' class='com.atoz_develop.beans.TestBean'>
    <property name="list3">
    	<list>
    		<bean class='com.atoz_develop.beans.DataBean'/>
    		<bean class='com.atoz_develop.beans.DataBean'/>
    		<ref bean='dataBean'/>
    		<ref bean='dataBean'/>
    	</list>
    </property>
    </bean>
    

    JNDI 란

    Java Naming and Directory Interface로 Java에서 외부 리소스를 조회할 때 사용된다. 보통 애플리케이션 서버(WAS, WebLogic, JBoss, Tomcat 등) 에서 제공하는 JNDI DataSource를 참조할 때 활용된다.

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:jee="http://www.springframework.org/schema/jee"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans.xsd
               http://www.springframework.org/schema/jee
               http://www.springframework.org/schema/jee/spring-jee.xsd">
    
        <!-- JNDI를 통해 데이터소스 가져오기 -->
        <jee:jndi-lookup id="dataSource" jndi-name="jdbc/db"/>
    </beans>
    • id="dataSource" → dataSource라는 Spring Bean으로 등록됨
    • jndi-name="jdbc/db" → JNDI에 등록된 jdbc/db DataSource를 참조

    util:properties

    <util:properties> 는 외부 설정 프로퍼티를 이용할 때 사용된다.

    <util:properties id="프로퍼티 이름" location="프로퍼티 경로">
    

    web.xml에서 <context-param> 태그는 모든 서블릿과 필터가 공유하는 루트 스프링 컨테이너(= RootContext = ApplicationContext)를 정의하고, web.xml에 <listener> 태그에 등록된 ContextLoaderListener가 ApplicationContext.xml(또는 root-context.xml)에 따라 ApplicationContext를 생성한다.

Designed by Tistory.