What is the most appropriate way of injecting daos in services, services in controllers in Spring?

StackOverflow https://stackoverflow.com/questions/6013702

  •  14-11-2019
  •  | 
  •  

문제

There are many annotations in the Spring framework like @Component, @Service, @Repository, @Service @Resource and @Autowired etc.

What is the most appropriate way of injecting my daos in services, and my service class in the Spring Controller.

With so many annotations it is getting confusing especially with @Autowired working for all situations.

도움이 되었습니까?

해결책

@Service and @Repository are just "sub-annotations" for @Component to specify the bean a bit more (to separete Services from Repositories for more sophisticated stuff). From the point of injection this three are equal.

For injection, there are 3:

  • @Resource
  • @Inject
  • @Autowired

@Autowired is the most powerful annotation, but @Resource (JSR-250) and @Inject (JSR-330) are standardized. — Anyway if you not plan to reuse your application in a non-Spring environment, then I would not pay to many attention to this concern.

다른 팁

See Annotation based configuration in Spring, best Spring Annotation tutorial for me.

I prefer to avoid annotations, especially if they start getting confusing. Nothing wrong with good old getter and setters in this case. Just gotta wire the bean on your own, which isn't so difficult that annotations are necessary.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top