2.2. FQCN Request Mode
In order to compensate for the type-safety problem in the pure string request pattern, the full class name (FQCN) request pattern emerges. The idea is that dependencies are not passed through the identifier of the string, but by the full class name of the requested dependency, when the object is requested by the container. This will prompt the "class does not exist" at compile time if the developer mistakenly writes the full class name identifier incorrectly. And, if you use IDE development tools such as Eclipse, it's easy to define the full class name identifier of the dependency into your code with the functionality of the automatic full code it provides.
In the first chapter, "Introduction to the 3.3 Dependency Injection framework", we mentioned that the Google Guice framework is a dependency injection framework that addresses type safety issues, and we look at an example of the injection point defined in a guice.
Public class depositor { Private Bank Bank; ...... @Inject//Bank Setter Injection Point Public void Setbank (Bank bank) { this. Bank = Bank; } ...... Public Cash Withdraw (BigDecimal amount) { return Bank.withdraw (Depositbook, amount); } } |
|
In the Guice framework, if you define the injection point in terms of annotations, we can use @inject. When the Guice framework resolves this annotation, the implementation class of the Bank class is automatically set to the injection point. That is, if there is an implementation class in the container that has only one bank class, Guice will instantiate it and distribute it to the dependent person.
The implementation class of bank Public class BANKICBC implements Bank {//...} |
|
But if there are multiple implementations of the Bank class in the container, such as a BANKCMB implementation class, then the Guice framework does not correctly identify which implementation's dependent object should be provided to the dependent, which is a flaw in the full class name request pattern. That is, it will limit the interface of the dependent object to only one implementation, and the solution to this problem will be described later in the "Mixed request Mode".
In the first chapter of the "principle of 3.1 Dependency Injection" we mentioned that annotations are not the only way to declare injection points, and that if you use API to declare injection points, spring, Seam, and Guice have their own APIs that can apply this full-name form of dependency injection. For example:
Spring's full-class name injection API Beanfactory injector = new filesystemapplicationcontext ("Depositconfiguration.xml") this. Bank = (Bank) Injector.getbean (bank). Class); Seam's full-class name injection API this. Bank = (Bank) Component. getinstance (BANKICBC. class); Guice's full-class name injection API Injector Injector = Guice. Createinjector (); this. Bank = (Bank) injector.getinstance (bank). Class); |
|
Dependency Injection and AOP Brief (vii)--FQCN request mode