171218 xml파일을 Configuration class에 포함 - RYUDONGJIN/Memo_wiki GitHub Wiki

<?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="student2" class="com.javalec.springEx6_2.Student">
		<constructor-arg value="홍길순"></constructor-arg>
		<constructor-arg value="30"></constructor-arg>
		<constructor-arg >
			<list>
				<value>마라톤</value>
				<value>요리</value>
			</list>
		</constructor-arg>
		<property name="height" value="190" />
		<property name="weight" value="70" />
	</bean>
</beans>
package com.javalec.springEx6_2;

import java.util.ArrayList;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:applicationCTX6_2.xml")
public class ApplicationConfig {

	@Bean
	public Student student1(){
		
		ArrayList<String> hobbys = new ArrayList<String>();
		hobbys.add("수영");
		hobbys.add("요리");
		
		Student student = new Student("홍길동", 20, hobbys);
		student.setHeight(180);
		student.setWeight(80);
		
		return student;
	}	
}
package com.javalec.springEx6_2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {

		public static void main(String[] args) {
			
			AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfig.class);
			
			Student student1 = ctx.getBean("student1", Student.class);
			System.out.println("이름 : " + student1.getName());
			System.out.println("나이 : " + student1.getAge());
			System.out.println("취미 : " + student1.getHobbys());
			System.out.println("키 : " + student1.getHeight());
			System.out.println("몸무게 : " + student1.getWeight());
			
			Student student2 = ctx.getBean("student2", Student.class);
			System.out.println("이름 : " + student2.getName());
			System.out.println("나이 : " + student2.getAge());
			System.out.println("취미 : " + student2.getHobbys());
			System.out.println("키 : " + student2.getHeight());
			System.out.println("몸무게 : " + student2.getWeight());
			
			ctx.close();			
		}	
}
⚠️ **GitHub.com Fallback** ⚠️