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!
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 .
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.
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.
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.
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/
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With