spring autowiring not working from a non-spring managed class

前端 未结 10 923
暗喜
暗喜 2020-12-03 06:43

I have a class (Class ABC) that\'s instantiated by calling the constructor. Class ABC in turn has a helper class (Class XYZ) injected using auto-wired.

Ours is a Sp

10条回答
  •  情书的邮戳
    2020-12-03 07:42

    Autowiring won't work because class ABC is not managed by Spring. You could make Spring manage ABC by using one of the @Component annotations (@Component, @Service, @Controller, etc) above the class definition, and then using context:component-scan in your application context XML, or go old school and just define the bean directly in your application context.

    If for some reason you can't make Spring manage class ABC, you can load the application context in ABC using something like:

    ApplicationContext context = new ClassPathXmlApplicationContext("path/to/applicationContext.xml");

    and then use:

    XYZ someXyz = (XYZ) context.getBean("MyXYZ");

    to manually set the bean value.

提交回复
热议问题