Guice 注解@Provides
来源:互联网 发布:魔兽世界编程宝典下载 编辑:程序博客网 时间:2024/05/25 21:32
package com.ilucky.guice.test10;import com.google.inject.Guice;import com.google.inject.Injector;import com.google.inject.Key;/** * v1.0:20161114 * @author IluckySi * * 注解@Provides. * 当使用@Provides方法创建对象时,该方法必须定义在Module类中,并且它必须加以@Provides注解, * 该方法的返回值类型就是被绑定的对象.当注入器需要该类型的实例时,它就会来调用该方法.注意:Provides和Provider的区别. * @Provides方法绑定方式是可以和注解绑定混合使用的. * 如果在@Provides方法上有绑定注解(@AnnotationOne),Guice以绑定注解优先. * Guice在调用@Provides方法之前会先解析该方法的依赖. */public class MainTest { public static void main(String[] args) { Injector injector = Guice.createInjector(new MyModule()); MyService myService = injector.getInstance(MyService.class); myService.service(); MyService myServiceOne = injector.getInstance(Key.get(MyService.class, AnnotationOne.class)); myServiceOne.service(); MyService myServiceTwo = injector.getInstance(Key.get(MyService.class, AnnotationTwo.class)); myServiceTwo.service(); System.out.println("-----------------@Provides的使用--------------------------"); Injector injector2 = Guice.createInjector(new MyModule2()); MyService myService2 = injector2.getInstance(MyService.class); myService2.service(); }}/**==>one==>one==>two-----------------@Provides的使用--------------------------==>xxx*/
package com.ilucky.guice.test10;import com.google.inject.Binder;import com.google.inject.Key;import com.google.inject.Module;import com.google.inject.Provides;public class MyModule implements Module{ //默认使用AnnotationOne. public void configure(Binder binder) { binder.bind(MyService.class).to(Key.get(MyService.class, AnnotationOne.class)); } @Provides @AnnotationOne public MyService one() { return new MyService("one"); } @Provides @AnnotationTwo public MyService two() { return new MyService("two"); }}
package com.ilucky.guice.test10;import com.google.inject.Binder;import com.google.inject.Module;import com.google.inject.Provides;public class MyModule2 implements Module{ public void configure(Binder binder) { } @Provides public MyService xxx() { return new MyService("xxx"); }}
package com.ilucky.guice.test10;public class MyService { private String type; public MyService(String type) { this.type = type; } public void service() { System.out.println("==>" + type); }}
package com.ilucky.guice.test10;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@BindingAnnotationpublic @interface AnnotationOne {}
package com.ilucky.guice.test10;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import com.google.inject.BindingAnnotation;@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })@Retention(RetentionPolicy.RUNTIME)@BindingAnnotationpublic @interface AnnotationTwo {}
阅读全文
0 0
- Guice 注解@Provides
- Guice注解(单例)
- Guice provider注解
- Google Guice 注入(@Inject注解)
- android 注解 guice.jar ,roboguice.jar 使用
- Google Guice 注入(@Inject注解)
- Google Guice 注入(@Inject注解)
- Guice Names的模板来生成注解
- Guice Providers注解和Names模板
- guice
- Guice
- Guice
- Guice注解实现一个类型绑定多个实现.
- RPM Provides命令
- 初试Guice
- 关于“Guice ”
- GUICE学习心得
- Guice 初识
- shell script执行过程
- linux qt在pro中依赖库方法举例:
- Go语言及Web框架Beego环境搭建
- 如何将word中的图片转变成eps文件插入latex
- redis入门1
- Guice 注解@Provides
- Wannacry之t.wnry核心库解密算法完全分析
- Oracle12C--集合(三十)
- fitsystemwindow
- 事物的四个特性
- 这学期的课程设计,用java写的词法分析器
- JVM底层又是如何实现synchronized的
- android之xml文件中特殊符号的引用
- iOS- 详解文本属性Attributes