Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-13117

Document use of Provider interface at injection point

    Details

    • Type: Task
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: None
    • Fix Version/s: 4.2.3
    • Component/s: None
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      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

          Activity

          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          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

          Show
          juergen.hoeller Juergen Hoeller added a comment - 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
          Hide
          xenoterracide Caleb Cushing added a comment -

          no I just somehow missed this when reading through the docs

          Show
          xenoterracide Caleb Cushing added a comment - no I just somehow missed this when reading through the docs
          Hide
          xenoterracide Caleb Cushing added a comment -

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

          Show
          xenoterracide Caleb Cushing added a comment - 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).
          Hide
          xenoterracide Caleb Cushing added a comment -

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

          Show
          xenoterracide Caleb Cushing added a comment - 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`.
          Hide
          juergen.hoeller Juergen Hoeller added a comment -

          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

          Show
          juergen.hoeller Juergen Hoeller added a comment - 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

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              xenoterracide Caleb Cushing
              Last updater:
              St├ęphane Nicoll
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                2 years, 17 weeks, 3 days ago