Spring

[Spring] XML기반의 Bean 설정(Configuration)

Noooodle 2024. 3. 21. 22:47

1. 기본 구조

 

<bean id="bean-ID" class="생성할-bean-객체의-class-이름(package-경로-포함)">
<!-- 의존 객체 주입 -->
</bean>

 

2. Bean 사용 방법

  • BeanFactory 또는 ApplicationContext 객체(container)에 대해 getBean() method 호출한다.
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("springIdol.xml");

TalentCompetition competition = ctx.getBean("springIdol", TalentCompetition.class);
Performer performer1 = (Performer) ctx.getBean("abc");

 

3. DI 방법

①생성자(Constructor)

  • 이용가능한 생성자가 bean 클래스에 정의되어 존재해야 한다.
  •  <constructor-arg>를 사용하여 생성자의 인자(argument)를 지정한다.
  • <value>값</value>
    • int, double, String 등 기본 데이터 타입의 값을 전달
  • <ref bean=“bean 식별자”/>
    • bean 속성으로 bean 객체의 참조를 전달
<bean id="math" class="springidol.Math" /> <!-- Bean class 생성 -->

<bean id="student" class="springidol.Student">
    <constructior-arg value="123" /> <!-- id -->
    <constructior-arg value="A" /> <!-- grade -->
    <constructior-arg ref="math" /> <!-- class, 위에서 만든 Bean 참조 -->
</bean>

이때 Student 클래스에 id, grade, class 를 정의하는 생성자가 있어야 한다.

 

<특징>

  • Bean 객체를 생성하는 시점에 모든 의존 객체가 주입된다.
  • 성능상 유리하다.
  • 객체를 사용하기 전에 DI가 이루어지므로 NullPointerException 발생 가능성이 낮다.
  • 생성자 인자들의 순서대로 의존 관계 주입(DI)을 해야한다.
  • 생성자 인자들의 개수가 많을 수록, 어떤 객체를 주입하는지 파악하기 어렵다.

 

②Setter

  • 이용가능한 setter method가 bean 클래스에 정의되어 존재해야 한다.
  •  <property>를 사용하여 생성자의 인자(argument)를 지정한다.
  • <value>값</value>
    • int, double, String 등 기본 데이터 타입의 값을 전달
  • <ref bean=“bean 식별자”/>
    • bean 속성으로 bean 객체의 참조를 전달
<bean id="math" class="springidol.Math" /> <!-- Bean class 생성 -->

<bean id="student" class="springidol.Student">
    <property name="id" value="123" /> 
    <property name="grade" value="A" />
    <property name="class" ref="math" /> <!-- 위에서 만든 Bean 참조 -->
</bean>

이때 Student 클래스에 id, grade, class에 관한 setter method가 있어야 한다.

 

<특징>

  • 생성자를 통해 bean 객체가 생성된 이후에 의존 객체가 주입된다.
  • setter들을 별도로 실행하므로 성능이 다소 저하된다.
  • setter의 실행이 누락된 경우 NullPointerException이 발생할 수 있다.
  • setter method별로 DI를 진행하므로 설정 정보를 명료하게 파악할 수 있다.

4. 기타 사항

  • Lookup Method 
  • Factory Method
  • Anonymous bean
  • Collection Wiring
  • SpEL(Spring Expression Language): 다른 bean에서 사용된 객체를 또다른 bean에서 참조하는 기능
<bean id="math" class="springidol.Math" /> <!-- Bean class 생성 -->

<bean id="student1" class="springidol.Student">
    <property name="id" value="123" /> 
    <property name="grade" value="A" />
    <property name="class" ref="math" />
</bean>

<bean id="student2" class="springidol.Student">
    <property name="id" value="124" /> 
    <property name="grade" value="#{student1.grade}" /> <!-- Spel 사용, student1 클래스에는 grade에 관한 getter method가 필요 -->
    <property name="class" ref="#{math}" /> <!-- Spel 사용 -->
</bean>

setter 방식으로 예시를 들었지만, 생성자 방식으로도 가능하다.

'Spring' 카테고리의 다른 글

JPA(2), ORM Annotation  (0) 2024.06.10
JPA(1), EntityManager  (0) 2024.06.01
[Spring]#12 이전 실습 정리&마무리  (0) 2024.02.22
[Spring]#11 객체 지향 원리와 적용  (1) 2024.02.18
[Spring]#10 순수 Java 개발 실습  (0) 2024.02.03