SPRING/chapter02_DI

DI_10_annoConfig : AnnoConfig.JAVA/Person.JAVA/PersonInfo.JAVA/PersonMain.JAVA/applicationContext10.xml

GAWON 2023. 6. 26. 19:13
package org.joonzis.DI_10_annoConfig;

import java.util.HashSet;
import java.util.Set;

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

// sts 버전 3 에서 사용시 해당 프로젝트에 CGLIB 라이브러리 추가

@Configuration  //applicationContext와 같은 역할을 하는 자바 클래스
public class AnnoConfig {
	
	@Bean  //Bean을 만드는 어노테이션
	public Person human1() {  // <bean id="human1" class="org.joonzis.DI_10_annoConfig.Person">
		Set<String> hobbies = new HashSet<String>();
		hobbies.add("여행");
		hobbies.add("잠자기");
		hobbies.add("청소");
		
		Person person = new Person();
		person.setName("김씨");
		person.setAge(10);
		person.setHobbies(hobbies);
		
		return person;
		
	}
	
	@Bean(name = "human2")  //@Bean(name="bean의id")
	public Person abc() {  
		Set<String> hobbies = new HashSet<String>();
		hobbies.add("밥먹기");
		hobbies.add("음악듣기");
		hobbies.add("산책");
		
		Person person = new Person();
		person.setName("박씨");
		person.setAge(30);
		person.setHobbies(hobbies);
		
		return person;
		
	}
	
	@Bean
	public PersonInfo pInfo() {
		Set<String> hobbies = new HashSet<String>();
		hobbies.add("게임");
		hobbies.add("곤충채집");
		hobbies.add("물고기키우기");
		
		PersonInfo info = new PersonInfo();
		info.setPerson(new Person("최씨", 50 ,hobbies));
		
		return info;
		
	}
	
}
package org.joonzis.DI_10_annoConfig;

import java.util.Set;

public class Person {
	private String name;
	private int age;
	private Set<String> hobbies;
	
	public Person() {}

	public Person(String name, int age, Set<String> hobbies) {
		super();
		this.name = name;
		this.age = age;
		this.hobbies = hobbies;
	}

	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 Set<String> getHobbies() {
		return hobbies;
	}

	public void setHobbies(Set<String> hobbies) {
		this.hobbies = hobbies;
	}
	
	
}
package org.joonzis.DI_10_annoConfig;

public class PersonInfo {
	private Person person;
	
	public PersonInfo() {}
	
	public void info() {
		System.out.println("이름: " + person.getName());
		System.out.println("나이: " + person.getAge());
		System.out.println("취미: " + person.getHobbies());
		
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}
	
	
}
package org.joonzis.DI_10_annoConfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class PersonMain {
	public static void main(String[] args) {
		
		AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(AnnoConfig.class);
		
		Person person1 = ctx.getBean("human1", Person.class);
		System.out.println("이름 : " + person1.getName());
		System.out.println("나이 : " + person1.getAge());
		System.out.println("취미 : " + person1.getHobbies());
		
		System.out.println("====================================");
		
		Person person2 = ctx.getBean("human2", Person.class);
		System.out.println("이름 : " + person2.getName());
		System.out.println("나이 : " + person2.getAge());
		System.out.println("취미 : " + person2.getHobbies());
		
		System.out.println("====================================");
		
		PersonInfo personInfo = ctx.getBean("pInfo", PersonInfo.class);
		personInfo.info();
		
		
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
	
	<bean id="cBean1" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressList">
			<list>
				<value>서울시 구로구 구로동</value>
				<value>서울시 금천구 독산동</value>
			</list>
		</property>
	</bean>
	
	<bean id="cBean2" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressSet">
			<set value-type="java.lang.String">
				<value>서울시 구로구 구로동</value>
				<value>서울시 금천구 독산동</value>
				<value>서울시 금천구 독산동</value>
			</set>
		</property>
	</bean>
	
	<bean id="cBean3" class="org.joonzis.DI_9_collection.CollectionBean">
		<property name="addressMap">
			<map>
				<entry>
					<key><value>조재형</value></key>
					<value>서울시 금천구 독산동</value>
				</entry>
				<entry>
					<key><value>장동완</value></key>
					<value>시흥시 신천동</value>
				</entry>
			</map>
		</property>
	</bean>
	
</beans>