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

后端 未结 5 799
栀梦
栀梦 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:56

    It is because your POJO class is not managed by spring container.

    @Autowire annotation will work only those objects which are managed by spring (ie created by the spring container).

    In your case the service and controller object are managed by spring, but your POJO class is not managed by spring, that is why the @Autowire is not producing the behavior expected by you.

    Another problem I noticed is, you are using the @Service annotation in the DAO layer when spring has the @Repository annotation specifically created for this purpose.

    Also it is not desirable to allow spring to manage the POJO classes since normally it will be data storage elements which has to be created outside the container.

    Can you tell us what is the purpose of the POJO class and why the service instance is used in it?

提交回复
热议问题