annotation-learn

  1. Know common annotations

  2. Learn about custom annotations

  3. Distributed Task Scheduling by Hand (ElasticJob startup class)

  4. @SpringBootApplication Automatically loads the object process

A introduction

Look at the code and learn the notes!

Open source project gitee.com/flowerAndJa…

code

Getting – started a com module. Haopt. The annotation. Getting custom annotation definition package code – started module com. Haopt. The annotation. The meta meta annotation of custom annotations Springboot module: elasticJob-Springboot-starter module used in Springboot project development. Customizingelastic – Job boot action classes for Springboot integration. Elasticjob-test module, used to test distributed scheduling.

knowledge

Below document

Custom annotations

1. Overview annotations annotate and explain the program. Show it to the compiler. Make the code more concise.

2. The common annotation @override overrides the parent method @deprecated redundant method to indicate that the method has expired

3. User-defined annotation public @interface Annotation name {public attribute type Attribute name () default Default value; } Remarks Attribute type Basic data type, String, class, annotation, enumeration, one-dimensional array of the above types. Property has no default value and must be assigned manually.

4. Meta-notes A note that describes a note

@target specifies where annotations are used @Retention annotation lifetime @Inherited Whether custom annotations are Inherited by child classes

Common annotations in SpringBoot

1. Get the properties in the configuration file (see the code for details). Springboot module com. Haopt. The annotation. Obtain package) value, @ @ ConfigurationProperties (prefix = “Data “), @Configuration+ @bean to get properties in the Configuration file

2. Conditional annotation @Conditional(Class
) creates an object based on the Condition class’s matches method, which returns true

ConditionalOnProperty: ConditionalOnClass: ConditionalOnMissingBean: ConditionalOnClass: ConditionalOnMissingBean: ConditionalOnMissingBean: ConditionalOnClass: ConditionalOnMissingBean: ConditionalOnMissingBean: ConditionalOnClass: ConditionalOnMissingBean: ConditionalOnMissingBean Initialize the Bean to determine that there is no corresponding Bean in the environment

The @import annotation is used to load classes that Spring creates and places in the container.

Note: The @SpringBootApplication of the SpringBoot project startup class contains the @ComponentScan annotation. The @ComponentScan annotation will only scan the annotations under this column and sub-packages. So when importing another project dependency, you cannot add a bean object to the IOC container.

@import (class name.class) provides four Import methods Import beans Import configuration classes You can Import @beans from configuration classes into the Spring IOC container. Import ImportSelector Implementation class imports all of the return value full class name data into the IOC container. Import ImportBeanDefinitionRegistrar method implementation class in the class imported into the IOC container.

@springBootApplication -> @enableAutoConfiguration -> @Import({AutoConfigurationImportSelector.class}) -> selectImports(AnnotationMetadata annotationMetadata) -> getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata AnnotationMetadata) -> meta-INF/spring-.Factories (configuration file) -> load classes in the configuration file