Spring get current ApplicationContext

后端 未结 11 926
陌清茗
陌清茗 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:47

    Add this to your code

    @Autowired
    private ApplicationContext _applicationContext;
    
    //Add below line in your calling method
    MyClass class = (MyClass) _applicationContext.getBean("myClass");
    
    // Or you can simply use this, put the below code in your controller data member declaration part.
    @Autowired
    private MyClass myClass;
    

    This will simply inject myClass into your application

提交回复
热议问题