171218 AnnotationConfigApplicationContext - RYUDONGJIN/Memo_wiki GitHub Wiki

package com.javalec.springEx5_2;

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(80);
		
		return student;
	}
	
	@Bean
	public Student student2() {
		ArrayList<String> hobbys = new ArrayList<String>();
		hobbys.add("독서");
		hobbys.add("음악감상");
		
		Student student = new Student("홍길순", 18, hobbys);
		student.setHeight(160);
		student.setWeight(450);
		
		return student;
	}
}
package com.javalec.springEx5_2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainStudent {

	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()+"\n");
		
		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** ⚠️