IoC 컨테이너와 빈(2) - 컴포넌트 스캔
IoC 컨테이너와 빈(2) - 컴포넌트 스캔
@Component
@controller
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Controller { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */ String value() default ""; }
@Service
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Service { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */ String value() default ""; }
@Repository
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Component public @interface Repository { /** * The value may indicate a suggestion for a logical component name, * to be turned into a Spring bean in case of an autodetected component. * @return the suggested component name, if any */ String value() default ""; }
@Bean
@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Bean { /** * The name of this bean, or if plural, aliases for this bean. If left unspecified * the name of the bean is the name of the annotated method. If specified, the method * name is ignored. */ String[] name() default {}; /** * Are dependencies to be injected via autowiring? */ Autowire autowire() default Autowire.NO; /** * The optional name of a method to call on the bean instance during initialization. * Not commonly used, given that the method may be called programmatically directly * within the body of a Bean-annotated method. Default value is {@code ""}, indicating * that no init method should be called. */ String initMethod() default ""; /** * The optional name of a method to call on the bean instance upon closing the * application context, for example a {@code close()} method on a JDBC {@code * DataSource} implementation, or a Hibernate {@code SessionFactory} object. * The method must have no arguments but may throw any exception. * As a convenience to the user, the container will attempt to infer a destroy * method against object returned from the {@code @Bean} method. For example, given a * {@code @Bean} method returning an Apache Commons DBCP {@code BasicDataSource}, the * container will notice the {@code close()} method available on that object and * automatically register it as the {@code destroyMethod}. This 'destroy method * inference' is currently limited to detecting only public, no-arg methods named * 'close'. The method may be declared at any level of the inheritance hierarchy, and * will be detected regardless of the return type of the {@code @Bean} method, i.e. * detection occurs reflectively against the bean instance itself at creation time. * To disable destroy method inference for a particular {@code @Bean}, specify an * empty string as the value, e.g. {@code @Bean(destroyMethod="")}. * Note: Only invoked on beans whose lifecycle is under the full control of the * factory, which is always the case for singletons but not guaranteed * for any other scope. * @see org.springframework.context.ConfigurableApplicationContext#close() */ String destroyMethod() default AbstractBeanDefinition.INFER_METHOD; }
@Component와 @Bean의 차이
@Bean 어노테이션의 경우 개발자가 직접 제어가 불가능한 외부라이브러리 등을 Bean으로 만들때 사용 한다.
@Component는 개발자가 직접 작성한 class를 Bean으로 등록 하기위해서 사용한다.
@Service, @Controller, @Repository은 실제 동작하는건 같지만 기능별로 분류해 놓았다.
@Component Scan이란
Spring에서는 XML에 위의 코드를 작성함으로써 @Component가 해당 패키지를 기본으로하여 어노테이션을 읽어 Bean으로 등록해주는 역할을 한다.
실제 스캐닝은 ConfigurationclassPostProcessor라는 BeanFactoryPostProcessor에 의해 처리가 된다.
excludeFilters, includeFilters 등을 사용하게 되면 Bean으로 등록하지 못하게 한다.
평션을 사용한 빈 등록
public static void main(String[] args) { new SpringApplicationBuilder() .sources(Demospring51Application.class) .initializers((ApplicationContextInitializer) applicationContext -> { applicationContext.registerBean(MyBean.class); }) .run(args); }
from http://rlawls1991.tistory.com/29 by ccl(A) rewrite - 2020-03-16 13:20:30
댓글
댓글 쓰기