Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
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 (46k 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.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Oct 31, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Oct 26, 2019 in Java by Anvi (10.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...