} @Override public String toString() { return "Engine{" + "name='" + name + '\'' + '}'; } public void run() { System.out.println("The engine is turning~~~"); }
}
Then we need one Module Class to generate dependent objects. Previously introduced@Module Is used to standard this class, and@Provide It is used to mark the specific method of providing dependent objects (there is an unwritten provision here, which is@Provide The labeling method is named by provide At first, this is not mandatory, but it is beneficial to improve the readability of the code). ```java @Module public class MarkCarModule { public MarkCarModule(){ } /** * It is used to label the method in the class marked by the Module. This method is called when it is necessary to provide a dependency, so that the pre provided object is assigned a value to the variable marked @ Inject as a dependency * @return */ @Provides Engine provideEngine(){ return new Engine("gear"); } }
Next, we need to make a little modification to the CarComponent. The @ Component annotation before did not take parameters. Now we need to add modules = {MarkCarModule.class} to tell Dagger2 that the class of MarkCarModule is the dependency.
@Component(modules = MarkCarModule.class) public interface CarComponent { void inject(Car car); }
We also need to modify the constructor of the Car class. There are more markCarModule(new MarkCarModule()) methods than before, which is equivalent to telling the injector DaggerCarComponent to inject the dependencies provided by MarkCarModule into the Car class.
public class Car { /** * We mentioned that both @ Inject and @ Module can provide dependencies. If we provide dependencies through the tag @ Inject on the constructor, how will Dagger2 choose? The specific rules are as follows: * * Step 1: first find out whether there are dependent methods in the class marked by @ Module. * Step 2: if there is a method that provides dependencies, check whether the method has parameters. * a: If there are parameters, initialize each parameter successively from step 1; * b: If it does not exist, directly initialize the class instance to complete a dependency injection. * * * Step 3: if there is no dependent method, find the constructor marked @ Inject to see if there are parameters in the constructor. * a: If there are parameters, initialize each parameter successively from step 1 * b: If it does not exist, directly initialize the class instance to complete a dependency injection */ @Inject Engine engine; public Car() { DaggerCarComponent.builder().markCarModule(new MarkCarModule()) .build().inject(this); } public Engine getEngine() { return this.engine; } public static void main(String ... args){ //TODO: Car car = new Car(); System.out.println(car.getEngine()); } }
Such a basic dependency injection is completed. Next, let's test our code.
output
Engine{name='gear'}
3. Case C
So what if a Car has two engines (that is, there are two Engine variables in the Car class)? It doesn't matter. We also have @ Qulifier! First, we need to use Qulifier to define two annotations:
public class Engine { /** * It is used for custom annotations, that is, @ Qulifier is used to mark annotation classes just like several basic meta annotations provided by Java. When we use @ Module to label the methods that provide dependencies, The method name can be defined arbitrarily (although we define that the method name generally starts with provide, this is not mandatory, but just to increase readability). So how does dagger2 know who the method provides dependencies for? The answer is the type of return value. Dagger2 determines which variable marked with @ Inject is assigned according to the type of return value. But the problem comes, once there are multiple same variables The return type dagger2 is confused@ In order to solve this problem, we use @ Qulifier to define our own annotations, Then, the dependent methods and dependent demanders (that is, the variables marked by @ Inject) are marked through user-defined annotations, so that dagger2 knows who to provide dependencies for. - --- a more concise definition: when the type is not enough to identify a dependency, we can use this annotation * 1. Use @ Qulifier to define two annotations */ @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface QualifierA { } @Qualifier @Retention(RetentionPolicy.RUNTIME) public @interface QualifierB { } private String name; Engine(String name) { this.name = name; } @Override public String toString() { return "Engine{" + "name='" + name + '\'' + '}'; } public void run() { System.out.println("The engine is turning~~~"); } }
At the same time, we need to make changes to the dependent providers
@Module public class MarkCarModule { public MarkCarModule(){ } /** * 2. At the same time, we need to make changes to the dependent providers * @return */ @Engine.QualifierA @Provides Engine provideEngineA(){ return new Engine("gearA"); } @Engine.QualifierB @Provides Engine provideEngineB(){ return new Engine("gearB"); } }
Next, the Car class that depends on the demand side also needs to be modified
public class Car { /** * 3. Next, the Car class that depends on the demand side also needs to be modified */ @Engine.QualifierA @Inject Engine engineA; @Engine.QualifierB @Inject Engine engineB; public Car() { DaggerCarComponent.builder().markCarModule(new MarkCarModule()) .build().inject(this); } public Engine getEngineA() { return this.engineA; } public Engine getEngineB() { return this.engineB; } public static void main(String... args) { //TODO: Car car = new Car(); System.out.println(car.getEngineA()); System.out.println(car.getEngineB()); } }
Execution results:
Engine{name='gearA'} Engine{name='gearB'}
4. Case D
Next, let's see how @ Scope limits the Scope and implements local singletons.
First, we need to define a CarScope annotation through @ Scope:
public class Engine { /** * For user-defined annotation, I can define the annotation Scope through @ Scope user-defined annotation to realize local singleton * 1. @Scope Define a CarScope annotation */ @Scope @Retention(RetentionPolicy.RUNTIME) public @interface CarScope { } private String name; Engine(String name) { this.name = name; System.out.println("Engine create: " + name); } @Override public String toString() { return "Engine{" + "name='" + name + '\'' + '}'; } public void run() { System.out.println("The engine is turning~~~"); } }
Next, we need to mark the dependent provider MarkCarModule with this @ CarScope.
@Module public class MarkCarModule { public MarkCarModule(){ } /** * 2. @CarScope Unmark dependent provider MarkCarModule * @return */ @Engine.CarScope @Provides Engine provideEngine(){ return new Engine("gear"); } }
You also need to use @ Scope to label the injector component
last
Xiaobian shares some of my usual learning materials here. Due to space constraints, the detailed information of pdf documents is too comprehensive, and there are too many details, so I only give a rough introduction to the screenshots of some knowledge points, and there are more detailed contents in each small node! If you need a program, you can follow + like it, Click here for free
Programmer code interview guide IT famous enterprise algorithm and data structure problem optimal solution
This is the "programmer interview book! The book summarizes the optimal solutions of various questions in the code interview of IT famous enterprises, and provides the implementation of relevant codes. In view of the lack of authoritative topic summary in the current programmer interview, this book selects nearly 200 classic code interview questions that have actually appeared, so as to help the "big programmers" make sure of their interview preparation. "After brushing this book, you are the" question king "!
TCP-IP protocol group (version 4)
This book is the latest version of the classic book on TCP/IP protocol family. This book has been popular with readers since its first edition was published.
The latest edition of this book "carries out" to protect the elements, taking into account the latest development of computer network technology in the physical environment. The book has seven parts, 30 drafts and seven appendices: the first part introduces some basic concepts and basic underlying technologies; the second part introduces network layer protocols; the third part introduces transport layer protocols; The fourth part introduces the application layer protocol: the fifth part introduces the next generation protocol, namely IPv6 protocol; the sixth part introduces the network security issues; the seventh part gives seven appendices.
Java development manual (Songshan version)
Needless to say, I read Ali's development manual every time it is updated. This is the latest * * (Songshan version) in early August**
MySQL 8 from entry to mastery
The main contents of this book include MySQL installation and configuration, database creation, data table creation, data types and operators, MySQL functions, querying data, data table operations (inserting, updating and deleting data), indexes, stored procedures and functions, views, triggers, user management, data backup and restore, MySQL logs, performance optimization, MySQL repl application MySQL Workbench, MySQL Utilities, MySQL Proxy, PHP operation MySQL database and PDO database abstract class library, etc. Finally, through the database design of three comprehensive cases, this paper describes the application of MySQL in practical work.
Spring 5 Advanced Programming (version 5)
This book covers all the contents of Spring 5. If you want to make full use of the powerful functions of this leading enterprise Java application development framework, this book is the most comprehensive Spring reference and practical guide.
The fifth edition of this book covers the core Spring and its integration with other leading Java technologies such as Hibemate JPA 2.Tls, Thymeleaf and websocket. This book focuses on how to use java to configure classes, lambda expressions, Spring Boot and reactive programming. At the same time, some insights and practical experience will be shared with enterprise application developers, including remoting, transaction, Web and presentation layers, and so on.
Java core knowledge points + 1000 Internet Java Engineer Interview Questions
The way of enterprise IT architecture transformation Alibaba's China Taiwan strategic thought and Architecture Practice
This book describes the technological development history of Alibaba, as well as the practice and development history of the Internet technology architecture.
People share some insights and practical experience, including remoting, transaction, Web and presentation layers, and so on.
[external chain picture transferring... (img-ZNzgptmr-1628072376534)]
Java core knowledge points + 1000 Internet Java Engineer Interview Questions
[external chain picture transferring... (img-kxBX3JXN-1628072376535)]
[external chain picture transferring... (img-L7QFF1B6-1628072376536)]
The way of enterprise IT architecture transformation Alibaba's China Taiwan strategic thought and Architecture Practice
This book describes the technological development history of Alibaba, as well as the practice and development history of the Internet technology architecture.