Spring 条件装配

条件注解举例
@Profile 配置化条件装配
@Conditional 编程条件装配

先自定义 属性类

@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnSystemPropertyCondition.class)// 用于条件判断的注解 需要实现Condition
public @interface ConditionalOnSystemProperty {// 标注了这个类会被OnSystemPropertyCondition
    /**
     * Java 系统属性名称
     * @return
     */
    String name();
    /**
     * Java 系统属性值
     * @return
     */
    String value();
}

自定义Condition

// 根据javaben中的属性进行 自定义的选择

public class OnSystemPropertyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // 实现了条件判断
        Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
        String propertyName = String.valueOf(attributes.get("name"));
        String propertyValue = String.valueOf(attributes.get("value"));
        String javaPropertyValue = System.getProperty(propertyName);
        System.out.println(javaPropertyValue);

        return propertyValue.equals(javaPropertyValue);
    }
}
  1. @Profile 配置化条件装配

既然我们需要自定义 条件装配,我们就定义出一个接口

// 接口用于规定 泛型 的

public interface CalculateService {
    void println();
}

子类

@Profile("Java7")
@Service
public class Java7CalculateService implements CalculateService {

    @Override
    public void println() {
        System.out.println("Java 7 ");
    }
}

@Profile("Java8")
@Service
public class Java8CalculateService implements CalculateService {
    @Override
    public void println() {
        System.out.println("Java 8  实现");
    }
}

以上就是Profile 的配置

    @ComponentScan(basePackages = {"所在的包"})
public class ConditionalOnSystemPropertyBootstrap {
		public static void main(String[] args) {
        ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)
                .web(WebApplicationType.NONE)
                .profiles("Java8")
                .run(args);
        // 通过名称和类型获取 helloWorld Bean
        CalculateService helloWorld = context.getBean(CalculateService.class);
        helloWorld.println();
        // 关闭上下文
        context.close();
    }
}
  1. Conditional配置
    public class ConditionalOnSystemPropertyBootstrap {
    @Bean
    @ConditionalOnSystemProperty(name = “java”, value = “java”)
    public String helloWorld() {
    return “Hello,World yaoyonglong”;
    }
    public static void main(String[] args) {
    ConfigurableApplicationContext context = new
    SpringApplicationBuilder(ConditionalOnSystemPropertyBootstrap.class)
    .web(WebApplicationType.NONE)
    .run(args);
    // 通过名称和类型获取 helloWorld Bean
    CalculateService helloWorld = context.getBean(“helloWorld”,String.class);
    System.out.println(helloWorld );
    // 关闭上下文
    context.close();
    }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值