Spring 注解驱动开发-注入Bean-@Conditional(未解决)

发布于 2021-12-12  51 次阅读


目前存在以下几个问题:

  • maven 导入junit 包 Scope属性设置为tet 就仍无法使用@Test注解
  • idea设置环境变量-Dos.name=linux没效果

使用Spring全注解,实现用@Conditional注解区分当前运行环境导入不同的Bean

package com.apesblog.test12_12;

import com.apesblog.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MainConfig {

    @Bean
    public Person person() {
        return new Person("张宇顺",18);
    }

    @Bean
    @Conditional(WindowsBean.class)
    public Person windows() {
        return new Person("windows",18);
    }

    @Bean
    @Conditional(LinuxBean.class)
    public Person linus() {
        return new Person("linus",18);
    }

}
package com.apesblog.test12_12;


import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

public class WindowsBean implements Condition {


    /**
     * Determine if the condition matches.
     *
     * @param context  the condition context
     * @param metadata metadata of the {@link AnnotationMetadata class}
     *                 or {@link MethodMetadata method} being checked.
     * @return {@code true} if the condition matches and the component can be registered
     * or {@code false} to veto registration.
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (context.getEnvironment().getProperty("os.name").contains("Windows")){
            return true;
        }
        return false;
    }
}
package com.apesblog.test12_12;


import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.core.type.MethodMetadata;

public class LinuxBean implements Condition {


    /**
     * Determine if the condition matches.
     *
     * @param context  the condition context
     * @param metadata metadata of the {@link AnnotationMetadata class}
     *                 or {@link MethodMetadata method} being checked.
     * @return {@code true} if the condition matches and the component can be registered
     * or {@code false} to veto registration.
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        if (context.getEnvironment().getProperty("os.name").contains("Linux")){
            return true;
        }
        return false;
    }
}
package com.apesblog.test12_12;


        import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        import org.springframework.core.env.ConfigurableEnvironment;

public class Test {

    @org.junit.Test
    public void test01(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String osname = environment.getProperty("os.name");
        System.out.println(osname);
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : beanDefinitionNames){
            System.out.println(name);
        }
    }
}