2025-01-22




INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [person.xml]
INFO : org.springframework.context.support.GenericXmlApplicationContext - Refreshing org.springframework.context.support.GenericXmlApplicationContext@32cf48b7: startup date [Wed Jan 22 17:45:30 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38102d01: defining beans [person1,person2,info]; root of factory hierarchy
이 름 >>> 유관순
주민번호 >>> 2025-0001
성 별 >>> 여성
나 이 >>> 19
연락처 >>> 010-2222-2222
INFO : org.springframework.context.support.GenericXmlApplicationContext - Closing org.springframework.context.support.GenericXmlApplicationContext@32cf48b7: startup date [Wed Jan 22 17:45:30 KST 2025]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38102d01: defining beans [person1,person2,info]; root of factory hierarchy
package com.spring.di04;
import org.springframework.context.support.GenericXmlApplicationContext;
public class Main {
public static void main(String[] args) {
GenericXmlApplicationContext container =
new GenericXmlApplicationContext("person.xml");
PersonInfo person = (PersonInfo)container.getBean("info");
// 비지니스 로직 호출
person.getPersonInfo();
container.close();
}
}
package com.spring.di04;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String name;
private String juminNo;
private String gender;
private int age;
private String phone;
}
package com.spring.di04;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
@NoArgsConstructor
@AllArgsConstructor
public class PersonInfo {
private Person person;
// 비지니스 로직
public void getPersonInfo() {
if (person != null) {
System.out.println("이 름 >>> " + person.getName());
System.out.println("주민번호 >>> " + person.getJuminNo());
System.out.println("성 별 >>> " + person.getGender());
System.out.println("나 이 >>> " + person.getAge());
System.out.println("연락처 >>> " + person.getPhone());
}
}
}
<?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="person1" class="com.spring.di04.Person">
<constructor-arg value="홍길동"/>
<constructor-arg value="2025-0000"/>
<constructor-arg value="남성"/>
<constructor-arg value="27"/>
<constructor-arg value="010-1111-1111"/>
</bean>
<!-- setter() 메서드로 데이터 초기화 -->
<bean id="person2" class="com.spring.di04.Person">
<property name="name" value="유관순"></property>
<property name="juminNo" value="2025-0001"></property>
<property name="gender" value="여성"></property>
<property name="age" value="19"></property>
<property name="phone" value="010-2222-2222"></property>
</bean>
<bean id="info" class="com.spring.di04.PersonInfo">
<constructor-arg>
<!-- person2 참조 -->
<ref bean="person2"/>
</constructor-arg>
</bean>
</beans>'Spring, Boot > 기초 내용 정리' 카테고리의 다른 글
| Spring(MVC)_01_01 (0) | 2025.01.22 |
|---|---|
| Spring(DI)_02_05 (0) | 2025.01.22 |
| Spring(DI)_02_03 (0) | 2025.01.21 |
| Spring(DI)_02_02 (0) | 2025.01.21 |
| Spring(DI)_02_01 (0) | 2025.01.21 |