Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to retrieve a Spring bean with prototype scope without using ApplicationContextAware

Using Spring 3.1. If I want to retrieve a bean with prototype scope (i.e. I want a different instance of the class each time), is it possible to retrieve the bean without having to use an ApplicationContextaware class?

This is how I do it at present

@Component
@Qualifier("MyService")
public class MyServiceImpl implements MyService {

    @Override
    public void doSomething() {
        Blah blah = (Blah)ApplicationContextProvider.getContext().getBean("blah");
        blah.setThing("thing");
        blah.doSomething();
    }
}


@Component("blah")
@Scope("prototype")
public class Blah {
    ....
}

where ApplicationContextProvider implements ApplicationContextAware.

Is it possible to do this with annotations or simple Spring configuration without having to use an ApplicationContextAware class?

like image 413
CodeClimber Avatar asked Nov 13 '22 13:11

CodeClimber


1 Answers

Spring has some fairly sophosticated methods for achieving what you're after...

See the spring documentation: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection

Searching for spring proxy scope on google also threw up some results...

like image 115
Michael Wiles Avatar answered Nov 15 '22 05:11

Michael Wiles