- Directory Tree
- UserBean.java 생성
package kr.or.connect.diexam01;
public class UserBean {
private String name;
private int age;
private boolean male;
public UserBean() {}
public UserBean(String name, int age, boolean male) {
this.name = name;
this.age = age;
this.male = male;
}
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 boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
}
- pom.xml 의존성 추가
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.30.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
...
</dependencies>
...
- resources 폴더 밑에 applicationContext.xml 생성
- main폴더 내에 어디든 생성해도 상관없지만 관리의 용이를 위해 resource 폴더 마련
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id ="userBean" class="kr.or.connect.diexam01.UserBean"/>
</beans>
- ApplicationContextExam01.java 생성
package kr.or.connect.diexam01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ApplicationContextExam01 {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("초기화 완료!!");
UserBean userBean = (UserBean)ac.getBean("userBean");
userBean.setName("choi");
System.out.println(userBean.getName());
UserBean userBean2 = (UserBean)ac.getBean("userBean");
if(userBean == userBean2)
System.out.println("같은 인스턴스입니다.");
}
}
- 결과