|
| 1 | +# Guice依赖注入(接口多实现) |
| 2 | + |
| 3 | +本教程主要详细讲解Guice依赖注入中的特性接口多实现,一般使用到guice的框架的插件机制都是基于该方式实现。 |
| 4 | + |
| 5 | +#### 基础环境 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +| 技术 | 版本 | |
| 10 | +| ----- | ----- | |
| 11 | +| Java | 1.8+ | |
| 12 | +| Guice | 4.2.3 | |
| 13 | + |
| 14 | +#### 初始化项目 |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +- 初始化项目 |
| 19 | + |
| 20 | +```bash |
| 21 | +mvn archetype:generate -DgroupId=com.edurt.sli.guice -DartifactId=guice-binder-multiple -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false |
| 22 | +``` |
| 23 | + |
| 24 | +- 修改pom.xml增加Guice依赖 |
| 25 | + |
| 26 | +```xml |
| 27 | +<?xml version="1.0" encoding="UTF-8"?> |
| 28 | +<project xmlns="http://maven.apache.org/POM/4.0.0" |
| 29 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 30 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
| 31 | + |
| 32 | + <parent> |
| 33 | + <artifactId>learn-integration-guice</artifactId> |
| 34 | + <groupId>com.edurt.sli.guice</groupId> |
| 35 | + <version>1.0.0</version> |
| 36 | + </parent> |
| 37 | + |
| 38 | + <modelVersion>4.0.0</modelVersion> |
| 39 | + |
| 40 | + <artifactId>guice-binder-multiple</artifactId> |
| 41 | + <name>Guice依赖注入(接口多实现)</name> |
| 42 | + |
| 43 | + <properties> |
| 44 | + <system.java.version>1.8</system.java.version> |
| 45 | + <guice.version>4.2.3</guice.version> |
| 46 | + <lombok.version>1.18.2</lombok.version> |
| 47 | + </properties> |
| 48 | + |
| 49 | + <dependencies> |
| 50 | + <dependency> |
| 51 | + <groupId>com.google.inject</groupId> |
| 52 | + <artifactId>guice</artifactId> |
| 53 | + <version>${guice.version}</version> |
| 54 | + </dependency> |
| 55 | + <dependency> |
| 56 | + <groupId>org.projectlombok</groupId> |
| 57 | + <artifactId>lombok</artifactId> |
| 58 | + <version>${lombok.version}</version> |
| 59 | + </dependency> |
| 60 | + </dependencies> |
| 61 | + |
| 62 | + <build> |
| 63 | + <plugins> |
| 64 | + <plugin> |
| 65 | + <groupId>org.apache.maven.plugins</groupId> |
| 66 | + <artifactId>maven-compiler-plugin</artifactId> |
| 67 | + <version>${plugin.maven.compiler.version}</version> |
| 68 | + <configuration> |
| 69 | + <source>${system.java.version}</source> |
| 70 | + <target>${system.java.version}</target> |
| 71 | + </configuration> |
| 72 | + </plugin> |
| 73 | + </plugins> |
| 74 | + </build> |
| 75 | + |
| 76 | +</project> |
| 77 | +``` |
| 78 | + |
| 79 | +`guice`: guice就是我们核心要使用的依赖 |
| 80 | + |
| 81 | +#### 接口多实现注入 |
| 82 | + |
| 83 | +--- |
| 84 | + |
| 85 | +如果一个接口有多个实现,如果单单通过`@Inject`和`Module`都难以直接实现,但多实现是经常会出现的,Guice提供了其它注入方式来解决此问题。 |
| 86 | + |
| 87 | +- 创建`com.edurt.sli.guice.multiple`文件夹,并在该文件夹下创建`Service`接口文件,用于添加我们需要测试的函数 |
| 88 | + |
| 89 | +```java |
| 90 | +package com.edurt.sli.guice.multiple; |
| 91 | + |
| 92 | +public interface Service { |
| 93 | + |
| 94 | + void print(String source); |
| 95 | + |
| 96 | +} |
| 97 | +``` |
| 98 | + |
| 99 | +- 创建`Service`接口的实现类`JavaService`和`GuiceService`,用于实现接口中的方法,代码如下 |
| 100 | + |
| 101 | +```java |
| 102 | +package com.edurt.sli.guice.multiple; |
| 103 | + |
| 104 | +public class JavaService implements Service { |
| 105 | + |
| 106 | + @Override |
| 107 | + public void print(String source) { |
| 108 | + System.out.println("Java Service " + source); |
| 109 | + } |
| 110 | + |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +```java |
| 115 | +package com.edurt.sli.guice.multiple; |
| 116 | + |
| 117 | +public class GuiceService implements Service { |
| 118 | + |
| 119 | + @Override |
| 120 | + public void print(String source) { |
| 121 | + System.out.println("Guice Service " + source); |
| 122 | + } |
| 123 | + |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +- 创建`Guice`和`Java`注解类,用于提供guice框架标识 |
| 128 | + |
| 129 | +```java |
| 130 | +package com.edurt.sli.guice.multiple; |
| 131 | + |
| 132 | +import com.google.inject.BindingAnnotation; |
| 133 | + |
| 134 | +import java.lang.annotation.ElementType; |
| 135 | +import java.lang.annotation.Retention; |
| 136 | +import java.lang.annotation.RetentionPolicy; |
| 137 | +import java.lang.annotation.Target; |
| 138 | + |
| 139 | +@Retention(RetentionPolicy.RUNTIME) |
| 140 | +@Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 141 | +@BindingAnnotation |
| 142 | +public @interface Guice { |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +```java |
| 147 | +package com.edurt.sli.guice.multiple; |
| 148 | + |
| 149 | +import com.google.inject.BindingAnnotation; |
| 150 | + |
| 151 | +import java.lang.annotation.ElementType; |
| 152 | +import java.lang.annotation.Retention; |
| 153 | +import java.lang.annotation.RetentionPolicy; |
| 154 | +import java.lang.annotation.Target; |
| 155 | + |
| 156 | +@Retention(RetentionPolicy.RUNTIME) |
| 157 | +@Target({ElementType.FIELD, ElementType.PARAMETER}) |
| 158 | +@BindingAnnotation |
| 159 | +public @interface Java { |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +- 创建用于测试注入的应用类`Application`,代码如下 |
| 164 | + |
| 165 | +```java |
| 166 | +package com.edurt.sli.guice.multiple; |
| 167 | + |
| 168 | +import com.google.inject.Guice; |
| 169 | +import com.google.inject.Inject; |
| 170 | + |
| 171 | +public class Application { |
| 172 | + |
| 173 | + @Inject |
| 174 | + @Java |
| 175 | + public Service java; |
| 176 | + |
| 177 | + @Inject |
| 178 | + @com.edurt.sli.guice.multiple.Guice |
| 179 | + public Service guice; |
| 180 | + |
| 181 | + public static void main(String[] args) { |
| 182 | + Application application = Guice.createInjector(binder -> { |
| 183 | + binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); |
| 184 | + binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); |
| 185 | + }).getInstance(Application.class); |
| 186 | + application.guice.print("sss"); |
| 187 | + application.java.print("sss"); |
| 188 | + } |
| 189 | + |
| 190 | +} |
| 191 | +``` |
| 192 | + |
| 193 | +我们运行程序输出 |
| 194 | + |
| 195 | +```bash |
| 196 | +Guice Service sss |
| 197 | +Java Service sss |
| 198 | +``` |
| 199 | + |
| 200 | +我们注意看`binder`的配置中,我们将注解与实际的实现类绑定到了一起,这样就实现了绑定多接口实现的功能。 |
| 201 | + |
| 202 | +> 注意:在本次程序中我们使用的是lambda表达式进行的代码编程,需要jdk1.8及以上版本 |
| 203 | +
|
| 204 | +#### 静态代码注入 |
| 205 | + |
| 206 | +--- |
| 207 | + |
| 208 | +我们如果需要进行静态代码注入服务该怎么写呢?我们参照以前讲解的`Guice依赖注入(构造函数注入)`资源中,我们创建一个`ApplicationStatic`类进行static的注入,代码如下 |
| 209 | + |
| 210 | +```java |
| 211 | +package com.edurt.sli.guice.multiple; |
| 212 | + |
| 213 | +import com.google.inject.Inject; |
| 214 | + |
| 215 | +public class ApplicationStatic { |
| 216 | + |
| 217 | + @Inject |
| 218 | + @Java |
| 219 | + public static Service java; |
| 220 | + |
| 221 | + @Inject |
| 222 | + @com.edurt.sli.guice.multiple.Guice |
| 223 | + public static Service guice; |
| 224 | + |
| 225 | + public static void main(String[] args) { |
| 226 | + com.google.inject.Guice.createInjector(binder -> { |
| 227 | + binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); |
| 228 | + binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); |
| 229 | + binder.requestStaticInjection(ApplicationStatic.class); |
| 230 | + }); |
| 231 | + ApplicationStatic.guice.print("sss"); |
| 232 | + ApplicationStatic.java.print("sss"); |
| 233 | + } |
| 234 | + |
| 235 | +} |
| 236 | +``` |
| 237 | + |
| 238 | +我们只需要在binder阶段将我们的主类注入到guice容器中,也就是我们看到的`binder.requestStaticInjection(ApplicationStatic.class);`代码,运行程序输出以下内容 |
| 239 | + |
| 240 | +```java |
| 241 | +Guice Service sss |
| 242 | +Java Service sss |
| 243 | +``` |
| 244 | + |
| 245 | +#### 属性绑定多接口 |
| 246 | + |
| 247 | +--- |
| 248 | + |
| 249 | +先看一下多接口绑定的示例 |
| 250 | + |
| 251 | +```java |
| 252 | +package com.edurt.sli.guice.multiple; |
| 253 | + |
| 254 | +import com.google.inject.Guice; |
| 255 | +import com.google.inject.Inject; |
| 256 | + |
| 257 | +public class ApplicationMultipleProperty { |
| 258 | + |
| 259 | + @Inject |
| 260 | + public Service java; |
| 261 | + |
| 262 | + @Inject |
| 263 | + public Service guice; |
| 264 | + |
| 265 | + public static void main(String[] args) { |
| 266 | + ApplicationMultipleProperty application = Guice.createInjector(binder -> { |
| 267 | + binder.bind(Service.class).annotatedWith(Java.class).to(JavaService.class); |
| 268 | + binder.bind(Service.class).annotatedWith(com.edurt.sli.guice.multiple.Guice.class).to(GuiceService.class); |
| 269 | + }).getInstance(ApplicationMultipleProperty.class); |
| 270 | + application.guice.print("sss"); |
| 271 | + application.java.print("sss"); |
| 272 | + } |
| 273 | + |
| 274 | +} |
| 275 | +``` |
| 276 | + |
| 277 | +运行以上代码,就会出现以下错误 |
| 278 | + |
| 279 | +```java |
| 280 | +Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors: |
| 281 | + |
| 282 | +1) No implementation for com.edurt.sli.guice.multiple.Service was bound. |
| 283 | + Did you mean? |
| 284 | + * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Java |
| 285 | + * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Guice |
| 286 | + while locating com.edurt.sli.guice.multiple.Service |
| 287 | + for field at com.edurt.sli.guice.multiple.Application.guice(Application.java:6) |
| 288 | + while locating com.edurt.sli.guice.multiple.Application |
| 289 | + |
| 290 | +2) No implementation for com.edurt.sli.guice.multiple.Service was bound. |
| 291 | + Did you mean? |
| 292 | + * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Java |
| 293 | + * com.edurt.sli.guice.multiple.Service annotated with interface com.edurt.sli.guice.multiple.Guice |
| 294 | + while locating com.edurt.sli.guice.multiple.Service |
| 295 | + for field at com.edurt.sli.guice.multiple.Application.java(Application.java:6) |
| 296 | + while locating com.edurt.sli.guice.multiple.Application |
| 297 | + |
| 298 | +2 errors |
| 299 | + at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1120) |
| 300 | + at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1078) |
| 301 | + at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1131) |
| 302 | + at com.edurt.sli.guice.multiple.Application.main(Application.java:18) |
| 303 | +``` |
| 304 | + |
| 305 | +这是因为我们使用了属性绑定了多接口实现,导致guice无法识别具体是哪个实现类,不过guice是强大的这种问题也被考虑到了,只需要使用`@Named`模板生成注解即可解决,我们姜代码修改为以下内容 |
| 306 | + |
| 307 | +```java |
| 308 | +package com.edurt.sli.guice.multiple; |
| 309 | + |
| 310 | +import com.google.inject.Guice; |
| 311 | +import com.google.inject.Inject; |
| 312 | +import com.google.inject.name.Named; |
| 313 | +import com.google.inject.name.Names; |
| 314 | + |
| 315 | +public class ApplicationMultipleProperty { |
| 316 | + |
| 317 | + @Inject |
| 318 | + @Named("Java") |
| 319 | + public Service java; |
| 320 | + |
| 321 | + @Inject |
| 322 | + @Named("Guice") |
| 323 | + public Service guice; |
| 324 | + |
| 325 | + public static void main(String[] args) { |
| 326 | + ApplicationMultipleProperty application = Guice.createInjector(binder -> { |
| 327 | + binder.bind(Service.class).annotatedWith(Names.named("Java")).to(JavaService.class); |
| 328 | + binder.bind(Service.class).annotatedWith(Names.named("Guice")).to(GuiceService.class); |
| 329 | + }).getInstance(ApplicationMultipleProperty.class); |
| 330 | + application.guice.print("sss"); |
| 331 | + application.java.print("sss"); |
| 332 | + } |
| 333 | + |
| 334 | +} |
| 335 | +``` |
| 336 | + |
| 337 | +运行程序后,输出以下结果 |
| 338 | + |
| 339 | +```java |
| 340 | +Guice Service sss |
| 341 | +Java Service sss |
| 342 | +``` |
| 343 | + |
| 344 | +这个示例也很好理解,其实我们只是做了两步操作 |
| 345 | + |
| 346 | +- 在绑定实现的时候使用`annotatedWith(Names.named("Java"))`进行对该服务实现做名称标志 |
| 347 | +- 在需要使用服务实现的地方使用`@Named("Java")`进行服务的引用即可 |
| 348 | + |
| 349 | +#### 打包文件部署 |
| 350 | + |
| 351 | +--- |
| 352 | + |
| 353 | +- 打包数据 |
| 354 | + |
| 355 | +```bash |
| 356 | +mvn clean package -Dmaven.test.skip=true -X |
| 357 | +``` |
| 358 | + |
| 359 | +运行打包后的文件即可 |
| 360 | + |
| 361 | +```bash |
| 362 | +java -jar target/guice-binder-multiple-1.0.0.jar |
| 363 | +``` |
| 364 | + |
| 365 | +#### 源码地址 |
| 366 | + |
| 367 | +--- |
| 368 | +- [GitHub](https://github.com/EdurtIO/spring-learn-integration/tree/master/guice/binder-multiple) |
| 369 | +- [Gitee](https://gitee.com/qianmoQ/spring-learn-integration/tree/master/guice/binder-multiple) |
0 commit comments