<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
package kr.or.connect.diexam01;
//빈클래스
public class UserBean {
//필드는 private한다.
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;
}
// setter, getter메소드는 프로퍼티라고 한다.
public void setName(String name) {
this.name = name;
}
public String getName() {
return 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;
}
}
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version> 4.3.14.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<?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"></bean>
</beans>
kr.or.connect.diexam01.UserBean userBean = new kr.or.connect.diexam01.UserBean();
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) {
// bean의 정보를 모두 읽어서 객체를 전부 생성한 후 메모리에 올려놓는다.
// 문제가 발생하면 해당 어플리케이션 종료
ApplicationContext ac = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
System.out.println("초기화 완료.");
// id가 userBean과 일치하는 애를 찾은 후 object 형태로 리턴한다.
UserBean userBean = (UserBean)ac.getBean("userBean");
userBean.setName("kim");
System.out.println(userBean.getName());
UserBean userBean2 = (UserBean)ac.getBean("userBean");
// 싱글톤이기 때문에 참이다.
if(userBean == userBean2) {
System.out.println("같은 인스턴스이다.");
}
}
}