Spring How to autowire a component without using @Component or other derivatives

前端 未结 2 1802
南笙
南笙 2020-12-19 15:58

I would like to autowire a component (still using the @Autowired annotation), but not require it to have the @Component (or other similar annotations) on it. How would I do

2条回答
  •  盖世英雄少女心
    2020-12-19 16:20

    I don't sure, If I correctly understood to your question. But if you want inject bean B without marking bean A via some annotation, or xml definition, you can use SpringBeanAutowiringSupport

    public class A {
    
        @Autowired
        private class B b;
    
        public A{
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); 
        }
    
    }
    

提交回复
热议问题