New in Spring: Load Application Context

1k Views Asked by At

I am new with Spring.

So far in my application, every time I need to use a bean I load the XML.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

So, in every class where I need to load a particular bean I use the above line.

In terms of efficciency or right use, I would like to know if this is the correct use (I suspect it is not) or if the context should be passed as parameter everytime one class need it.

Thanks

3

There are 3 best solutions below

0
On

I assume that you are using Spring in non-web application.

If you are creating new application context each time you need to retrieve a bean, it is indeed not the correct solution. You should create application context once per application.

So the solution would be as you suggest passing the application context instance to classes that need it, or to otherwise ensure that you use the same instance in your application.

One of many issues you might run into with your current setup is problem with bean scoping. Spring has singleton beans, but those are singletons only within one application context. So if you retrieve a bean that is singleton from two different application contexts, they will not be the same instance. Other issues will involve performance, because application context creation will be expensive operation.

0
On

If you use spring, then you should use it everywhere. So instead of passing the application context around, put every bean in there and let Spring connect the dots for you.

In a nutshell, never call new yourself. Ask Spring for the bean instead. If a bean has dependencies, use constructor injection.

That way, Spring can create all the beans for you, wire them up and return fully working instances without you having to worry about where something comes form.

You should also read the articles about Java-based container configuration.

Related articles:

1
On

Just Load the XML file and create the application context object. As long as XML is loaded, Spring will inject all the object dependencies.

So, You don't need to load or create application context object again and again. Just check your console with the below example, you will understand it.

Example: In main method you only write this line of code.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

interface IParent {
    public void whoAreYou();
}

class ChildA implements IParent {

    public ChildA (IParent ChildB) {
        super();
        System.err.println("Constructor ChildA ");
        ChildB.whoAreYou();
    }

    public ChildA (){
        System.err.println("Constructor ChildA ");
    }

    @Override
    public void whoAreYou() {
        System.err.println("ChildA ");
    }

}

class ChildB implements IParent {

    public ChildB (){
        System.err.println("Constructor ChildB");
    }

    @Override
    public void whoAreYou() {
        System.err.println("ChildB");

    }
}

XML File :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">


   <context:annotation-config/>

   <bean id="childA" class="com.spring.xmlbased.config.ChildA">
       <constructor-arg>
           <bean id="ChildB" class="com.spring.xmlbased.config.ChildB"/>
       </constructor-arg>
   </bean>
</beans>

Please let me know, if u need further clarification.