Spring get current ApplicationContext

后端 未结 11 911
陌清茗
陌清茗 2020-11-28 20:04

I am using Spring MVC for my web application. My beans are written in \"spring-servlet.xml\" file

Now I have a class MyClass and i want to

11条回答
  •  南方客
    南方客 (楼主)
    2020-11-28 20:30

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/spring-servlet.xml");
    

    Then you can retrieve the bean:

    MyClass myClass = (MyClass) context.getBean("myClass");
    

    Reference: springbyexample.org

提交回复
热议问题