@Bean @Profile("integration") public static PropertySourcesPlaceholderConfigurer propertiesForIntegration() throws Exception { String file = "application-integration-local.properties"; String envIntegration = System.getenv("CLOUDUNIT_JENKINS_CI"); if ("true".equalsIgnoreCase(envIntegration)) { file = "application-integration.properties"; } PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setLocations(getResources(file)); pspc.setIgnoreUnresolvablePlaceholders(true); pspc.setLocalOverride(true); return pspc; }
protected void mergePropertySource() throws Exception { if (this.environment != null) { this.addLast(new PropertySource<Environment>(PropertySourcesPlaceholderConfigurer.ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { public String getProperty(String key) { return this.source.getProperty(key); } }); } else { logger.warn("The injected environment was null!"); } if (this.locations != null && this.locations.length > 0) { Properties localProperties = new Properties(); loadProperties(localProperties); PropertySource<?> localPropertySource = new PropertiesPropertySource(PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, localProperties); if (this.localOverride) { this.addFirst(localPropertySource); } else { this.addLast(localPropertySource); } } }
@Override public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata .getAnnotationAttributes(EnableApolloConfig.class.getName())); String[] namespaces = attributes.getStringArray("value"); int order = attributes.getNumber("order"); PropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), PropertySourcesPlaceholderConfigurer.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(), PropertySourcesProcessor.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), ApolloAnnotationProcessor.class); }
@Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer placeholderConfigurer = new PropertySourcesPlaceholderConfigurer(); placeholderConfigurer.setFileEncoding("UTF-8"); Properties properties = EnvLoader.getEnvs(); if (properties.isEmpty()) { ParamsHome.getInstance().init(StringUtils.EMPTY); EnvLoader.init(); IniLoader.init(); properties = EnvLoader.getEnvs(); } placeholderConfigurer.setProperties(properties); return placeholderConfigurer; }
@Test public void postProcessEmbeddedServletContainerFactory() throws Exception { RootBeanDefinition bd = new RootBeanDefinition( MockEmbeddedServletContainerFactory.class); MutablePropertyValues pv = new MutablePropertyValues(); pv.add("port", "${port}"); bd.setPropertyValues(pv); this.context.registerBeanDefinition("embeddedServletContainerFactory", bd); PropertySourcesPlaceholderConfigurer propertySupport = new PropertySourcesPlaceholderConfigurer(); Properties properties = new Properties(); properties.put("port", 8080); propertySupport.setProperties(properties); this.context.registerBeanDefinition("propertySupport", beanDefinition(propertySupport)); this.context.refresh(); assertThat(getEmbeddedServletContainerFactory().getContainer().getPort()) .isEqualTo(8080); }
public void postProcess(ConfigurableListableBeanFactory beanFactory) { // 注册 Spring 属性配置 PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); MutablePropertySources mutablePropertySources = new MutablePropertySources(); mutablePropertySources.addLast(new PropertySource<String>(Configs.class.getName()) { @Override public String getProperty(String name) { return Configs.getString(name); } }); configurer.setPropertySources(mutablePropertySources); configurer.postProcessBeanFactory(beanFactory); /* * 注册 @ConfigValue 处理器. ConfigValueBeanPostProcessor 实现了 ApplicationListener 接口, 不能使用 * beanFactory.addBeanPostProcessor() 来注册实例. */ beanFactory.registerSingleton(ConfigValueBeanPostProcessor.class.getName(), new ConfigValueBeanPostProcessor(beanFactory)); }
public Optional<Resource[]> getPlaceholderConfigurerResources() { List<Resource> resources = Lists.newArrayList(); Iterator<Entry<String, PropertySourcesPlaceholderConfigurer>> iterator = placeholderMap.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, PropertySourcesPlaceholderConfigurer> entry = iterator.next(); Optional<Resource[]> optional = this.<Resource[]> getPropertyValue(entry.getValue(), PropertySourcesEnum.LOCATIONS.getName()); if (optional.isPresent()) { resources.addAll(Arrays.asList(optional.get())); } } return Optional.fromNullable(resources.toArray(new Resource[0])); }
private PropertySources deducePropertySources() { PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(); if (configurer != null) { // Flatten the sources into a single list so they can be iterated return new FlatPropertySources(configurer.getAppliedPropertySources()); } if (this.environment instanceof ConfigurableEnvironment) { MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment) .getPropertySources(); return new FlatPropertySources(propertySources); } // empty, so not very useful, but fulfils the contract return new MutablePropertySources(); }
private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer() { // Take care not to cause early instantiation of all FactoryBeans if (this.beanFactory instanceof ListableBeanFactory) { ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory; Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory .getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false, false); if (beans.size() == 1) { return beans.values().iterator().next(); } } return null; }
@Bean @Profile("vagrant") public static PropertySourcesPlaceholderConfigurer properties() throws Exception { String file = "application-vagrant.properties"; PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setLocations(getResources(file)); pspc.setIgnoreUnresolvablePlaceholders(true); pspc.setLocalOverride(true); return pspc; }
@Bean @Profile("production") public static PropertySourcesPlaceholderConfigurer propertiesForProduction() throws Exception { String file = "application-production.properties"; PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setLocations(getResources(file)); pspc.setIgnoreUnresolvablePlaceholders(true); pspc.setLocalOverride(true); return pspc; }
@Bean @Profile("vagrant-demo") public static PropertySourcesPlaceholderConfigurer propertiesForDemo() throws Exception { String file = "application-vagrant-demo.properties"; PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); pspc.setLocations(getResources(file)); pspc.setIgnoreUnresolvablePlaceholders(true); pspc.setLocalOverride(true); return pspc; }
@Bean @Profile("test") public static PropertySourcesPlaceholderConfigurer propertiesForTest() throws Exception { PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer(); Resource[] resources = new Resource[] {new ClassPathResource("application-test.properties")}; pspc.setLocations(resources); pspc.setIgnoreUnresolvablePlaceholders(true); pspc.setLocalOverride(true); return pspc; }
@Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySources = new PropertySourcesPlaceholderConfigurer(); //propertySources.setIgnoreUnresolvablePlaceholders(true); return propertySources; }
@Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); return propertySourcesPlaceholderConfigurer; }
private static PropertySources deducePropertySources(DefaultListableBeanFactory beanFactory) { PropertySourcesPlaceholderConfigurer configurer = getSinglePropertySourcesPlaceholderConfigurer(beanFactory); if (configurer != null) { return new FlatPropertySources(configurer.getAppliedPropertySources()); } Environment environment = new StandardEnvironment(); MutablePropertySources propertySources = ((ConfigurableEnvironment) environment).getPropertySources(); return new FlatPropertySources(propertySources); }
private static PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer(DefaultListableBeanFactory beanFactory) { Map<String, PropertySourcesPlaceholderConfigurer> beans = beanFactory.getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false, false); if (beans.size() == 1) { return beans.values().iterator().next(); } return null; }
@Override protected Class<?> getBeanClass(Element element) { // As of Spring 3.1, the default value of system-properties-mode has changed from // 'FALLBACK' to 'ENVIRONMENT'. This latter value indicates that resolution of // placeholders against system properties is a function of the Environment and // its current set of PropertySources if (element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIB).equals(SYSTEM_PROPERTIES_MODE_DEFAULT)) { return PropertySourcesPlaceholderConfigurer.class; } // the user has explicitly specified a value for system-properties-mode. Revert // to PropertyPlaceholderConfigurer to ensure backward compatibility. return PropertyPlaceholderConfigurer.class; }
@Bean public PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yamlPropertiesFactory = new YamlPropertiesFactoryBean(); ConfigurationFileResourceResolver configurationFileResourceResolver = new ConfigurationFileResourceResolver(); yamlPropertiesFactory.setResources(configurationFileResourceResolver.getResourceArray()); propertySourcesPlaceholderConfigurer.setProperties(yamlPropertiesFactory.getObject()); return propertySourcesPlaceholderConfigurer; }
@Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException { AppResourceLoader propertyLoader = new PropertyResourceLoader(); PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setIgnoreResourceNotFound(Boolean.FALSE); configurer.setLocation(propertyLoader.find()); return configurer; }
/** * Make vars.properties available to spring integration configuration * System properties are only used if there is no setting in vars.properties. */ @Bean(name = "aemServiceConfigurationPropertiesConfigurer") public static PropertySourcesPlaceholderConfigurer configurer() { PropertySourcesPlaceholderConfigurer ppc = new PropertySourcesPlaceholderConfigurer(); ppc.setLocation(new ClassPathResource("META-INF/spring/jwala-defaults.properties")); ppc.setLocalOverride(true); ppc.setProperties(ApplicationProperties.getProperties()); return ppc; }
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), PropertySourcesPlaceholderConfigurer.class); BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(), ApolloAnnotationProcessor.class); }
@SuppressWarnings("unchecked") @SneakyThrows private <T> Optional<T> getPropertyValue(PropertySourcesPlaceholderConfigurer placeholderConfigurer, String property) { Field field = ReflectionUtils.findField(placeholderConfigurer.getClass(), property); ReflectionUtils.makeAccessible(field); return Optional.fromNullable((T) field.get(placeholderConfigurer)); }
@Test public void doesNotEarlyInitializeFactoryBeans() throws Exception { SpringApplication application = new SpringApplication(EarlyInitConfig.class, PropertySourcesPlaceholderConfigurer.class, EmbeddedServletContainerAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); this.context = application.run("--server.port=0"); String bean = (String) this.context.getBean("earlyInit"); assertThat(bean).isEqualTo("bucket"); }
/** * 易变属性源 * * @param placeholderConfigurer 占位符配置器 * @return 易变属性源 */ private MutablePropertySources mergePropertySources(PropertySourcesPlaceholderConfigurer placeholderConfigurer) { Optional<MutablePropertySources> multablePropertySources = getPropertySourcesSources(placeholderConfigurer); if (multablePropertySources.isPresent()) { multablePropertySources.get().addLast(registryPropertySource); return multablePropertySources.get(); } return this.builder(placeholderConfigurer).environmentSources().localPropertiesSources().elastiConfigSources() .bulid(); }
@Bean public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties); propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true); return propertySourcesPlaceholderConfigurer; }
private void autowireThis(HttpSecurity http) { final ApplicationContext parent = http.getSharedObject(ApplicationContext.class); checkForJwtAnnotation(parent); final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.setParent(parent); context.register(PropertySourcesPlaceholderConfigurer.class); context.register(getClass()); context.refresh(); context.getAutowireCapableBeanFactory().autowireBean(this); }
public static Optional<PropertiesPropertySource> getLocalPropertiesSources( PropertySourcesPlaceholderConfigurer placeholderConfigurer) { Method method = ReflectionUtils.findMethod(placeholderConfigurer.getClass(), PropertySourcesEnum.MERGEPROPERTIES.getName()); ReflectionUtils.makeAccessible(method); Properties properties = (java.util.Properties) ReflectionUtils.invokeMethod(method, placeholderConfigurer); return Optional.fromNullable(new PropertiesPropertySource(PropertySourcesEnum.LOCALPROPERTIES.getName(), properties)); }
/** * 初始化PlaceholderMap * * @param beanFactory 可配置列表Bean工厂 * @return placeholderMap */ private Map<String, PropertySourcesPlaceholderConfigurer> initPlaceholderMap( final ConfigurableListableBeanFactory beanFactory) { Map<String, PropertySourcesPlaceholderConfigurer> placeholderMap = beanFactory.getBeansOfType( PropertySourcesPlaceholderConfigurer.class, true, false); if (placeholderMap.isEmpty()) { beanFactory.registerSingleton(PropertySourcesPlaceholderConfigurer.class.getCanonicalName(), new PropertySourcesPlaceholderConfigurer()); placeholderMap = beanFactory.getBeansOfType(PropertySourcesPlaceholderConfigurer.class); } return placeholderMap; }
@Test public void withPropertyPlaceholderClassName() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "mybeanclass=java.lang.String"); this.context.register(PropertySourcesPlaceholderConfigurer.class, WithPropertyPlaceholderClassName.class, OnBeanClassConfiguration.class); this.context.refresh(); }