共计 7374 个字符,预计需要花费 19 分钟才能阅读完成。
本教程次要具体解说 Guice 依赖注入中的个性接口多实现,个别应用到 guice 的框架的插件机制都是基于该形式实现。
根底环境
技术 | 版本 |
---|---|
Java | 1.8+ |
Guice | 4.2.3 |
初始化我的项目
- 初始化我的项目
mvn archetype:generate -DgroupId=com.edurt.sli.guice -DartifactId=guice-binder-multiple -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
- 批改 pom.xml 减少 Guice 依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent> <artifactId>learn-integration-guice</artifactId> <groupId>com.edurt.sli.guice</groupId> <version>1.0.0</version> </parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>guice-binder-multiple</artifactId> <name>Guice 依赖注入(接口多实现)</name>
<properties> <system.java.version>1.8</system.java.version> <guice.version>4.2.3</guice.version> <lombok.version>1.18.2</lombok.version> </properties>
<dependencies> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>${guice.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> </dependencies>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${plugin.maven.compiler.version}</version> <configuration> <source>${system.java.version}</source> <target>${system.java.version}</target> </configuration> </plugin> </plugins> </build>
</project>
guice
: guice 就是咱们外围要应用的依赖
接口多实现注入
如果一个接口有多个实现,如果单单通过 @Inject
和Module
都难以间接实现,但多实现是常常会呈现的,Guice 提供了其它注入形式来解决此问题。
- 创立
com.edurt.sli.guice.multiple
文件夹,并在该文件夹下创立Service
接口文件,用于增加咱们须要测试的函数
package com.edurt.sli.guice.multiple;
public interface Service {void print(String source);
}
- 创立
Service
接口的实现类JavaService
和GuiceService
,用于实现接口中的办法,代码如下
package com.edurt.sli.guice.multiple;
public class JavaService implements Service {
@Override
public void print(String source) {System.out.println("Java Service" + source); } }
package com.edurt.sli.guice.multiple;
public class GuiceService implements Service {
@Override
public void print(String source) {System.out.println("Guice Service" + source); } }
- 创立
Guice
和Java
注解类,用于提供 guice 框架标识
package com.edurt.sli.guice.multiple;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface Guice {}
package com.edurt.sli.guice.multiple;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER})
@BindingAnnotation
public @interface Java {}
- 创立用于测试注入的利用类
Application
,代码如下
package com.edurt.sli.guice.multiple;
import com.google.inject.Guice;
import com.google.inject.Inject;
public class Application {
@Inject @Java public Service java;
@Inject @com.edurt.sli.guice.multiple.Guice public Service guice;
public static void main(String[] args) {Application application = Guice.createInjector(binder -> { binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); }).getInstance(Application.class); application.guice.print("sss"); application.java.print("sss"); }
}
咱们运行程序输入
Guice Service sss
Java Service sss
咱们留神看 binder
的配置中,咱们将注解与理论的实现类绑定到了一起,这样就实现了绑定多接口实现的性能。
留神:在本次程序中咱们应用的是 lambda 表达式进行的代码编程,须要 jdk1.8 及以上版本
动态代码注入
咱们如果须要进行动态代码注入服务该怎么写呢?咱们参照以前解说的 Guice 依赖注入(构造函数注入)
资源中,咱们创立一个 ApplicationStatic
类进行 static 的注入,代码如下
package com.edurt.sli.guice.multiple;
import com.google.inject.Inject;
public class ApplicationStatic {
@Inject @Java public static Service java;
@Inject @com.edurt.sli.guice.multiple.Guice public static Service guice;
public static void main(String[] args) {com.google.inject.Guice.createInjector(binder -> { binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); binder.requestStaticInjection(ApplicationStatic.class); }); ApplicationStatic.guice.print("sss"); ApplicationStatic.java.print("sss"); }
}
咱们只须要在 binder 阶段将咱们的主类注入到 guice 容器中,也就是咱们看到的 binder.requestStaticInjection(ApplicationStatic.class);
代码,运行程序输入以下内容
Guice Service sss
Java Service sss
属性绑定多接口
先看一下多接口绑定的示例
package com.edurt.sli.guice.multiple;
import com.google.inject.Guice;
import com.google.inject.Inject;
public class ApplicationMultipleProperty {
@Inject public Service java;
@Inject public Service guice;
public static void main(String[] args) {ApplicationMultipleProperty application = Guice.createInjector(binder -> { binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); }).getInstance(ApplicationMultipleProperty.class); application.guice.print("sss"); application.java.print("sss"); }
}
运行以上代码,就会呈现以下谬误
Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:
1) No implementation for com.edurt.sli.guice.multiple.Service was bound.
Did you mean? * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Java * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Guice while locating com.edurt.sli.guice.multiple.Service for field at com.edurt.sli.guice.multiple.Application.guice(Application.java:6) while locating com.edurt.sli.guice.multiple.Application
2) No implementation for com.edurt.sli.guice.multiple.Service was bound.
Did you mean? * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Java * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Guice while locating com.edurt.sli.guice.multiple.Service for field at com.edurt.sli.guice.multiple.Application.java(Application.java:6) while locating com.edurt.sli.guice.multiple.Application
2 errors
at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1120) at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1078) at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1131) at com.edurt.sli.guice.multiple.Application.main(Application.java:18)
这是因为咱们应用了属性绑定了多接口实现,导致 guice 无奈辨认具体是哪个实现类,不过 guice 是弱小的这种问题也被思考到了,只须要应用 @Named
模板生成注解即可解决,咱们姜代码批改为以下内容
package com.edurt.sli.guice.multiple;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import com.google.inject.name.Names;
public class ApplicationMultipleProperty {@Inject @Named("Java") public Service java;
@Inject @Named("Guice") public Service guice;
public static void main(String[] args) {ApplicationMultipleProperty application = Guice.createInjector(binder -> { binder.bind(Service.class).annotatedWith(Names.named("Java")).to(JavaService.class); binder.bind(Service.class).annotatedWith(Names.named("Guice")).to(GuiceService.class); }).getInstance(ApplicationMultipleProperty.class); application.guice.print("sss"); application.java.print("sss"); }
}
运行程序后,输入以下后果
Guice Service sss
Java Service sss
这个示例也很好了解,其实咱们只是做了两步操作
- 在绑定实现的时候应用
annotatedWith(Names.named("Java"))
进行对该服务实现做名称标记 - 在须要应用服务实现的中央应用
@Named("Java")
进行服务的援用即可
打包文件部署
- 打包数据
mvn clean package -Dmaven.test.skip=true -X
运行打包后的文件即可
java -jar target/guice-binder-multiple-1.0.0.jar
源码地址
- GitHub
- Gitee
正文完