Binding with @Autowired not working inside instances initiated with 'new'

前端 未结 5 1022
谎友^
谎友^ 2021-01-20 08:04

In my web spring application I create an instance with key word new as following.
In my one of action class, following method exists.

publi         


        
5条回答
  •  醉梦人生
    2021-01-20 08:07

    Another solution can be the use of @Component like this:

    @Component("myBean")
    public class MyBean{  
    
        @Autowired  
        MyService service;  
    
        public void process(){ 
    
        service.execute(); // this service instance has not initialized by Spring DI :( .service object is null. 
    
        }
    }
    

    And at your class where process() is, you can Autowire like this:

    @Autowired
    MyBean b
    
    public void process(){  
    
    
      b.process();
    
    
    }   
    

提交回复
热议问题