강의_6장_03.pdf

실습 6-3

ApplicationConfig.java

package com.lgy.spring6_3;

import java.util.ArrayList;

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

@Configuration
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(84);
		return student;
	}
//	@Bean
//	public Student student2() {
//		ArrayList<String> hobbys = new ArrayList<String>();
//		hobbys.add("독서");
//		hobbys.add("영화감상");
//		Student student = new Student("홍길순", 18, hobbys);
//		student.setHeight(170);
//		student.setWeight(55);
//		return student;
//	}

}

Student.java

package com.lgy.spring6_3;

import java.util.ArrayList;

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

@Configuration
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(84);
		return student;
	}
//	@Bean
//	public Student student2() {
//		ArrayList<String> hobbys = new ArrayList<String>();
//		hobbys.add("독서");
//		hobbys.add("영화감상");
//		Student student = new Student("홍길순", 18, hobbys);
//		student.setHeight(170);
//		student.setWeight(55);
//		return student;
//	}

}

applicationCTX.xml

20230519173701.png

context 체크!

ApplicationConfig.java

package com.lgy.spring6_3;

import java.util.ArrayList;

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

@Configuration
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(84);
		return student;
	}
//	@Bean
//	public Student student2() {
//		ArrayList<String> hobbys = new ArrayList<String>();
//		hobbys.add("독서");
//		hobbys.add("영화감상");
//		Student student = new Student("홍길순", 18, hobbys);
//		student.setHeight(170);
//		student.setWeight(55);
//		return student;
//	}

}

MainClass.java

package com.lgy.spring6_3;

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 클래스로 자식 자바 스프링 설정파일 정보 가져옴
//		AnnotationConfigApplicationContext ctx =new AnnotationConfigApplicationContext(ApplicationConfig.class);
		AbstractApplicationContext ctx = new GenericXmlApplicationContext("classpath:applicationCTX.xml");
		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());
	}
}

20230519174311.png

실습 6-4

ApplicationConfig.java