Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property placeholder from Configuration

Tags:

java

spring

With Spring in xml context we can simple load properties like this:

<context:property-placeholder location:"classpath*:app.properties"/>

Is there any chance to configure same properties inside @Configuration bean (~ from java code) without boilerplate?

Thanks!

like image 394
aim Avatar asked Feb 06 '13 08:02

aim


People also ask

How do you make a property placeholder in mule 4?

You can use the <global-property> element to set a placeholder value from within your Mule configuration, such as from within another Mule configuration file. You can use the global property syntax to reference the values from a . yaml or .

What is property placeholder in mule 4?

February 17, 2022. Secure Property Placeholder is an essential standard for keeping our Sensitive data like User ID and Password secure (encrypted/cipher-text) in the Property file. Securing properties is one of the crucial elements in every Mule project.

What is a configuration property?

The ConfigurationProperty class represents an individual configuration setting. This class allows you to get or set the name, type, and default value for a particular configuration entity (attribute or element) and specify whether the attribute is required, is an element key, or represents a default element collection.

What can I use instead of PropertyPlaceholderConfigurer?

Just use: final var configurer = new PropertySourcesPlaceholderConfigurer(); configurer. setProperties(properties); Side note: It even works as a replacement for configuring properties over the XML configuration.


2 Answers

You can use the annotation @PropertySource like this

@Configuration
@PropertySource(value="classpath*:app.properties")
public class AppConfig {
 @Autowired
 Environment env;

 @Bean
 public TestBean testBean() {
     TestBean testBean = new TestBean();
     testBean.setName(env.getProperty("testbean.name"));
     return testBean;
 }
}

See: http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/context/annotation/PropertySource.html

EDIT: if you are using spring boot you can use @ConfigurationProperties annotation to wire the properties file directly to bean properties, like this:

test.properties

name=John Doe
age=12

PersonProperties.java

@Component
@PropertySource("classpath:test.properties")
@ConfigurationProperties
public class GlobalProperties {

    private int age;
    private String name;

    //getters and setters
}

source: https://www.mkyong.com/spring-boot/spring-boot-configurationproperties-example/

like image 141
rvazquezglez Avatar answered Sep 29 '22 01:09

rvazquezglez


Manual configuration can be done via following code

public static PropertySourcesPlaceholderConfigurer loadProperties(){
  PropertySourcesPlaceholderConfigurer propertySPC =
   new PropertySourcesPlaceholderConfigurer();
  Resource[] resources = new ClassPathResource[ ]
   { new ClassPathResource( "yourfilename.properties" ) };
  propertySPC .setLocations( resources );
  propertySPC .setIgnoreUnresolvablePlaceholders( true );
  return propertySPC ;
}

Sources: Property Placeholder

like image 40
Dangling Piyush Avatar answered Sep 29 '22 02:09

Dangling Piyush