ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • @PostConstruct
    SPRING 공부/어노테이션 2022. 2. 5. 21:48

    인프런 강의에서 김영한 개발이사님의 강의를 듣고있었는데,

    스프링 MVC 1편을 나는 듣지 않았기 때문에 강의 자료를 다운받아 Import 하여 보는중 해당 어노테이션을 발견했다.

     

    The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This method must be invoked before the class is put into service. This annotation must be supported on all classes that support dependency injection. The method annotated with PostConstruct must be invoked even if the class does not request any resources to be injected. Only one method in a given class can be annotated with this annotation. The method on which the PostConstruct annotation is applied must fulfill all of the following criteria:

     

    API문서를 확인해보니 의존성 주입이 실행된 후 무언가 초기화를 위해 사용하는 메서드에 사용되는

    어노테이션이라 한다.

     

    이 어노테이션이 써진 메서드는 주입될 어떠한 자원이 없어도 호출된다고 한다. 

     

    강의 자료의 코드를 보아하니

        @PostConstruct
        public void init() {
            itemRepository.save(new Item("itemA", 10000, 10));
            itemRepository.save(new Item("itemB", 20000, 20));
        }

    itemRepository의 스태틱 변수(Map) 에 자원을 추가시켜준것을 확인할 수 있었다.

     

    'SPRING 공부 > 어노테이션' 카테고리의 다른 글

    @SpringBootApplication  (0) 2022.01.10
Designed by Tistory.