AnnoConfig.java
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("여행1");
hobbies.add("여행2");
Person person = new Person("전", hobbies, 100.22);
return person;
}
}
Person.java
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;
}
}
PersonMain.java
package org.joonzis.DI_11_annoConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
public class PersonMain {
public static void main(String[] args) {
// 1. AnnoCinfig.java 를 이용하여 Bean 객체 출력
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(AnnoConfig.class);
Person person = ctx.getBean("person1", Person.class);
System.out.println("이름 : " + person.getName());
System.out.println("키 : " + person.getHeight());
System.out.println("취미 : " + person.getHobbies());
System.out.println("============================");
// 2. applicationContext.xml 파일을 이용하여 Bean 객체 출력
AbstractApplicationContext ctx2 =
new GenericXmlApplicationContext("applicationContext11.xml");
// List 객체 가져오기
Person person2 = (Person)ctx2.getBean("person2");
//Person person2 = ctx.getBean("perseon2", Person.class);
System.out.println("이름 : " + person2.getName());
System.out.println("키 : " + person2.getHeight());
System.out.println("취미 : " + person2.getHobbies());
}
}
applicationContext11.xml
<?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 name="hobbies">
<list>
<value>취미1</value>
<value>취미2</value>
</list>
</property>
<property name="height" value="78.9"/>
</bean>
</beans>
반응형
'Spring & Spring Boot' 카테고리의 다른 글
[STS] Eclipse STS lombok.jar 추가 (0) | 2023.07.16 |
---|---|
DI_12_component (0) | 2023.07.06 |
DI_10_annoConfig (1) | 2023.07.06 |
DI_9_collection (0) | 2023.07.06 |
DI_8_set (0) | 2023.07.04 |