상세 컨텐츠

본문 제목

section9) Spring Configuration with Java Annotations-Bean Scopes and Lifecycle Methods

Spring/Spring&Hibernate 강의

by 빙하둘리 2022. 12. 19. 23:14

본문

728x90

section9) Spring Configuration with Java Annotations-Bean Scopes and Lifecycle Methods

 

예시 코드1

package com.luv2code.springdemo;

public class AnnotationBeanScopeDemoApp {

	public static void main(String[] args) {
    
    // load spring config file
    ClassPathXmlApplicationContext context = new
    	ClassPathXmlApplicationContext("applicationContext.xml");
    
    // retrieve bean from spring container
    Coach theCocah = context.getBean("tennisCoach", Coach.class);
    
    Coach alphaCoach = context.getBean("tennisCoach", Coach.class);
    
    // check if they are the same
    boolean result = (theCoach == alphaCoach);
    
    // print out the results
    System.out.println("\nPointing to the same object: " + result);
    
    System.out.println("\nMemory location for theCoach: " + theCoach);
    
    System.out.println("\nMemory location for alphaCoach: " + alphaCoach + "\n");
    
    // close the context
    context.close();
    }
}

Pointing to the same object: true

참조값은 TennisCoach@78a2da20으로 동일

default scope is singleton

 

그러면 @Scope("prototype")을 써서 scope를 prototype으로 만들어보자. prototype scope는 주소가 다르게 찍힐 것이다.

package com.luv2code.springdemo;

import org.springframework.beans.factory.annotation.Autowired; ...(import한 클래스들 생략)

@Component
@Scope("prototype")
public class TennisCoach implements Coach {

	@Autoiwred
    @Qualifier("randomFortuneService")
    private FortuneService fortuneService;
    
    // define a default constructor
    public TennisCoach() {
    	System.out.println(">> TennisCoach: inside default constructor");
    }
    (중략)

즉, prototype의 경우 새로운 인스턴스를 매번 생성한다.

 

@Component
public class TennisCoach implements Coach {

	@PostConstruct
    public void doMyStartStuff() {...}

Code will execute after constructor and after injection of dependencies

 

@Component
public class TennisCoach implements Coach {

	@PreDestroy
    public void doMyStartStuff() {...}

Code will execute before bean is destroyed

 

return type 등에 관한 내용

Special Note about @PostConstruct and @PreDestroy Method Signatures

Special Note about @PostConstruct and @PreDestroy Method Signatures

I want to provide additional details regarding the method signatures of @PostContruct and @PreDestroy methods.

Access modifier

The method can have any access modifier (public, protected, private)

Return type
The method can have any return type. However, "void' is most commonly used. If you give a return type just note that you will not be able to capture the return value. As a result, "void" is commonly used.

Method name
The method can have any method name.

Arguments
The method can not accept any arguments. The method should be no-arg.

 

@PostConstruct를 해주면 코드는 constructor와 injection of dependencies 이후로 시행

@PreDestory를 해주면 코드는 bean이 destroy 되기 전에 시행

 

결과는 DI->@PostConstruct->Bean이 생성->PreDestroy 실행 후 destroy

728x90

관련글 더보기

댓글 영역