Notice
Recent Posts
Recent Comments
Link
«   2025/03   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Tags
more
Archives
Today
Total
관리 메뉴

WON.dev

DI_11_annoConfig : AnnoConfig.JAVA/Person.JAVA/PersonMain.JAVA/applicationContext11.xml 본문

SPRING/chapter02_DI

DI_11_annoConfig : AnnoConfig.JAVA/Person.JAVA/PersonMain.JAVA/applicationContext11.xml

GAWON 2023. 6. 27. 09:24
package org.joonzis.DI_11_annoConfig;

import java.util.ArrayList;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AnnoConfig {

	@Bean  
	public Person person1() {  
		ArrayList<String> hobbies = new ArrayList<String>();
		hobbies.add("드라이브");
		hobbies.add("술먹기");
		hobbies.add("볼링치기");
		
		Person person = new Person("김씨",hobbies,170.0);
		
		return person;
		
	}
	
	@Bean  
	public Person person2() {  
		ArrayList<String> hobbies = new ArrayList<String>();
		hobbies.add("눕기");
		hobbies.add("게임");
		hobbies.add("포켓볼");
		
		Person person = new Person();
		person.setName("양씨");
		person.setHobbies(hobbies);
		person.setHeight(150.0);
		
		return person;
		
	}

	
}
package org.joonzis.DI_11_annoConfig;

import java.util.ArrayList;

public class Person {
	private String name;
	private ArrayList<String> hobbies;
	private double height;
	
	public Person() {}

	public Person(String name, ArrayList<String> hobbies, double height) {
		super();
		this.name = name;
		this.hobbies = hobbies;
		this.height = height;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ArrayList<String> getHobbies() {
		return hobbies;
	}

	public void setHobbies(ArrayList<String> hobbies) {
		this.hobbies = hobbies;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}
	
	
}
package org.joonzis.DI_11_annoConfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class PersonMain {
	public static void main(String[] args) {
		//1. AnnoConfig.java 를 이용하여 Bean 객체 출력
		AnnotationConfigApplicationContext ctx = 
				new AnnotationConfigApplicationContext(AnnoConfig.class);
		
		Person person1 = ctx.getBean("person1", Person.class);
		System.out.println("이름 : " + person1.getName());
		System.out.println("취미 : " + person1.getHobbies());
		System.out.println("키 : " + person1.getHeight());
	
		System.out.println("==================================");
		
		
		//2. applicationContext.xml 파일을 이용해서 Bean 객체 출력
		AnnotationConfigApplicationContext ctx1 = 
				new AnnotationConfigApplicationContext("applicationContext.xml");
		
		
		Person person2 = ctx.getBean("person2", Person.class);
		System.out.println("이름 : " + person2.getName());
		System.out.println("취미 : " + person2.getHobbies());
		System.out.println("키 : " + person2.getHeight());
		
		
	}
	
	
		
	
}
<?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="person2" class="org.joonzis.DI_11_annoConfig.Person">
		<property name="name" value="박씨"></property>
		<property name="hobbies">
			<list>
				<value>드라이브</value>
				<value>술먹기</value>
				<value>볼링치기</value>
			</list>
		</property>
		<property name="height" value="170.0"></property>
	</bean>
	
	
	<bean id="cBean2" class="org.joonzis.DI_11_annoConfig">
		<property name="ArrayList">
			<list>
				<value>양씨</value>
				<value>눕기</value>
				<value>150.0</value>
			</list>
		</property>
	</bean>
</beans>