0 votes
1 view
in Java by (3.4k points)

Is there a way to statically/globally request a copy of the ApplicationContext in a Spring application?

Assuming the main class starts up and initializes the application context, does it need to pass that down through the call stack to any classes that need it, or is there a way for a class to ask for the previously created context? (Which I assume has to be a singleton?)

1 Answer

0 votes
by (41.5k points)

If the object that requires access to the container is a bean in the container, just complete the BeanFactoryAware or ApplicationContextAware interfaces.

If an object outside the container needs a path to the container, I've used a standard GoF singleton pattern for the spring container. That way, you only own one singleton in your application, the excess are all singleton beans in the container.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...