Spring obtains the implementation method of the ApplicationContext object tool class,
How to Implement Spring's tool class for obtaining ApplicationContext objects
(1) tool class:
Package com. util; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; final public class ApplicationContextUtil {private static ApplicationContext ac = null; private ApplicationContextUtil () {}static {ac = new ClassPathXmlApplicationContext ("applicationContext. xml ");} public static ApplicationContext getApplicationContext () {// get the returned container object return ac ;}}
(2) usage:
Public static void getByUtil () {ApplicationContextUtil. getApplicationContext (). getBean ("userService ");}
If you have any questions, please leave a message or go to the community on this site for discussion. Thank you for reading this article. Thank you for your support!