Guice 注解@Provides

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)
@BindingAnnotation
public @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)
@BindingAnnotation
public @interface AnnotationTwo {
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值