We know that the automatic configuration function of spring boot can determine which spring configuration should be used and which should not be used according to different situations. For example:
How did this be achieved? The reason is that it utilizes Spring's conditional configuration, which allows configurations to exist in the application, but these configurations are ignored before certain specific conditions are met.
To implement conditional configuration, we need to use @Conditional conditional annotation. Next, let’s write a small example to experience how @Conditional works.
1. @Conditional small example
We know that the command to display the list in Windows is dir, while the command to display the list in Linux system is ls. Based on the conditional configuration, we can achieve different values under different operating systems.
1. Definition of judgment conditions
1.) Decision conditions under windows
/** * Implement spring's Condition interface, and override the matches() method, and return true if the operating system is Windows * */public class WindowsCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Windows"); }}2.) Decision conditions under linux
/** * Implement spring's Condition interface, and override the matches() method, and return true if the operating system is linux * */public class LinuxCondition implements Condition{ @Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { return context.getEnvironment().getProperty("os.name").contains("Linux"); }}2. Bean classes under different systems
1.) Interface
public interface ListService { public String showListLine();}2.) Bean class under windows
public class WindowsListService implements ListService{ @Override public String showListLine() { return "dir"; }}3.) Bean class under linux
public class LinuxListService implements ListService{ @Override public String showListLine() { return "ls"; }}3. Configuration class
@Configurationpublic class ConditionConfig { /** * Pass the @Conditional annotation, and return the WindowsListService instance if it meets the Windows conditions* */ @Bean @Conditional(WindowsCondition.class) public ListService windonwsListService() { return new WindowsListService(); } /** * Pass the @Conditional annotation, and return the LinuxListService instance if it meets the Linux conditions* */ @Bean @Conditional(LinuxCondition.class) public ListService linuxListService() { return new LinuxListService(); }}4. Test class
public class ConditionTest { public static void main(String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class); ListService listService = context.getBean(ListService.class); System.out .println(context.getEnvironment().getProperty("os.name") + " The list command under the system is: " + listService.showListLine()); }}5. Run the test class, since I am on Windows 7 system, the result is
The list commands under Windows 7 system are: dir
If yours is on Linux, the result will be
The list commands under Linux system are: ls
2. Conditional configuration of spring boot
In the spring boot project, there will be a jar package called spring-boot-autoconfigure
Conditional configuration is implemented in this jar. It uses the following conditional annotations, which start with @Conditional:
Next, let's look at a source code column:
Take JdbcTemplateAutoConfiguration as an example, it contains this code:
@Bean @Primary @ConditionalOnMissingBean(JdbcOperations.class) public JdbcTemplate jdbcTemplate() { return new JdbcTemplate(this.dataSource); }A JdbcTemplate bean will be initialized only when there is no JdbcOperations (if you look at the source code of JdbcTemplate, you will find that the JdbcTemplate class implements the JdbcOperations interface).
Based on the above content, we can read the source code related to automatic configuration.
Summarize
The above is the automatic configuration of @Conditional and spring boot in Spring boot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!