Reputation:
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
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