“This is the 22nd day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Container configuration notes

1, the @autowired

The @AutoWired annotation is used to mark dependencies that Spring will resolve and inject. This annotation can be applied to constructors, fields, and setter methods.

2, @ Primary

When multiple beans of the same type need to be configured in the system, @primary can define the priority of these beans.

@postconstruct vs. @predestroy

It’s worth noting that these two annotations are not part of Spring; they are derived from two annotations in JSR-250, located in common-Annotations. Jar. The @postconstruct annotation is used to annotate methods that need to be executed before the Bean is initialized by Spring. The @predestroy annotation is used to annotate the methods that need to be executed before the Bean is destroyed.

4, @ the Qualifier

When there are multiple beans of the same type in the system, @AutoWired does not know which implementation class to choose for dependency injection. At this point, we can use the @Qualifier annotation to fine-tune to help @AutoWired select the right dependency. Here is an example of code for this annotation:

2. Spring Boot annotations

1. @ SpringBootApplication

The @SpringBootApplication annotation is a quick configuration annotation that defines one or more beans in the class it annotates and automatically triggers automatic configuration of beans and automatic scanning of components. This annotation is equivalent to @Configuration, @EnableAutoConfiguration, and @ComponentScan. This annotation is used in the main class of the Spring Boot application. Example code is as follows:

@SpringBootApplication public class Application{ public static void main(String [] args){ SpringApplication.run(Application.class,args); }}Copy the code

2, @ EnableAutoConfiguration

The @enableAutoConfiguration annotation is used to inform Spring to automatically configure configuration items associated with dependencies imported under the current classpath.

3, @ ConditionalOnClass and @ ConditionalOnMissingClass

These two annotations are class-conditional annotations that determine whether certain configurations should be performed based on the presence or absence of a class. Here is a simple example code:

@Configuration

@ConditionalOnClass(DataSource.class)

class MySQLAutoConfiguration {

 //...

}
Copy the code

ConditionalOnBean and ConditionalOnMissingBean

These two annotations are object conditional annotations that determine whether certain configuration methods should be performed based on the presence or absence of an object. Example code is as follows:

@Bean

@ConditionalOnBean(name="dataSource")

LocalContainerEntityManagerFactoryBean entityManagerFactory(){

 //...

}

@Bean

@ConditionalOnMissingBean

public MyBean myBean(){

 //...

}
Copy the code

5, @ ConditionalOnProperty

The @conditionalonProperty annotation decides whether to execute the labeled method based on whether the configuration items in the Spring configuration file meet the configuration requirements. Example code is as follows:

@Bean

@ConditionalOnProperty(name="alipay",havingValue="on")

Alipay alipay(){

 return new Alipay();

}
Copy the code

6, @ ConditionalOnResource

The following is an example of code that uses this annotation to detect methods that trigger when a configuration file is enabled:

@ConditionalOnResource(resources = "classpath:website.properties")

Properties addWebsiteProperties(){

 //...

}
Copy the code

7, @ ConditionalOnWebApplication and @ ConditionalOnNotWebApplication

These two annotations are used to determine whether the current application is a Web application. If the current application is a Web application, use the Spring WebApplicationContext and define the life cycle of its session. Here is a simple example:

@ConditionalOnWebApplication

HealthCheckController healthCheckController(){

 //...

}
Copy the code

8, @ ConditionalExpression

This annotation allows us to control more fine-grained constraints on express-based configuration conditions. When an expression satisfies a condition or when the expression is true, the method marked by this annotation is executed.

@Bean

@ConditionalException("${localstore} && ${local == 'true'}")

LocalFileStore store(){

 //...

}
Copy the code

9, @ Conditional

The @Conditional annotation can control more complex configuration conditions. If Spring’s built-in condition control annotations do not meet application requirements, you can use this annotation to define your own control conditions to meet your own requirements. Here is a simple example of using this annotation:

@Conditioanl(CustomConditioanl.class)

CustomProperties addCustomProperties(){

 //...

}
Copy the code

conclusion

This summary of Spring Boot common types of annotations in the use of the way, so that we can unified Spring Boot commonly used annotations have a comprehensive understanding. For reasons of space, some notes about Spring Boot that are not commonly used will be added and explained in the next share.