본문 바로가기

Spring Boot

20230526_스프링부트 학습

* 오토 컨피그레이션

 

메타 에노테이션을 상속의 개념과 헷갈리면 안됨.

@Component - @Service

                       -@Controller   

 

 

 

      -@Controller-------------   @RestController              ----합성 에노테이션

      - @ ResponseBody ----   

 

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration
@ComponentScan
public @interface MySpringBootAnnotation {
}
@MySpringBootAnnotation
public class HellobootApplication {

public static void main(String[] args) {
SpringApplication.run(HellobootApplication.class, args);
}

}
@Configuration
public class Config {

@Bean
public ServletWebServerFactory servletWebServerFactory() {
return new TomcatServletWebServerFactory();
}

@Bean
public DispatcherServlet dispatcherServlet() {
return new DispatcherServlet();
}
}

 

###########

빈 오브젝트와 역할과 구분

 

컨포넌트스캔 에노테이션에 의해 유저구성정보 빈을 읽어들임. ex 컨트롤러 서비스 등

 

사용성에 맞춰 나눠진 패키지구조

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import({DispatcherServletConfig.class, TomcatWebServerConfig.class})
public @interface EnableMyAutoConfiguration {
}

현재는 하드코딩 형식으로 설정파일 두개를 Import했지만 동적인 방식으로 바꿔보자.

public class MyAutoConfigImportSelector implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] {
"tobyspring.config.autoconfig.DispatcherServletConfig",
"tobyspring.config.autoconfig.TomcatWebServerConfig"
};
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Import(MyAutoConfigImportSelector.class)
public @interface EnableMyAutoConfiguration {
}

 

'Spring Boot' 카테고리의 다른 글

20230604_스프링부트학습  (0) 2023.06.04
20230528_스프링부트학습  (0) 2023.05.28
20230521_스프링부트 학습  (0) 2023.05.21
스프링부트_20230520  (0) 2023.05.20
인프런_스프링부트학습_20230514  (0) 2023.05.14