1
votes

I'm studying spring beans and came across @Lookup, it says:

If we happen to decide to have a prototype Spring bean, then we are almost immediately faced with the problem of how will our singleton Spring beans access these prototype Spring beans?

hmm, I don't get it, because when I studied scope=prototype it says:

4.4.2 The prototype scope The non-singleton, prototype scope of bean deployment results in the creation of a new bean instance every time a request for that specific bean is made

so it seems i misinterpreted the words:

a request for that specific bean is made

actually programming in spring framework every line of the code is inside of some bean (i.e. @controller, @Service, etc), isn't it? And almost all of them are singletons, isn't it? So if I need prototype I just make scope=prototype and almost everytime it's injected to another bean (i.e. @controller, @Service, etc) isn't it?

So please give a real world scenarios, 1) when one should use @Lookup and 2) when it's not needed Ok for the 1) the scenario:

@Component
@Scope("prototype")
public class SchoolNotification {
    // ... prototype-scoped state
}

@Component
public class StudentServices {

    // ... member variables, etc.

    @Lookup
    public SchoolNotification getNotification() {
        return null;
    }

    // ... getters and setters
}

Please, show me scenario for the 2) case, and explain please the difference Thank u

1
For additional edification, the term you can use for further research is "method injection". Prior to the addition of the @Lookup annotation, the way to actually inject the singleton bean involved some very confusing syntax that involved making the singleton class abstract.Roddy of the Frozen Peas

1 Answers

2
votes

The implicit Bean scope in Spring is Singleton.
That means for a JVM instance, only a single instance of a Bean exists in memory (theoretically).

When you @Autowire a Prototype-scoped Bean inside a Singleton-scoped Bean, that Prototype one becomes a sort-of-singleton. Just think about it; a Singleton gets created, its injectable fields get Autowired, and that is it, the instance lives forever along with all its fields (keep in mind those Prototype-scoped fields are "pure" instances, they're not proxied).


@Lookup

is a proxy-driven annotation. What that means is Spring will extend your class using JDK proxies or CGLIB proxies, and it will override/implement the @Lookup-annotated method, providing its own version which uses a BeanFactory#getBean each time it is invoked.
The documentation is clear on this point

An annotation that indicates 'lookup' methods, to be overridden by the container to redirect them back to the BeanFactory for a getBean call.

Thus, that means a fresh Bean instance is returned every time.


Just for your knowledge, another approach for working with Prototype-scoped Beans inside
"other"-scoped Beans is using ProxyFactoryBean. The only difference is that the proxy is created at configuration-time, and then made available for direct @Autowireing, thus not requiring the definition of a @Lookup method, which sometimes is not wanted (usually by folks that are obsessed with clean code, like me).