Hi guys. In this lesson we will talk about siblings and C prank basic components. sibling core is a framework for dependency injection, which is a pattern that allows us to build very decoupled systems. Okay, we got it. But what is dependency injection? dependency injection is a design pattern that removes the dependency from the programming code.
So it can be easy to manage and test application. dependency injections makes our programming code loosely coupled. To understand it, let's see it on an example. First of all, we will see the example for the doubt using dependency injection laser. We will see the example for with using dependency injection for example, We have a class like user service. In user service, we want to call user repository.
And we want to store user to database with save users method. To do it, we can easily create user repository instance. And we can directly call it from save user method. Okay, this solves our problem for now. But later due to the reason, we need to change user repository with different repository, for example, we should change it with admin repository. To do it, we should change user service class for every change request.
So the problem is user service is closely coupled with user repository or admin personally, so how can we avoid it? Of course we can avoid from it with using dependency injection. And we can say that with using the dependency inversion principle, the dependency inversion principle is a solid principle. The penance inversion principles taste that high level modules should not depend on low level modules. Both should depend on abstractions. Also, abstractions should not depend on details.
Details should depend on abstractions. Okay, let's see them on our example. First of all, user repository class should depend on abstract class. To do it, we can create interface for user repository. Then user repository can be implemented with user repository, implement Patient class, then user repository implementation class can depend on user repository. So we do it like this, we can maintain dependency inversion principle to dependency injection, we can inject it with two ways.
First one is we can inject it with constructors. Second one is we can inject it with using setter. After all, we can call it anywhere with injections. And if repository is chained without modification, we can call user service methods easily. World constructor and setter dependency injection, reduce coupling. Okay, now we will talk about how sibling handle dependency injection in siblings dependency injection can be handled with three ways.
Dependency injection in Splunk can be done through constructors, setters, or fields. Let's see them for our example. For constructor example, we can create beans for user service and user repository. And later, we can directly call from user service construct. Now, we so other think about spring, what is b? Just think simple.
Beans are objects or instances of spring. So we have just create user a poster object here with using beam. Later, we can call these objects from sibling components throughout application scope. We should also notice something here. What is configuration, the configuration annotation indicates that the class is a source of bean definitions. So, this class is a spring configure class to describe it, we have just marked with configuration annotation.
Second dependency injection method in sibling is setter based injection. So for our example, we can set it with using setter methods. The last dependency injection method is fields based dependency injection to handle it, auto wired annotation will be used. Actually, we will generally use this annotation in our project. But we should not forget something for these dependency injection methods. These methods can be only used m one c brain components okay now we will talk about supreme components.
First one is component annotation. This is general purpose annotation indicating that the class is a separate component. Second one is repost or annotation. This is to indicate that the class defines a data repository. Third one is service annotation, service beans hold the business logic and call methods in the repo storage. The last one is controllers or rest controller annotation.
These indicate that a particular class serves the role of a controller. So you can call service methods here and you can serve it to view later Now, we will talk about the other basic annotations. First one is Spring Boot application annotation. These means to mark Class S main class. So this class will be initializer class for separate both. Second one is transactional notation.
So this provides transaction consistent methods. If you get an any exception in a method, all DB chains will be roll back. Okay, that's all. Thank you.