Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document purpose of name attribute in @PropertySource #30195

Closed
manueljordan opened this issue Mar 26, 2023 · 7 comments
Closed

Document purpose of name attribute in @PropertySource #30195

manueljordan opened this issue Mar 26, 2023 · 7 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Milestone

Comments

@manueljordan
Copy link

manueljordan commented Mar 26, 2023

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

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 26, 2023
@rstoyanchev rstoyanchev added in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task labels Mar 28, 2023
@anii1827
Copy link

anii1827 commented Apr 1, 2023

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);

@sbrannen sbrannen changed the title Improve documentation to explain: How and where use the "name" of the @PropertySource? Document purpose of name attribute in @PropertySource Apr 1, 2023
@sbrannen
Copy link
Member

sbrannen commented Apr 1, 2023

To summarize what's been stated on Stack Overflow and above by @anii1827...

The name of a PropertySource serves two general purposes.

  1. diagnostics: to determine the source of the properties in logging and debugging
  2. 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 the addBefore() and addAfter() methods).

Note that this issue has been assigned the documentation label.

@sbrannen sbrannen removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 1, 2023
@sbrannen sbrannen added this to the 6.0.x milestone Apr 1, 2023
@anii1827
Copy link

anii1827 commented Apr 1, 2023

hi @sbrannen
I would like to contribute to this.
can you assign to me?

@sbrannen sbrannen self-assigned this Apr 1, 2023
@sbrannen sbrannen modified the milestones: 6.0.x, 6.0.8 Apr 1, 2023
@manueljordan
Copy link
Author

Thanks to both the valuable feedback. Now has more sense.

@sbrannen
Copy link
Member

sbrannen commented Apr 4, 2023

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 49a4ed2), and I drafted the text I plan to include in #30195 (comment).

In light of that, it's easier if I keep this issue assigned to me.

Cheers,

Sam

@sbrannen sbrannen modified the milestones: 6.0.8, 6.0.9 Apr 4, 2023
@manueljordan
Copy link
Author

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?

@snicoll snicoll modified the milestones: 6.0.9, 6.0.x Apr 7, 2023
@sbrannen sbrannen modified the milestones: 6.0.x, 6.1.x Jul 4, 2023
@sbrannen sbrannen modified the milestones: 6.1.x, 6.0.12 Aug 18, 2023
@sbrannen
Copy link
Member

@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.

sbrannen added a commit to sbrannen/spring-framework that referenced this issue Aug 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

6 participants