-
Notifications
You must be signed in to change notification settings - Fork 38.5k
@LookupMethod annotation for use with component scanning [SPR-5192] #9865
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
Comments
Casey Butterworth commented I am attaching 2 files:
Note that this solution does not work yet for reasons I will outline in an additional comment. |
Casey Butterworth commented As you might expect, this change isn't quite this straightforward otherwise I assume it would already have happened, but hopefully the following can be resolved in a future spring version:
I can think of a few solutions to this:
Personally my preference is option 2, but i guess this will need some impact analysis from someone who understands the lifecycle in more depth than me. If anyone knows how to go about Option 1 I am very open to ideas! Anyway, hopefully this provides enough information for someone to provide some feedback and ideas. |
Casey Butterworth commented Sorry, accidentally attached the annotation twice (can an admin delete?) In any event, we really only need the 0.3 kB version. |
Benjamin Lerman commented Example project for LookupMathodBeanFactoryPostProcessors |
Benjamin Lerman commented Hi, I just attach an other try at this one. I did not start from an AnnotationBeanPostProcessor but from an BeanFactoryPostProcessor because one need to be able to change the bean before it is instanciated. I also tried to implements some of the Qualifier mechanism. It is far from complete, but as long a The other limitation is that I cannot do anything for abstract class, because those are not registered by ClassPathScanningCandidateComponentProvider. That means that bean that are proxied must still have a default implementation of the factory method. |
Cuneyt Tuna commented Is there any progress on this issue? |
Martin Green commented Is there any chance we could get this annotation - it would be extremely usefull and make the code much more self documenting. |
Marko Topolnik commented I would just like to point out that the bean class does not need to be abstract -- in fact, I have never used it that way. This is the way I do it:
|
Alexis Torres commented Hello, I was really interested in that option till I understood Not saying that this shouldn't be implemented but maybe priority is not as important. |
Dmitry Katsubo commented I would vote for
|
Alexey Fansky commented So are there any solutions for this? |
Phil Webb commented Generally the preferred solution with this type of problem is to use the public class MySingleton {
@Autowired
private Provider<MyPrototype> myPrototype;
public void operation() {
MyPrototype instance = myPrototype.get();
// do something with the instance
}
} |
Juergen Hoeller commented I'm considering this for 4.1 still, following up on the work in #15860 and #12089. It is actually quite straightforward to implement, even nicer with a by-type lookup option if no bean name has been specified... and can easily support arguments for factory methods or constructors as well. Juergen |
Juergen Hoeller commented Finally introduced as Juergen |
mirak commented for prototypes, why can't we just call the @Configuration
public class MyConfig implements HouseFactory {
@Override
@Bean
@Scope("prototype")
public House house(String name, int windows){
return new House(name,windows);
}
}
public class Street{
@Autowired
private HouseFactory houseFactory;
public void buildSomeHouses(){
House house1 = houseFactory.house("blue house",5);
House house2 = houseFactory.house("red house",9);
}
}
public interface HouseFactory {
House house(String name, int windows);
} Actually I tried this on spring 3.2.16 which I am stuck on until we migrate servlet versions, and the issue is that it fails because it tries to autowire a String bean and Integer bean, which it can't find of course. |
Juergen Hoeller commented I'm afraid Since you mentioned it, why specifically do you feel stuck on Spring 3.2.x? Spring 4.x is still runtime-compatible with Servlet 2.5 containers, so does that mean you're on Servlet 2.4? Which application server is it in your case? |
mirak commented This is a Jonas 4 something based on tomcat5, which supports servlet 2.4 only. I realised that when I tried to used spring 4.x . We are supposed to migrate to Websphere 8.5.5.4 someday ... Regarding the BeanFactory.getBean what we do actually is something like @Bean
public House houseFactory() {
return new HouseFactory() {
@Override
public HouseFactory House house(String name, int windows) {
return ctx.getBean("house", name, windows );
}
};
} We don't expose the spring api in our business objects, and if a constructor changes, we know the impacts at compile time. |
mirak commented I think I will emulate the spring 4.x |
mirak commented By the way I realised it's possible to also use an interface that implements the |
mirak commented
It seems this JIRA #17048 fixes my issue in Spring 4.1.3 . |
Casey Butterworth opened SPR-5192 and commented
I have recently started some work on an
@LookupMethod
annotation to be used in the following situation: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:
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:
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
The text was updated successfully, but these errors were encountered: