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 use of Provider interface at injection point [SPR-13117] #17708

Closed
spring-issuemaster opened this issue Jun 12, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Jun 12, 2015

Caleb Cushing opened SPR-13117 and commented

Let's say I define a bean

@Bean
@Scope( "request" )
static Foo myFoo() {
...

and have another

@Service
public class MySingletonService {
...

that needs the request scoped instance of foo across multiple methods.

what I'd like to be able to write is something like this

@Autowired Provider<Foo> fooProvider;

public void myUsesFoo() {
     Foo foo = fooProvider.get();
     ...
}

to do this currently I am Autowiring ApplicationContext and calling getBean(...). I would like to wire something less flexible and have it work in a "magic" way. meaning I don't write the provider I just request one. I would also like implementing the provider to be easy for unit test purposes.

as a Note: I'd be ok if Provider was a new interface, or perhaps I could just request any Bean wrapped with Optional or ThreadLocal (or something I haven't thought of).


Issue Links:

  • #12515 Provide better documentation for support of JSR-330 (@Inject) ("is depended on by")

Referenced from: commits a13a1eb

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 12, 2015

Juergen Hoeller commented

I'm not entirely sure I understand your request: What you have in mind seems to be pretty much what javax.inject.Provider is designed for, which we support already - in exactly the style that you outlined? Alternatively, you could use Spring's ObjectFactory interface for the same purpose, not having to drag in the javax.inject jar.

Am I missing something?

Juergen

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 12, 2015

Caleb Cushing commented

no I just somehow missed this when reading through the docs

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jun 13, 2015

Caleb Cushing commented

Maybe it would be a good time to improve reference docs? http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-factory-scopes-other-injection doesn't mention either of these options, there is brief mention of ObjectFactory in custom scope but not as an option for injecting into singletons (though now reading again that that's not what you're saying).

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 17, 2015

Caleb Cushing commented

though I just renamed this issue to Document Providers, it might be worth considering allowing a Java 8 Supplier to be used instead of Provider or ObjectFactory.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 28, 2015

Juergen Hoeller commented

I've added coverage of ObjectFactory and Provider to the corresponding sections of the reference docs.

As for a Java 8 Supplier, while we could theoretically support it as an alternative variant, it isn't semantically defined to do what we want to achieve here. From that perspective, I prefer one of our variants that are explicitly meant for that purpose.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.