I need to fetch a singleton bean from same ApplicationContext twice in 2 different classes.
Example snippet :
CLass A {
public void foo(){
ApplicationContext context = new ClassPathXmlApplicationContext("common.spring.xml");
MyParametrizedSingletonClass myParametrizedSingletonClass = (MyParametrizedSingletonClass) context.getBean("myParametrizedSingletonClass");
// do more stuff..
}
CLass B {
public void bar(){
ApplicationContext context = new ClassPathXmlApplicationContext("common.spring.xml");
MyParametrizedSingletonClass myParametrizedSingletonClass = (MyParametrizedSingletonClass) context.getBean("myParametrizedSingletonClass");
// do more stuff..
}
Since MyParametrizedSingletonClass is a singletom it if its constructor is called more than once for same constructor arguments it throws error.
How do I cache and reuse ApplicationContext with spring?