A
A
Albert2020-04-27 14:09:46
Java
Albert, 2020-04-27 14:09:46

How to inject a repository in another class?

I give up, I do not know what request to formulate in the search engine.
The bottom line is, I'm playing with writing a bot for a telegram, but there was one problem when debugging, since it takes a long time to constantly upload to a server and test the written code with hooks.
I call the methods one by one and check the functionality, but I noticed that the repository is not injected, more precisely, when debugging, its value is Null,
5ea6bc9eec447422636789.png

although the method call in the source class is injected without problems:
5ea6bd2a3b840302223421.png

Who can tell me what features of interaction with this tool I don’t understand?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2020-04-27
@6ETuK

TelegramController ts = new TelegramController();

but I noticed that the repository is not injected, more precisely, when debugging, its value is Null

Of course, they are not injected, but you don’t add them to the constructor of the object that you create with your hands !
Autowiring is a DI container tool, meaning if a controller is created from a container, it will get all the dependencies. In Spring, everything revolves around the dependency container, in which case you create a service, specify dependencies, and the container already collects your service.
But in this case, you created the controller with your hands, which means that everything depends on the handles and provide the object. BUT! Using controllers as services from within other controllers (and any other layers of the application, not the framework) is not correct and very confusing, for all the time I saw only one such case for hot swapping at the time of the move.
If you want to reuse logic:
The logic should not be in another controller, but in some service (object) that you would call from two (or more) controllers. Then everything would be easy and UNDERSTOOD to work and be injected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question