Skip to content

Spring Boot Configuration #

Find similar titles

5회 업데이트 됨.

Edit
  • 최초 작성자
    Inco_KJS
  • 최근 업데이트
    jmkang

Structured data

Category
Programming

Spring Boot #

Spring Boot Configuration #

Spring Boot Configuration이란 Spring Boot를 사용하면서 필요한 설정에 관한 내용이다.

@SpringBootApplication #

@SpringBootApplication Annotation은 Spring Boot에서는 없어서는 안 될 Annotation 설정이다. 해당 annotation은 처음 Spring boot가 나올 때는 존재하지 않던 Annotation이다. @SpringBootApplication은 @EnableAutoConfiguration과 @ComponentScan 두 가지를 META Annotation으로 가지고 있다. @SpringBootApplication Annotation은 4가지 속성이 있는데 @EnableAutoConfiguration 어노테이션의 속성이 2개이고 나머지 2개는 @ComponentScan어노테이션의 속성이다.

@EnableAutoConfiguration #

@EnableAutoConfiguration Annotation에는 exclude와 excludeName이라는 2가지 속성이 있다. 이 속성들은 Spring Boot의 핵심적인 annotation이며 자동 설정을 담당한다. 만약 자동 설정을 하고 싶지 않은 클래스가 있다면 이 속성들을 사용하여 자동설정에서 제외할 수 있다.

@ConfigurationProperties #

@ConfigurationProperties는 환경설정 값을 만들고 사용하는 데 필요하며 DataSource를 구상한다. 또한, 환경설정 파일에서 값을 추출해 주입한다. 이로써 환경설정 값도 추상화가 가능하다. SpringBoot 프로젝트를 처음 생성한다면 Application.Properties란 파일에 application 환경설정에 필요한 값을 입력할 수 있다.

그 외 #

  • @Required : Bean 생성 시 필수 properties임을 Setter Method에 적용해 알린다.

  • @Qualifier("ID") : @Autowired와 같이 쓰이며, 같은 타입의 Bean 객체가 있을 때 해당 아이디를 적어 원하는 Bean이 주입될 수 있도록 하는 Annotation

  • @Resource : Bean 객체를 주입해준다 @Autowired와 차이점은 Autowired는 타입으로, Resource는 이름으로 연결해준다.

  • @PostConstruct, @PreConstruct : 의존하는 객체를 생성한 이후 초기화 작업을 위해 객체 생성 전/후에(pre/post) 실행해야 할 메소드 앞에 붙인다.

  • @PreDestroy : 객체를 제거하기 전(pre)에 해야 할 작업을 수행하기 위해 사용한다.

  • @PropertySource : 해당 프로퍼티 파일을 Environment로 로딩하게 해준다.

  • @Lazy : 지연 로딩을 지원한다. 일반적일 때처럼 Spring에서 클래스가 로드될 때 바로 Bean 등록을 마치는 것이 아니라 실제로 사용될 때 로딩이 이뤄지게 하는 방법으로 @Component나 @Bean Annotation과 같이 쓰인다.

  • @Value : 적용하고 싶은 value를 properties에서 가져와 사용한다.

  • @SpringBootApplication : @EnableAutoConfiguration, @Configuration, @ComponentScan 3가지를 하나의 annotation으로 합친 것이다.

  • @CookieValue : Cookie Value를 Parameter로 전달받을 수 있는 방법이다.

0.0.1_20230725_7_v68