org.springframework.context.support.PropertySourcesPlaceholderConfigurer Java Examples

The following examples show how to use org.springframework.context.support.PropertySourcesPlaceholderConfigurer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RqueueMessageHandlerTest.java    From rqueue with Apache License 2.0 6 votes vote down vote up
@Test
public void testMethodHavingNameFromPropertyFile() {
  StaticApplicationContext applicationContext = new StaticApplicationContext();
  applicationContext.registerSingleton("messageHandler", MessageHandlersWithProperty.class);
  applicationContext.registerSingleton("rqueueMessageHandler", RqueueMessageHandler.class);
  Map<String, Object> map = new HashMap<>();
  map.put("slow.queue.name", slowQueue);
  map.put("smart.queue.name", smartQueue);
  applicationContext
      .getEnvironment()
      .getPropertySources()
      .addLast(new MapPropertySource("test", map));

  applicationContext.registerSingleton("ppc", PropertySourcesPlaceholderConfigurer.class);
  applicationContext.refresh();
  MessageHandler messageHandler = applicationContext.getBean(MessageHandler.class);
  MessageHandlersWithProperty messageListener =
      applicationContext.getBean(MessageHandlersWithProperty.class);
  messageHandler.handleMessage(buildMessage(slowQueue, message));
  assertEquals(message, messageListener.getLastReceivedMessage());
  messageListener.setLastReceivedMessage(null);
  messageHandler.handleMessage(buildMessage(smartQueue, message + message));
  assertEquals(message + message, messageListener.getLastReceivedMessage());
}
 
Example #2
Source File: OverrideProperties.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreResourceNotFound(true);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource source = new MockPropertySource()
        .withProperty("rabbitmq.host", "192.168.10.10")
        .withProperty("rabbitmq.port", "5673")
        .withProperty("rabbitmq.username", "jfw")
        .withProperty("rabbitmq.password", "jfw")
        .withProperty("rabbitmq.channel-cache-size", 100);
    propertySources.addLast(source);
    configurer.setPropertySources(propertySources);
    return configurer;
}
 
Example #3
Source File: DefaultApolloConfigRegistrarHelper.java    From apollo with Apache License 2.0 6 votes vote down vote up
@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);

  Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>();
  // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer
  propertySourcesPlaceholderPropertyValues.put("order", 0);

  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(),
      PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues);
  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesProcessor.class.getName(),
      PropertySourcesProcessor.class);
  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloAnnotationProcessor.class.getName(),
      ApolloAnnotationProcessor.class);
  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueProcessor.class.getName(),
      SpringValueProcessor.class);
  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, SpringValueDefinitionProcessor.class.getName(),
      SpringValueDefinitionProcessor.class);
  BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, ApolloJsonValueProcessor.class.getName(),
      ApolloJsonValueProcessor.class);
}
 
Example #4
Source File: Spr12233Tests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void spr12233() throws Exception {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(PropertySourcesPlaceholderConfigurer.class);
	ctx.register(ImportConfiguration.class);
	ctx.refresh();
	ctx.close();
}
 
Example #5
Source File: EagleBeanFactoryPostProcessor.java    From eagle with Apache License 2.0 5 votes vote down vote up
private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer(DefaultListableBeanFactory beanFactory) {
    if (beanFactory instanceof ListableBeanFactory) {
        ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
        Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory
                .getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false,
                        false);
        if (beans.size() == 1) {
            return beans.values().iterator().next();
        }
    }
    return null;
}
 
Example #6
Source File: PersistenceTestContext.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    pspc.setIgnoreResourceNotFound(true);
    pspc.setIgnoreUnresolvablePlaceholders(true);
    return pspc;
}
 
Example #7
Source File: ScheduledAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void propertyPlaceholderWithFixedRate(boolean durationFormat) {
	BeanDefinition processorDefinition = new RootBeanDefinition(ScheduledAnnotationBeanPostProcessor.class);
	BeanDefinition placeholderDefinition = new RootBeanDefinition(PropertySourcesPlaceholderConfigurer.class);
	Properties properties = new Properties();
	properties.setProperty("fixedRate", (durationFormat ? "PT3S" : "3000"));
	properties.setProperty("initialDelay", (durationFormat ? "PT1S" : "1000"));
	placeholderDefinition.getPropertyValues().addPropertyValue("properties", properties);
	BeanDefinition targetDefinition = new RootBeanDefinition(PropertyPlaceholderWithFixedRateTestBean.class);
	context.registerBeanDefinition("postProcessor", processorDefinition);
	context.registerBeanDefinition("placeholder", placeholderDefinition);
	context.registerBeanDefinition("target", targetDefinition);
	context.refresh();

	ScheduledTaskHolder postProcessor = context.getBean("postProcessor", ScheduledTaskHolder.class);
	assertEquals(1, postProcessor.getScheduledTasks().size());

	Object target = context.getBean("target");
	ScheduledTaskRegistrar registrar = (ScheduledTaskRegistrar)
			new DirectFieldAccessor(postProcessor).getPropertyValue("registrar");
	@SuppressWarnings("unchecked")
	List<IntervalTask> fixedRateTasks = (List<IntervalTask>)
			new DirectFieldAccessor(registrar).getPropertyValue("fixedRateTasks");
	assertEquals(1, fixedRateTasks.size());
	IntervalTask task = fixedRateTasks.get(0);
	ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task.getRunnable();
	Object targetObject = runnable.getTarget();
	Method targetMethod = runnable.getMethod();
	assertEquals(target, targetObject);
	assertEquals("fixedRate", targetMethod.getName());
	assertEquals(1000L, task.getInitialDelay());
	assertEquals(3000L, task.getInterval());
}
 
Example #8
Source File: AemServiceConfiguration.java    From jwala with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: RestManagementConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties);
    propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);

    return propertySourcesPlaceholderConfigurer;
}
 
Example #10
Source File: WebSocketDocsTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtobufMessagesEnvelope() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));

		ResponseEntity<String> response = httpClient.getForEntity(
				new URI("http://localhost:" + properties.get("admin.port") + "/schema/envelope/protobuf"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("message Envelope"));
	} finally {
		context.close();
	}
}
 
Example #11
Source File: CommonPropertiesConfiguration.java    From Profiles-Blog with MIT License 5 votes vote down vote up
@Bean
@Profile(Profiles.TEST)
public static PropertySourcesPlaceholderConfigurer testProperties() {
    return createPropertySourcesPlaceholderConfigurer(
            "common_application.properties",
            "common_test_application.properties");
}
 
Example #12
Source File: SyncopeCoreApplication.java    From syncope with Apache License 2.0 5 votes vote down vote up
@ConditionalOnMissingBean(name = "propertySourcesPlaceholderConfigurer")
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws IOException {
    PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
    pspc.setIgnoreResourceNotFound(true);
    pspc.setIgnoreUnresolvablePlaceholders(true);
    return pspc;
}
 
Example #13
Source File: InvalidProperties.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreResourceNotFound(true);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    MutablePropertySources propertySources = new MutablePropertySources();
    MockPropertySource source = new MockPropertySource()
        .withProperty("rabbitmq.port", "invalid");
    propertySources.addLast(source);
    configurer.setPropertySources(propertySources);
    return configurer;
}
 
Example #14
Source File: RestPortalConfiguration.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer properties(@Qualifier("graviteeProperties") Properties graviteeProperties) {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    propertySourcesPlaceholderConfigurer.setProperties(graviteeProperties);
    propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);

    return propertySourcesPlaceholderConfigurer;
}
 
Example #15
Source File: WebSocketDocsTest.java    From chassis with Apache License 2.0 5 votes vote down vote up
@Test
public void testProtobufMessagesSchema() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "false");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));

		ResponseEntity<String> response = httpClient.getForEntity(
				new URI("http://localhost:" + properties.get("admin.port") + "/schema/messages/protobuf"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("message error"));
	} finally {
		context.close();
	}
}
 
Example #16
Source File: PropertyPlaceholderBeanDefinitionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@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 (SYSTEM_PROPERTIES_MODE_DEFAULT.equals(element.getAttribute(SYSTEM_PROPERTIES_MODE_ATTRIBUTE))) {
		return PropertySourcesPlaceholderConfigurer.class;
	}

	// The user has explicitly specified a value for system-properties-mode: revert to
	// PropertyPlaceholderConfigurer to ensure backward compatibility with 3.0 and earlier.
	return PropertyPlaceholderConfigurer.class;
}
 
Example #17
Source File: CommonPropertiesConfiguration.java    From Profiles-Blog with MIT License 5 votes vote down vote up
@Bean
@Profile(Profiles.PROD)
public static PropertySourcesPlaceholderConfigurer prodProperties() {
    return createPropertySourcesPlaceholderConfigurer(
            "common_application.properties",
            "common_prod_application.properties");
}
 
Example #18
Source File: PropertySourcesDeducer.java    From spring-cloud-gray with Apache License 2.0 5 votes vote down vote up
private PropertySourcesPlaceholderConfigurer getSinglePropertySourcesPlaceholderConfigurer() {
    // Take care not to cause early instantiation of all FactoryBeans
    Map<String, PropertySourcesPlaceholderConfigurer> beans = this.applicationContext
            .getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false, false);
    if (beans.size() == 1) {
        return beans.values().iterator().next();
    }
    if (beans.size() > 1 && logger.isWarnEnabled()) {
        logger.warn(
                "Multiple PropertySourcesPlaceholderConfigurer " + "beans registered "
                        + beans.keySet() + ", falling back to Environment");
    }
    return null;
}
 
Example #19
Source File: RetryTestConsumerContextConfig.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setIgnoreResourceNotFound(true);
    configurer.setIgnoreUnresolvablePlaceholders(true);
    return configurer;
}
 
Example #20
Source File: PropertyResolver.java    From mutual-tls-ssl with Apache License 2.0 5 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
    PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
    YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
    yaml.setResources(new ClassPathResource(CLIENT_PROPERTY_FILE));
    propertySourcesPlaceholderConfigurer.setProperties(Objects.requireNonNull(yaml.getObject()));
    return propertySourcesPlaceholderConfigurer;
}
 
Example #21
Source File: PropertySourceConfigurationSource.java    From conf4j with MIT License 5 votes vote down vote up
private List<PropertySourcesPlaceholderConfigurer> getAllPropertySourcesPlaceholderConfigurers() {
    if (!(this.beanFactory instanceof ListableBeanFactory)) {
        return emptyList();
    }

    ListableBeanFactory listableBeanFactory = (ListableBeanFactory) this.beanFactory;
    // take care not to cause early instantiation of flattenedPropertySources FactoryBeans
    Map<String, PropertySourcesPlaceholderConfigurer> beans = listableBeanFactory
            .getBeansOfType(PropertySourcesPlaceholderConfigurer.class, false, false);
    List<PropertySourcesPlaceholderConfigurer> configurers = new ArrayList<>(beans.values());
    configurers.sort(comparingInt(PropertyResourceConfigurer::getOrder));

    return configurers;
}
 
Example #22
Source File: JdbcHttpSessionConfigurationTests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Bean
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
	return new PropertySourcesPlaceholderConfigurer();
}
 
Example #23
Source File: TestDataConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
 
Example #24
Source File: UiWebConfig.java    From oauth2lab with MIT License 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
 
Example #25
Source File: SanitizingEurekaInstanceConfigBeanTest.java    From spring-cloud-services-connector with Apache License 2.0 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer getPropertySourcesPlaceholderConfigurer() {
	return new PropertySourcesPlaceholderConfigurer();
}
 
Example #26
Source File: PropertiesWithJavaConfig.java    From Spring-5.0-Projects with MIT License 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer
  propertySourcesPlaceholderConfigurer() {
   return new PropertySourcesPlaceholderConfigurer();
}
 
Example #27
Source File: DataSourceConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
 
Example #28
Source File: PropertiesWithJavaConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
 
Example #29
Source File: ApplicationConfig.java    From bonita-ui-designer with GNU General Public License v2.0 4 votes vote down vote up
/**
 * load property file described in @PropertySource
 */
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
 
Example #30
Source File: SharedTest.java    From chassis with Apache License 2.0 4 votes vote down vote up
@Test
public void testSwagger() throws Exception {
	Map<String, Object> properties = new HashMap<String, Object>();
	properties.put("admin.enabled", "true");
	properties.put("admin.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("admin.hostname", "localhost");
	
	properties.put("websocket.enabled", "true");
	properties.put("websocket.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("websocket.hostname", "localhost");

	properties.put("http.enabled", "true");
	properties.put("http.port", "" + SocketUtils.findAvailableTcpPort());
	properties.put("http.hostname", "localhost");
	
	AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
	StandardEnvironment environment = new StandardEnvironment();
	environment.getPropertySources().addFirst(new MapPropertySource("default", properties));
	context.setEnvironment(environment);
	context.register(PropertySourcesPlaceholderConfigurer.class);
	context.register(TransportConfiguration.class);
	context.register(MetricsConfiguration.class);

	RestTemplate httpClient = new RestTemplate();
	
	try {
		context.refresh();

		httpClient.setInterceptors(Lists.newArrayList(LOGGING_INTERCEPTOR));
		List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
		for (MessageSerDe messageSerDe : context.getBeansOfType(MessageSerDe.class).values()) {
			messageConverters.add(new SerDeHttpMessageConverter(messageSerDe));
		}
		messageConverters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8));
		httpClient.setMessageConverters(messageConverters);

		ResponseEntity<String> response = httpClient.getForEntity(new URI("http://localhost:" + properties.get("http.port") + "/swagger/index.html"), String.class);
		
		logger.info("Got response: [{}]", response);
		
		Assert.assertEquals(response.getStatusCode().value(), HttpStatus.OK.value());
		Assert.assertTrue(response.getBody().contains("<title>Swagger UI</title>"));
	} finally {
		context.close();
	}
}