Spring @Autowired not working

后端 未结 7 1951
再見小時候
再見小時候 2021-02-08 12:35

I have some problems wth autowire annotation. My app looks like this:

Here is controller:

@Controller
public class MyController {
    @Autowired
    @Qua         


        
7条回答
  •  说谎
    说谎 (楼主)
    2021-02-08 12:51

    I found the solution. As Javi said (thanks a lot for you, Javi), I have to annotate DAO and Service layer classes with @Repository and @Service annotation. Now I've tried to write like this:

    @Service("someService")
    public class SomeServiceImpl implements SomeService{    
        @Autowired
        @Qualifier("myDAO")
        private MyDAO myDAO;
    
        ....
    }
    

    and

    @Repository("myDAO")
        public class JDBCDAOImpl implements MyDAO {    
        @Autowired
        @Qualifier("dataSource")
        private DataSource dataSource;    
        ....
    }
    

    and all works fine!!!

    But I still not found an answer for this quesion: if application will be more complex, and will have more complex structure, where @Repositore and @Service annotation are not preferred for some classes, how to inject correctly beans, which located in lower levels (in a fields of classes, or in a field of fields of classes) (with @Autowire annotation, of course)?

提交回复
热议问题