@Autowired and @Service working from controller but not from a different package

后端 未结 5 801
栀梦
栀梦 2021-02-08 14:54

I need help understanding the concept behind @Autowired and @Service. I have a DAO defined with @Service and controller with @Autowi

5条回答
  •  野的像风
    2021-02-08 15:39

    When using classpath scanning, you have to communicate to Spring which classes to manage. This is done using the @Service annotation, and its relations (@Controller, @Repository, etc).

    If you choose not to annotate your bean, you must explicitly declare it in your config, just like you did with dataSource and jdbcTemplate.

    Annotating your classes means only those classes in the package are managed by Spring; which allows you to scan a package without having to manage all classes in that package.

提交回复
热议问题