Using @Autowired many times

I am very new to spring, so I can ask a stupid question, but anyway ...

I created a Spring MVC 4.0 application.

my settings:

Controller β†’ Service β†’ DAO

at the controller level, I use 4 to 5 different @Autowired variables, such as

 @Autowired private ClientService clientService; @Autowired private CommentService commentService; @Autowired private SearchService searchService; 

At the service level, I Autowire also several DAO

 @Autowired SearchDAO searchDAO; @Autowired private ActivityDAO activityDAO; @Autowired private UserService userService; 

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

Is it possible to use @Autowire as many times as I need, or does it bring too much memory? Will this have some other effects for my application?

I am using Spring 4.0 + hibernate JPA

+5
source share
4 answers

No problem with @Autowired .

Autowired finds the bean context in Spring and assigns it to a variable. It simply refers to the same service object / Dao bean. It will not create duplicates.

But having so many objects introduced into one class is a sign that one class is doing a lot. Check if you can refactor a class into several classes, where possible.

+4
source

Answers and some comments have already answered your memory. About your other question

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

From a design point of view, this sounds very bad. Ali Degani mentioned the principle of shared responsibility. In fact, if you should transfer your service from auto-link as fields to auto-load through the constructor, this will immediately tell you whether to consider refactoring, among other advantages

+1
source

@Autowired will create a single instance of a specific service in your application, so I assume that having multiple auto levels of the same service in different controllers should not be a memory problem, unless, of course, the volume of the bean is not "prototype", in this case spring will create several copies.

0
source

I have about 10 different controllers, and in most of them I @Autowire have the same services. So is my question or not?

It’s good to repeat the use of services between controllers. However, I would be embarrassed to use more than a few services in each controller and reorganize the code so that the controller does not become "too thick". As a rule, I try to keep my controllers as a mapping layer between the HTTP world and the Java world and drop all business logic to the service level.

Spring will create beans with a singelton scope by default, which means that if you auto-detect the same bean in multiple controllers, they will have the same bean instance.

Is it possible to use @Autowire as many times as I need, or will there be too much memory usage? Will this have some other effects for my application?

Autoinstallation by itself does not require a lot of memory, it's just a reference to an instance of a Java object. As a rule, Spring beans do not contain any state (they delegate this to components such as caches and databases), so if you do nothing, you do not need to worry about memory usage.

One thing you need to pay attention to is that you should avoid creating circular dependencies between beans. Since you are using field injection, Spring throws an exception during application initialization and you need to reorganize your application.

0
source

Source: https://habr.com/ru/post/1244450/


All Articles