we know that the spring boot auto configuration function can determine which spring configuration should be used in different circumstances, which should not be used, for example, is JdbcTemplate in

  • Spring in Classpath? If yes, and DataSource also exists, then automatically configure a JdbcTemplate Bean
  • Thymeleaf, is it in Classpath? If so, automatically configure Thymeleaf's template parser, view parser, template engine

, how did this happen? The reason is that it takes advantage of the conditional configuration of the Spring, the conditional configuration allows the configuration to exist in the application, but the configuration will be ignored before meeting certain specific conditions. To achieve conditional configuration of

, we need to use @Conditional conditional annotation. Next, write a small example to see how @Conditional works.

1, @Conditional small example

we know that the command to display lists under windows is dir, and the command to display lists under the Linux system is, based on conditional configuration, we can return different values under different operating systems.

1. judgment condition definition

1.)

 / * * to implement the Condition interface of spring, and rewrite the matches () method, if the operating system is windows. Hes (ConditionContext context, AnnotatedTypeMetadata metadata) {return context.getEnvironment ().GetProperty ("os.name").Contains ("Windows"); Return to true * * / public class LinuxCondition implements Condition{@Override public Boolean matches (ConditionContext context) Class 

1.)

 public interface ListService {public String showListLine ();}

2.).

 public class LinuxListService implements ListService{@Override public String () {{} {} {/ * * *} by annotation. The windows condition returns the WindowsListService instance * * / @Bean @Conditional (WindowsCondition.class) public ListService windonwsListService () {return new WindowsListService ();} / * * * * / * * by @Conditional annotations. @Conditional (LinuxCondition.class) public ListService linuxListService () {return new LinuxListService ();}}

4. test class

 NnotationConfigApplicationContext (ConditionConfig.class); ListService listService = context.getBean (ListService.class); System.out.Println (context.getEnvironment ().GetProperty ("os.name") + "the list command under the system:" + listService.showListLine () ");} Run the test class, because I am the windows7 system, so the result is the list command under the 

Windows 7 system: dir

if you are a Linux system, then the result will be the list command under the

Linux system: ls

will exist in the spring boot project, a jar package named spring-boot-autoconfigure,

> > > </p>
<p>. There is this code in it: </p>
<pre> @Bean @Primary @ConditionalOnMissingBean (JdbcOperations.class) public JdbcTemplate jdbcTemplate () {return new JdbcTemplate. When a class implements the JdbcOperations interface) instance, it initializes a JdbcTemplate Bean. </p>
<p> based on the above content, we can read the auto configuration related source code. </p>
<p><span style= summed up the

above the Spring boot of the Spring boot in the automatic configuration of @Conditional and spring, I hope to help you, if you have any questions please give me a message, the small editor will respond to you in time. Thank you for your support for the script home website.