Spring Framework component registration @Component

Spring Framework component registration @Component

EDITORIAL

In the spring popular today, for spring use and master the necessary skills but indispensable. But the whole system spring is particularly large, it is learned, had slowly accumulated bit by bit from the foundation. This paper describes the @Componentannotations in the spring in a simple to use, as well as annotations 派生性and层次性

@Component simple to use

@ComponentAnnotation is a meta annotation, i.e., can be marked on the other notes. In the spring, any @Componentcomponents are annotated identified candidate scan assembly, and is @Componentmarked annotations annotation element, when any component labeling it, also regarded as a candidate scan assembly. Simply put, in the spring, is a common javaBean @Componentthe comment tag, when using annotation configuration and class-based scanning path, as a candidate component is added to the spring container

package com.spring.study.ioc.register;

/**
 * spring扫描的候选组件
 *
 * @author TangFD
 * @since 2019/6/25.
 */
@Data
@Component
public class TestComponent {
    private String id = "@Component";
}

Add spring boot class, and class path when the spring starts to be scanned

/**
 * spring 容器启动引导类
 *
 * @author TangFD
 * @since 2019/6/25.
 */
@ComponentScan("com.spring.study.ioc.register")
public class TestComponentBootstrap {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(TestComponentBootstrap.class);
        System.out.println("context id : " + applicationContext.getId());
        TestComponent bean = applicationContext.getBean(TestComponent.class);
        System.out.println("TestComponent bean : " + bean);
        applicationContext.close();
    }
}

After the spring container starts, the console print results:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@Component)

Thus, by simply adding in the spring on a normal javaBean @Componentnotes, together with the scan path type, it is possible to add the spring javaBean container. spring can manage the life cycle of the Bean.

Mentioned earlier @Componentis a meta annotation tag when it is in the other annotations on the spring assembly will also have to be scanned, and the ability to identify the components. In the spring, it is @Componentmarked with a lot of notes, for example: @Controller, , @Service, @Repositorywhen an ordinary javaBean these annotations are labeled, the same will start when the spring containers Bean considered candidate components, added to the vessel.

The above TestComponentcomments into classes @Service, the result is the same

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(annotation = Component.class)
    String value() default "";
}

/**
 * spring扫描的候选组件
 *
 * @author TangFD
 * @since 2019/6/25.
 */
@Data
@Service
public class TestComponent {
    private String id = "@Service";
}

After the spring container starts, the console print results:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@Service)

派生性with层次性

These two concepts from Mu-class network of small and medium-Marco course descriptions, strictly speaking, no annotations are derived and hierarchy, the reason why say this because a lot of comment in the spring there are levels derived and of the structure. With these two features, we can also customize your own notes, the use of @Componentmeta-annotation to be added to the normal spring container scanning javaBean

Derivation
custom annotation a @FirstAnnotation, is identified @Component element annotation, and keep the same signature, when a component uses @FirstAnnotation annotation label, the container will be scanned and spring loaded

/**
 * 自定义注解@FirstAnnotation,被@Component标注
 * @author TangFD
 * @since 2019/6/10.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface FirstAnnotation {
    String value() default "";
}

The above TestComponentcomments into classes @FirstAnnotation, the result is the same

/**
 * spring扫描的候选组件
 *
 * @author TangFD
 * @since 2019/6/25.
 */
@Data
@FirstAnnotation
public class TestComponent {
    private String id = "@FirstAnnotation";
}

After the spring container starts, the console print results:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@FirstAnnotation)

Hierarchy

Spring annotation mode does not have the real derivation and hierarchy, just like java classes, inheritance, and have functions similar hierarchy

Custom annotation a @SecondAnnotation, is @FirstAnnotationannotated identified components when used with a @SecondAnnotationtime label annotation, the container will also be scanned and spring loaded

/**
 * 自定义注解@SecondAnnotation ,被@FirstAnnotation标注
 *
 * @author TangFD
 * @since 2019/6/11.
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@FirstAnnotation
public @interface SecondAnnotation {
    String value() default "";
}

The above TestComponentcomments into classes @SecondAnnotation, the result is the same

/**
 * spring扫描的候选组件
 *
 * @author TangFD
 * @since 2019/6/25.
 */
@Data
@SecondAnnotation
public class TestComponent {
    private String id = "@SecondAnnotation";
}

After the spring container starts, the console print results:

context id : org.springframework.context.annotation.AnnotationConfigApplicationContext@21b8d17c
TestComponent bean : TestComponent(id=@SecondAnnotation)

summary

This article describes the spring in @Componenta simple to use yuan annotations, and notes by way of illustration the inheritance hierarchy and sexual function. Article content is very simple, as long as the understanding and the entry of the spring, there will not be any problems.

The reason why these simple content out to write, one to his writing blog, or a record of the open road study notes a simple head, and second, to think things will sort themselves further understanding, consolidate their Know how.

Learning is never a simple matter, there can be confusion, can be lazy, but the pace of progress will never be stopped.

Short step, a thousand miles; not small streams, into jianghai;

Guess you like

Origin www.cnblogs.com/tangfd/p/11094907.html