using @Autowired many times

2020-03-23 17:52发布

问题:

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

回答1:

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.



回答2:

The answers and some comments already gave an answer about your memory concerns. About your other question

I have about 10 different controllers and in majority of them I @Autowire same services, So my question is this ok or not?

From the design perspective it sounds very bad. Ali Deghani mentioned a Single Responsibility principle. In fact, if you were to move your service from autowiring as fields to autowiring via constructor it would immediately hint if you should consider refactor, amongst other benefits



回答3:

I have about 10 different controllers and in majority of them I @Autowire same services, So my question is this ok or not?

It's ok to re-use services between controllers. That said, I would hesitate to use more than a few services in each controller and refactor the code so that the controller does not grow "too fat". Generally, I strive to keep my controllers as a mapping layer between the HTTP world and the Java world and push down all business logic down to the service layer.

Spring will create beans with singelton scope by default, meaning that if you autowire the same bean in multiple controllers they will share the same bean instance.

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?

Autowiring in itself does not require much memory, it is just a reference to a Java object instance. Typically, Spring beans do not contain any state (they delegate that to components such as caches and databases) so unless you do something exceptionally you do not have to worry about memory usage.

One thing to look out for is if is that you should avoid creating circular dependencies between beans. Since you are using field injection, Spring will throw an exception during application initialization and you need to refactor your application.