[Spring]Bean 생성주기

2024. 6. 10. 12:29·BackEnd/Spring
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
'BackEnd/Spring' 카테고리의 다른 글
  • [Spring]AOP(Aspect Oriented Programming)_스프링 핵심원리(고급편)
  • [Spring]Spring Environment
  • [Spring]Ch9.데이터베이스:관리자 회원가입 기능 만들기_올인원 스프링 프레임워크
  • [Spring]web.xml에 한글깨짐현상 방지 filter 태그 추가하기
방카킴
방카킴
김행원의 개발 블로그
  • 방카킴
    방카@Dev
    방카킴
  • 전체
    오늘
    어제
    • 분류 전체보기 (46)
      • Notice (2)
      • FrontEnd (5)
        • Javascript (1)
        • CSS (3)
      • BackEnd (25)
        • Java (1)
        • JSP (4)
        • Spring (19)
        • DB (1)
      • Git (1)
      • AI (8)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    Agent
    agenticai
    AI
    LLM
    북리뷰
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
방카킴
[Spring]Bean 생성주기
상단으로

티스토리툴바