user5268786
user5268786

Reputation:

How to use same instance of ClassPathXmlApplicationContext in whole App?

I am using Spring JDBCTemplate + Java example. In this project, I need to use below code many times in my StudentDetailsLoader, UserDetailsLoader, AdminDetailsLoader and VendorDetailsLoader classes and in many classes to load respective class beans.

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

Is there any way we just create in one place and call its singleton instance wherever we need? Please guide.

Upvotes: 2

Views: 136

Answers (1)

PAA
PAA

Reputation: 12015

Create a Bean and simply call instance of the bean

public class AppContext {
    public static ApplicationContext getAppContext(){
        return new ClassPathXmlApplicationContext("applicationContext.xml");
    }
}

The bean

<bean id="appContext" class="com.common.rest.AppContext" />

Upvotes: 1

Related Questions