Skip to content

@LookupMethod annotation for use with component scanning [SPR-5192] #9865

Closed
@spring-projects-issues

Description

@spring-projects-issues

Casey Butterworth opened SPR-5192 and commented

I have recently started some work on an @LookupMethod annotation to be used in the following situation:

  • A prototype scoped bean needs to be used within a singleton
  • The singleton was created using component scanning

Currently the most obvious solution would be to forgo component scanning for the singleton and define the lookup-method in the ApplicationContext XML or using spring-java-config, e.g:

<!-- this could also be created using component scanning -->
<bean id="myPrototype" class="sample.MyPrototype" scope="prototype"/>

<bean id="mySingleton" class="sample.MySingleton">
  <lookup-method name="createMyPrototype" bean="myPrototype"/>
</bean>

However, since I've been using component scanning with 2.5, it doesn't feel right defining the wiring outside of the components, and it would be ideal if we could do something like the following:

@Component
public abstract class MySingleton {
   @LookupMethod(beanRef = "myPrototype")
   protected abstract MyPrototype createMyPrototype();
}

@Component
public class MyPrototype {}

It would be even better if @LookupMethod could work in conjunction with autowiring (by type), but that can be a subsequent feature request.

I've started to implement a solution and will attach shortly.


Attachments:

Issue Links:

Referenced from: commits eb0ab84

37 votes, 33 watchers

Metadata

Metadata

Assignees

Labels

has: votes-jiraIssues migrated from JIRA with more than 10 votes at the time of importin: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions