What is the most appropriate way of injecting daos in services, services in controllers in Spring?
There are many annot开发者_如何转开发ations 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.
精彩评论