多次使用@Autowired [英] using @Autowired many times
问题描述
我已经构建了Spring MVC 4.0应用程序。
我的设置如下所示:
控制器>>服务>> DAO
在控制器级别,我使用了大约4到5个不同的 @Autowired
像这样的变量
@Autowired
私人ClientService clientService;
@Autowired
private CommentService commentService;
@Autowired
私人SearchService searchService;
在服务等级I Autowire中还有几个DAO
@Autowired
SearchDAO searchDAO;
@Autowired
私人ActivityDAO activityDAO;
@Autowired
private UserService userService;
我有大约10个不同的控制器,其中大多数控制器都是I @Autowire
相同的服务,所以我的问题是这个好吗?
可以使用 @Autowire
多少次我需要或会带来太多的内存使用量?它会对我的应用程序有一些其他影响吗?
我使用Spring 4.0 + hibernate JPA
Autowired在Spring上下文中查找bean并分配给变量。它只是引用Service / Dao bean的同一个对象。它不会造成重复。
但是有太多的对象注入一个类是一个类做了很多事情的标志。尽可能检查将类重构为多个类的可能性。
I'm very new to spring, so I might ask silly question but anyway...
I have built Spring MVC 4.0 application.
my settings are like this:
Controller >> Service >> DAO
in controller level I use about 4 to 5 different @Autowired
variables like this
@Autowired
private ClientService clientService;
@Autowired
private CommentService commentService;
@Autowired
private SearchService searchService;
In Service level I Autowire also several DAOs
@Autowired
SearchDAO searchDAO;
@Autowired
private ActivityDAO activityDAO;
@Autowired
private UserService userService;
I have about 10 different controllers and in majority of them I @Autowire
same services, So my question is this ok or not?
Is is ok to use @Autowire
as many times as I need or will bring too much memory usage? Will it have some other effects on my application?
I use Spring 4.0 + hibernate JPA
There is no problem with @Autowired
.
Autowired finds the bean in Spring context and assign to the variable. It is just referencing to the same object of Service/Dao bean. It will not create duplicate.
But having so many objects injected to one class is a sign of one class doing a lot. Check possibility of refactoring the class into multiple classes wherever possible.
这篇关于多次使用@Autowired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!