Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
관리 메뉴

방카@Dev

[Spring]Bean 생성주기 본문

BackEnd/Spring

[Spring]Bean 생성주기

방카킴 2024. 6. 10. 12:29
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>

 

<결과>