As a new graduate, I entered a web-based company and started training. As part of that, there was training using Spring Boot, so I would like to keep it as a memorandum. This is my first post on Qiita, so I hope you can see it with warm eyes. We welcome your suggestions, so please comment if you like.
DI(Dependency Injection) A type of design pattern (design concept). It seems to mean the injection of dependency. What will happen
Sample.java
Test test;
Sample(){
test = new Test();
}
This looks like below
Sample.java
Test test;
Sample(Test test){
this.test = test;
}
--There is a difference between creating an instance yourself and receiving what you create. --The instance is this test. --This has the advantage that it can be developed even if the Test class is not created. ――Honestly, it's a field that I didn't really get into the development site.
Reference: Difference between class and instance Reference: A brief description of Spring DI and AOP
When trying to implement DI
--The amount of code is likely to increase because you have to create a lot of instances at the caller. --So appeared ** DI container ** --DI container automatically injects instances
With DI container
Sample.java
Test1 test1;
Test2 test2;
Test3 test3;
Sample(Test1 test1,Test2 test2,Test3 test3){
this.test1 = test1;
this.test2 = test2;
this.test3 = test3;
}
This looks like below
Sample.java
@Autowired
Test1 test1;
@Autowired
Test2 test2;
@Autowired
Test3 test3;
When actually using it, it is necessary to annotate each class file. For details on how to use it, click here [https://qiita.com/shuntaro_tamura/items/ba5a2e9b3ba305285edd)
This time I posted it as a practice of qiita. I'm still immature because I'm a new graduate, From now on, I would like to gradually increase the output and improve the quality.
Recommended Posts