6.28
春は、オペレーティングシステムの情報へのアクセスを提供します。
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Myconfig.class); ConfigurableEnvironment contextEnvironment = (ConfigurableEnvironment) applicationContext.getEnvironment(); /*对应jvm参数中的os.name*/ String ospropertyname = contextEnvironment.getProperty("os.name"); System.out.println(ospropertyname);
Beanコンテナかどうかを判断するための条件条件付きレジスタ
この方法は、Beanプラス@Conditionalを注釈される必要があります
条件タイプのクラスの配列を受け入れる@Conditiona、
条件は、インタフェース条件付き内部マッチを実装するクラスを作成する必要があります
メソッドは、パラメータと一致しているConditionContext conditionContext、AnnotatedTypeMetadata annotatedTypeMetadata
public class Os implements Condition { @Override public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { /*获取ioc使用的beanfactory*/ ConfigurableListableBeanFactory beanFactory = conditionContext.getBeanFactory(); /*获取bean定义的注册类*/ BeanDefinitionRegistry registry = conditionContext.getRegistry(); Environment environment = conditionContext.getEnvironment(); String ospropertyname = environment.getProperty("os.name"); /*判断容器中是否含有bean*/ //boolean person = registry.containsBeanDefinition("person"); if(ospropertyname.contains("window")){ return true; } return false; } }