방카@Dev
[Spring]Bean 생성주기 본문
package com.kbfg.digi;
import org.springframework.context.support.GenericXmlApplicationContext;
public class MainClass {
public static void main(String[] args) {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext(); //생성
ctx.load("classpath:applicationCTX.xml"); //설정
ctx.refresh(); //안하면 load에서 해줌!!
Student student = ctx.getBean("student",Student.class); //사용
System.out.println("이름 : "+ student.getName());
System.out.println("나이 : "+ student.getAge());
ctx.close(); //종료
System.out.println("이름 : "+ student.getName());
System.out.println("나이 : "+ student.getAge());
System.out.println("이름 : "+ student.getName());
System.out.println("나이 : "+ student.getAge());
}
}
- 생성 : ctx.refresh(); ➡️ afterPropertiesSet
- 종료 : ctx.close(); ➡️ destroy
package com.kbfg.digi;
import javax.annotation.*;
public class OtherStudent {
private String name;
private int age;
@PostConstruct
public void initMethod() {
System.out.println("initMethod()");
}
@PreDestroy
public void destroyMethod(){
System.out.println("destroyMethod()");
}
/* public OtherStudent() {}; */
public OtherStudent(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
}
package com.kbfg.digi;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class Student implements InitializingBean, DisposableBean{
private String name;
private int age;
public Student() {};
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public void destroy() throws Exception {
// TODO Auto-generated method stub
System.out.println("destroy()");
}
@Override
public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub
System.out.println("afterPropertiesSet()");
}
}
<applicationCTX.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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
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">
<context:annotation-config/>
<bean id="student" class="com.kbfg.digi.Student">
<constructor-arg value="홍길순"></constructor-arg>
<constructor-arg value="30"></constructor-arg>
</bean>
<bean id="otherStudent" class="com.kbfg.digi.OtherStudent" scope="singleton">
<constructor-arg value="김건"></constructor-arg>
<constructor-arg value="33"></constructor-arg>
</bean>
</beans>
<결과>
'BackEnd > Spring' 카테고리의 다른 글
[Spring]AOP(Aspect Oriented Programming)_스프링 핵심원리(고급편) (0) | 2024.06.11 |
---|---|
[Spring]Spring Environment (0) | 2024.06.10 |
[Spring]Ch9.데이터베이스:관리자 회원가입 기능 만들기_올인원 스프링 프레임워크 (1) | 2024.06.09 |
[Spring]web.xml에 한글깨짐현상 방지 filter 태그 추가하기 (0) | 2024.06.08 |
[Spring]Ch8.Service와 DAO 구현_올인원 스프링 프레임워크 (1) | 2024.06.08 |