Student.java

package com.lgy.spring6_2;

import java.util.ArrayList;

public class Student {
	private String name; 
	private int age;
	private ArrayList<String> hobbys;
	private double height;
	private double weight;
	
	public Student(String name, int age, ArrayList<String> hobbys) {
		this.name = name;
		this.age = age;
		this.hobbys = hobbys;
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public ArrayList<String> getHobbys() {
		return hobbys;
	}
	public void setHobbys(ArrayList<String> hobbys) {
		this.hobbys = hobbys;
	}
	public double getHeight() {
		return height;
	}
	public void setHeight(double height) {
		this.height = height;
	}
	public double getWeight() {
		return weight;
	}
	public void setWeight(double weight) {
		this.weight = weight;
	}
}

ApplicationConfig.java

package com.lgy.spring6_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(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_2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainClass {
	public static void main(String[] args) {
//		AnnotationConfigApplicationContext 클래스로 자식 자바 스프링 설정파일 정보 가져옴
		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());
	}
}

그냥 돌리면 오류 발생

20230519170008.png

pom.xml 해당 위치에 붙여 넣기

20230519170326.png

<!-- cglib  -->
		<dependency>
			<groupId>com.kenai.nbpwr</groupId>
			<artifactId>net-sf-cglib</artifactId>	
			<version>2.1.3-201003011305</version>	
		</dependency>

20230519171210.png