spring 条件化的bean

1、假设你希望一个bean只有在应用的类路径下包含特定的库时才创建。或者希望某个bean只有在另外的特定的bean也声明了之后才创建。还可以设置某个特定的环境变量设置之后,才会创建某个bean。

2、什么地方会用到呢?

当创建bean是附件条件的时候。

3、举个例子

package org.springframework.cloud.kubernetes.config.reload;

import io.fabric8.kubernetes.client.KubernetesClient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.info.InfoEndpointAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.cloud.context.restart.RestartEndpoint;
import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator;
import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * Definition of beans needed for the automatic reload of configuration.
 *
 * @author Nicolla Ferraro
 */
@Configuration
@ConditionalOnProperty(value = "spring.cloud.kubernetes.enabled", matchIfMissing = true)
@ConditionalOnClass(EndpointAutoConfiguration.class)
@AutoConfigureAfter({ InfoEndpointAutoConfiguration.class,
		RefreshEndpointAutoConfiguration.class, RefreshAutoConfiguration.class })  //加载ConfigReloadAutoConfiguration之前先加载{}里边的类
@EnableConfigurationProperties(ConfigReloadProperties.class)    //使使用 @ConfigurationProperties 注解的类生效。
public class ConfigReloadAutoConfiguration {

	/**
	 * Configuration reload must be enabled explicitly.
	 */
	@ConditionalOnProperty("spring.cloud.kubernetes.reload.enabled")
	@ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class })
	@EnableScheduling
	@EnableAsync
	protected static class ConfigReloadAutoConfigurationBeans {

		@Autowired
		private AbstractEnvironment environment;

		@Autowired
		private KubernetesClient kubernetesClient;

		@Autowired
		private ConfigMapPropertySourceLocator configMapPropertySourceLocator;

		@Autowired
		private SecretsPropertySourceLocator secretsPropertySourceLocator;

		/**
		 * @param properties config reload properties
		 * @param strategy configuration update strategy
		 * @return a bean that listen to configuration changes and fire a reload.
		 */
		@Bean
		@ConditionalOnMissingBean
		public ConfigurationChangeDetector propertyChangeWatcher(
				ConfigReloadProperties properties, ConfigurationUpdateStrategy strategy) {
			switch (properties.getMode()) {
			case POLLING:
				return new PollingConfigurationChangeDetector(this.environment,
						properties, this.kubernetesClient, strategy,
						this.configMapPropertySourceLocator,
						this.secretsPropertySourceLocator);
			case EVENT:
				return new EventBasedConfigurationChangeDetector(this.environment,
						properties, this.kubernetesClient, strategy,
						this.configMapPropertySourceLocator,
						this.secretsPropertySourceLocator);
			}
			throw new IllegalStateException(
					"Unsupported configuration reload mode: " + properties.getMode());
		}

		/**
		 * @param properties config reload properties
		 * @param ctx application context
		 * @param restarter restart endpoint
		 * @param refresher context refresher
		 * @return provides the action to execute when the configuration changes.
		 */
		@Bean
		@ConditionalOnMissingBean
		public ConfigurationUpdateStrategy configurationUpdateStrategy(
				ConfigReloadProperties properties, ConfigurableApplicationContext ctx,
				RestartEndpoint restarter, ContextRefresher refresher) {
			switch (properties.getStrategy()) {
			case RESTART_CONTEXT:
				return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
						restarter::restart);
			case REFRESH:
				return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
						refresher::refresh);
			case SHUTDOWN:
				return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
						ctx::close);
			}
			throw new IllegalStateException("Unsupported configuration update strategy: "
					+ properties.getStrategy());
		}

	}

}

4、这里涉及到了好几个注解,我们逐个分析它们的作用。

  • @Configuration  //加入spring容器的bean是一个配置类
  • @ConditionalOnProperty(value = "spring.cloud.kubernetes.enabled", matchIfMissing = true) //配置文件中相应的属性被设置成true是才会创建这个bean
  • @ConditionalOnClass(EndpointAutoConfiguration.class)  //只不过判断某个类是否存在于 classpath 中
发布了85 篇原创文章 · 获赞 21 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41670928/article/details/103297391
今日推荐