Organizational Research By

Surprising Reserch Topic

Question:How to avoid using ApplicationContext.getBean() when implementing Spring IOC



asked Sep 13, 2013 in Core java by anonymous
edited Sep 12, 2013
0 votes
8,312 views



Related Hot Questions

4 Answers

0 votes

My contextProvider Class Code

public class ContextProvider implements ApplicationContextAware {

    private static ApplicationContext ctx;

    /**
     * Objects as properties
     */
    private static DUtils utils;

    public void setApplicationContext(ApplicationContext appContext)
            throws BeansException {
        ctx = appContext;

    }

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

    public static DUtils getUtils() {
        return utils;
    }

    public void setUtils(DUtils dUtilsRef) {
        utils = dUtilsRef;
    }

}

For instance, consider a class A that depends on org.hd.derbyops.DUtils. I'm using the following code line

ContextProvider.getUtils();

inorder to get DUtils Object in class A, thus avoiding usage of ApplicationContext.getBean() anywhere in my code.

Assume, if I have 10 classes and my class A is dependent on all of them, whose objects to be created and accessed without using ApplicationContext.getBean(). In that case also, as done above, I have a thought of creating properties of ContextProvider class followed by setter's and getter's of that property, where in get is static. So that, I can use it wherever I'm in need of an object, like this

ContextProvider.get<PropertyName>;

Here's my brief question. Firstly, Is my approach right? If it's right, loading all the beans at the start-up, wouldn't it be a performance killer? How would you do that in your applications without calling getBean atleast more than once?

If you were to design a web-application & you were to implement Spring IOC, without using ApplicationContext.getBean() in any of the code. How would you do that?

answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

I'm just getting started with Spring IOC concept. I often see most of the examples found in the internet use the code to get the object.

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) appContext.getBean("hello"); 

As a reference from these questions 1 and 2 in the stackoverflow. I've inferred that, it's not necessary to use appContext.getBean("hello") in the code which is considered to be the bad practice. Also, not recommended anymore. Correct me right here, If my inference is wrong.

Keeping that in view, I have made changes in my project accordingly. Here's my applicationContext.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="utilClassRef" class="org.hd.derbyops.DUtils" lazy-init="false" />
<bean id="appContext" class="org.hd.derbyops.ContextProvider" lazy-init="false">
   <property name="utils" ref="utilClassRef" />
bean>
beans>
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

My contextProvider Class Code

public class ContextProvider implements ApplicationContextAware {

    private static ApplicationContext ctx;

    /**
     * Objects as properties
     */
    private static DUtils utils;

    public void setApplicationContext(ApplicationContext appContext)
            throws BeansException {
        ctx = appContext;

    }

    public static ApplicationContext getApplicationContext() {
        return ctx;
    }

    public static DUtils getUtils() {
        return utils;
    }

    public void setUtils(DUtils dUtilsRef) {
        utils = dUtilsRef;
    }

}

For instance, consider a class A that depends on org.hd.derbyops.DUtils. I'm using the following code line

ContextProvider.getUtils();

inorder to get DUtils Object in class A, thus avoiding usage of ApplicationContext.getBean() anywhere in my code.

Assume, if I have 10 classes and my class A is dependent on all of them, whose objects to be created and accessed without using ApplicationContext.getBean(). In that case also, as done above, I have a thought of creating properties of ContextProvider class followed by setter's and getter's of that property, where in get is static. So that, I can use it wherever I'm in need of an object, like this

ContextProvider.get<PropertyName>;

Here's my brief question. Firstly, Is my approach right? If it's right, loading all the beans at the start-up, wouldn't it be a performance killer? How would you do that in your applications without calling getBean atleast more than once?

If you were to design a web-application & you were to implement Spring IOC, without using ApplicationContext.getBean() in any of the code. How would you do that?

answered Sep 13, 2013 by anonymous
edited Sep 12, 2013
0 votes

I'm just getting started with Spring IOC concept. I often see most of the examples found in the internet use the code to get the object.

ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) appContext.getBean("hello"); 

As a reference from these questions 1 and 2 in the stackoverflow. I've inferred that, it's not necessary to use appContext.getBean("hello") in the code which is considered to be the bad practice. Also, not recommended anymore. Correct me right here, If my inference is wrong.

Keeping that in view, I have made changes in my project accordingly. Here's my applicationContext.xml

xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id="utilClassRef" class="org.hd.derbyops.DUtils" lazy-init="false" />
<bean id="appContext" class="org.hd.derbyops.ContextProvider" lazy-init="false">
   <property name="utils" ref="utilClassRef" />
bean>
beans>
answered Sep 13, 2013 by anonymous
edited Sep 12, 2013

...