Spring's @Conditional conditional assembly bean

I. Introduction

Understanding how spring assembles beans according to conditions helps us better use springboot for development, and source code understanding;

Inheriting the spirit of open source, Spreading technology knowledge;

Two @Conditional assembly bean

The idea is as follows

  1. Spring provides @Conditionalannotations to implement conditional whether to assemble beans, so that bean can be dynamically assembled according to conditions;
  2. To determine @Conditionalwhether the annotation needs to be matched with the Condition bean, we only need to implement the Condition interface and rewrite the matches method;
  3. According to the boolean value returned by the matches method to determine @Conditionalwhether the bean needs to be assembled

The implementation process is as follows

2.1 User class

The user class is used to configure whether the class is equipped with the bean

/**
 * @Author lsc
 * <p> </p>
 */
public class User {

    private String userName;

	// 省略 set get
}
复制代码

2.2 Condition implementation class

RuntimeExist implements Condition, using conditionContext to get the jvm environment variable, if it exists, it returns true, otherwise it returns false; conditionContext also has the following methods

  • getRegistry (): use BeanDefinitionRegistry to check the bean definition;
  • getBeanFactory (): Use ConfigurableListableBeanFactory to check whether the bean exists;
  • getEnvironment (): Environment environment variable
  • getResourceLoader (): the resource loaded by ResourceLoader;
  • getClassLoader (): Class loaded by ClassLoader.
public class RuntimeExist  implements Condition {
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        // 获取jvm 运行时环境
        Environment environment = conditionContext.getEnvironment();
        // 检查 jvm 运行时环境是否存在属性
        return environment.containsProperty("java.runtime.name");
    }
}
复制代码

2.3 User configuration

According to whether the return value of the matches method of RuntimeExist is to assemble the bean; the property name zszxz is set here to facilitate better testing;

/**
 * @Author lsc
 * <p> </p>
 */
@Configuration
public class UserConfig {

    @Bean
    // 根据 RuntimeExist的 matches 方法的返回值 是否装配bean
    @Conditional(RuntimeExist.class)
    public User user(){
        User user = new User();
        user.setUserName("zszxz");
        return user;
    }
}
复制代码

2.4 Test

Because the java.runtime.name attribute exists in the jvm runtime environment, the assembly will be successful and zszxz will be printed out;

If you change java.runtime.name to any attribute that does not exist during jvm runtime, the test will report an exception;

@RunWith(SpringJUnit4ClassRunner.class)//创建spring应用上下文
@ContextConfiguration(classes= UserConfig.class)//加载配置类
public class ConditionTest {

    @Autowired
    User user;

    @Test
    public void test(){
        // zszxz
        System.out.println(user.getUserName());
    }
}
复制代码

Source address: public account summary article

Guess you like

Origin juejin.im/post/5e970169518825738b421d75
Recommended