user103555
user103555

Reputation: 39

Insert bean from applicationContext into POJO

I have all of my DAO's as beans in my applicationContext. Now this works great for autowiring these objects into my controllers to make database calls. However, given that I have embedded objects within some of my POJO's (Example: User class has list of pendingsurvey objects, list of appointment objects, etc) and don't want to grab these embedded objects when I grab the main object (Example: In a page with a list of users I don't need to know their pendingsurveys or appointments), I had set it up so that if the embedded object is null, go to the database to get the data. However, apparently @Autowire doesn't work in this case because the POJO's are not a Spring controlled object or something.

So question now is, how can I get a bean from applicationContext to use in my POJO's? I'm not that good with Spring so specific instructions would be greatly appreciated..

I've tried this but it gives me a bunch of injection dependency errors:

/* applicationContext.xml */
<bean id="userDao" class="UserDao" scope="singleton">
    <property name="connectionWrapper" ref="connectionWrapper" />
</bean>

/* User.java - I tried putting in constructor and in the getter for userDao */
ApplicationContext ctx = AppContext.getApplicationContext('applicationContext.xml');
UserDao userDao = (UserDao) ctx.getBean("userDao");

Upvotes: 0

Views: 6907

Answers (2)

user103555
user103555

Reputation: 39

Got it to work, same concept as Ric Jafe's answer but different implementation of it (Source: http://blog.jdevelop.eu/?p=154):

Made this class:

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext ctx = null;

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        // Assign the ApplicationContext into a static method
        this.ctx = ctx;
    }
}

Added this line into applicationContext.xml:

<bean id="applicationContextProvider" class="org.ApplicationContextProvider"></bean>

Called it in my POJO with:

ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
if (applicationContext != null && applicationContext.containsBean("keyName")) {
    object = (Object) applicationContext.getBean("keyName");
}

Thanks for all the help guys.

Upvotes: 2

Ric Jafe
Ric Jafe

Reputation: 2321

When you load a context like that you are creating a new context, a copy of what was created when the application loaded. To access the same context that your application is using, you have several options, documented here: http://mythinkpond.wordpress.com/2010/03/22/spring-application-context/

For ease of access I'll copy/paste the one that most likely you want to use:

public class MyClass implements ApplicationContextAware {

    static final long serialVersionUID = 02L;

    ApplicationContext applicationContext = null;

    public void doSomething(){
        if (applicationContext != null && applicationContext.containsBean("accessKeys")){
            MyBean beanA = (MyBean) applicationContext.getBean("mybean");
            //Do something with this AccessBean
        }

        return null;
    }

    @Override
    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
        System.out.println("setting context");
        this.applicationContext = applicationContext;
    }

}

Notice the implements ApplicationContext. This tells the Spring framework you need the Application Context in this class.. So it autowires it to the applicationContext variable. For this to happen you also need the setter method. Then you can just use it and get your beans. Good coffee :)

P.S. - If you need the context in other classes, you can pass the applicationContext variable to them or use the same method. This way you only have 1 context at all times, containing your beans.

Upvotes: 1

Related Questions