In Spring Framework, for the @PropertySource
, according with its API or javadoc it has the name attribute
Indicate the name of this property source.
If omitted, the factory() will generate a name based on the underlying resource
(in the case of DefaultPropertySourceFactory:
derived from the resource description through a
corresponding name-less ResourcePropertySource constructor).
Ok, is clear that is possible define the name such as:
@PropertySource(name="infra",
value={"classpath:/com/.../infrastructure.properties"})
But How and where use that name? What is its purpose?
It was reported at SO at:
The comment is valid, but would be nice if can be expanded at both Spring Reference documentation and javadoc
Comment From: anii1827
hi @manueljordan,
This will provide more clarity for the name attribute of the @PropertySource
annotation.
MutablePropertySources
will provide to get specific properties file by name provided in @PropertySource
annotation.
@Autowired
Environment env;
MutablePropertySources sources = ((ConfigurableEnvironment)env).getPropertySources();
PropertySource<?> propertySource = sources.get("infra");
Object property = propertySource.getProperty("name.value");
System.out.println(property);
Comment From: sbrannen
To summarize what's been stated on Stack Overflow and above by @anii1827...
The name of a PropertySource
serves two general purposes.
- diagnostics: to determine the source of the properties in logging and debugging
- programmatic interaction with
MutablePropertySources
: the name can be used to retrieve properties from a particular property source (or to determine if a particular named property source already exists). The name can also be used to add a new property source relative to an existing property source (see theaddBefore()
andaddAfter()
methods).
Note that this issue has been assigned the documentation
label.
Comment From: anii1827
hi @sbrannen I would like to contribute to this. can you assign to me?
Comment From: manueljordan
Thanks to both the valuable feedback. Now has more sense.
Comment From: sbrannen
Hi @anii1827,
I would like to contribute to this. can you assign to me?
That's very kind of you to offer!
Unfortunately, I had already started on this issue (see https://github.com/spring-projects/spring-framework/commit/49a4ed2ffaa72a4565224d057cc2dca8d57cd0f0), and I drafted the text I plan to include in https://github.com/spring-projects/spring-framework/issues/30195#issuecomment-1492957314.
In light of that, it's easier if I keep this issue assigned to me.
Cheers,
Sam
Comment From: manueljordan
Sam, just being curious your point (2) is already explained by the valuable comment of anii1827 - but could you pls add a simple snipet code about your point (1)? I want understand very well that point - If I understand correctly, if exists the db.url
property, through the point (1) is possible to know from what .properties
file comes from?, Am I correct?
Comment From: sbrannen
@manueljordan, core Spring Framework does not provide any direct mechanism for linking a specific property to a PropertySource
.
Rather, that's a feature of Spring Boot: org.springframework.boot.origin.PropertySourceOrigin
.
Spring Boot uses its Origin
abstraction in several places for logging and diagnostics.