Guice 3.0 学习笔记一

测试代码入下:

package com.test.maven.guice.client.binding;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

import javax.inject.Named;

import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.BindingAnnotation;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.name.Names;

/**
 * guice 3.0 学习笔记一
 */
public class TestInject {

	@Test 
	/**
	 * 接口绑定
	 */
	public void testBindingInterface1(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
				bind(Person.class).to(YellowPerson.class);
			}
		});
		
		Person p1 = inj.getInstance(Person.class);
		System.out.println(p1.speak());
	}
	
	@Test 
	/**
	 * 绑定字段属性
	 */
	public void testInjectField1(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
				bind(Person.class).to(BlackPerson.class);
				bind(Person.class).annotatedWith(Names.named("b")).to(BlackPerson.class);
				bind(Person.class).annotatedWith(W.class).to(WhitePerson.class);
			}
		});
		
		PersonClient client = inj.getInstance(PersonClient.class);
		System.out.println(client.black.speak());
		System.out.println(client.white.speak());
		System.out.println(client.yellow.speak());
	}
	
	@Test 
	/**
	 * 绑定方法
	 */
	public void testInjectMethod(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
				bind(Person.class).to(YellowPerson.class);
				bind(Person.class).annotatedWith(Names.named("b")).to(BlackPerson.class);
				bind(Person.class).annotatedWith(W.class).to(WhitePerson.class);
			}
		});
		
		PersonClientMethod client = inj.getInstance(PersonClientMethod.class);
		System.out.println(client.black.speak());
		System.out.println(client.white.speak());
		System.out.println(client.yellow.speak());
	}
	
	@Test 
	/**
	 * 与 testInjectMethod 效果相同,
	 */
	public void testInjectMethod2(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
			}
			
			@Provides
			public Person getYellowPerson(){
				// 可添加其他的操作
				return new YellowPerson();
			}

			@Provides
			@Named(value="b")
			public Person getBlackPerson(){
				// 可添加其他的操作
				return new BlackPerson();
			}
			
			@Provides
			@W 
			public Person getWhitePerson(){
				// 可添加其他的操作
				return new WhitePerson();
			}
		});
		
		PersonClientMethod client = inj.getInstance(PersonClientMethod.class);
		System.out.println(client.black.speak());
		System.out.println(client.white.speak());
		System.out.println(client.yellow.speak());
	}
	
	@Test 
	public void testToProvider(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
				bind(Person.class).toProvider(new Provider<Person>(){

					@Inject @B Person black;
					
					@Override
					public Person get() {
						return new Person(){

							@Override
							public String speak() {
								return "那美克星人 和 " + black.speak();
							}
						};
					}
					
				});
				
				bind(Person.class).annotatedWith(B.class).to(BlackPerson.class);
			}
			
		});
		
		Provider<Person> p1 = inj.getProvider(Person.class);
		System.out.println(p1.get().speak());
	}
	
	@Test 
	/**
	 * 绑定实例, 构造方法注入将失效
	 */
	public void testToInstance(){
		Injector inj = Guice.createInjector(new AbstractModule() {
			@Override
			protected void configure() {
				bind(Person.class).toInstance(new BlackPerson());
				bind(Person.class).annotatedWith(Names.named("o")).toInstance(new Person(){
					@Override
					public String speak() {
						return "火星人";
					}
				});
			}
		});
		
		Person p1 = inj.getInstance(Person.class);
		Person p2 = inj.getInstance(Key.get(Person.class, Names.named("o")));
		
		System.out.println(p1.speak());
		System.out.println(p2.speak());
	}
	
	public static interface Person{
		public String speak();
	}
	
	public static class YellowPerson implements Person{
		@Override
		public String speak() {
			return "Yellow";
		}
	}
	
	public static class BlackPerson implements Person{
		@Override
		public String speak() {
			return "Black";
		}
	}
	
	public static class WhitePerson implements Person{
		@Override
		public String speak() {
			return "white";
		}
	}
	
	@Retention(RetentionPolicy.RUNTIME)
	@BindingAnnotation
	static @interface  Y{}
	
	@Retention(RetentionPolicy.RUNTIME)
	@BindingAnnotation
	static @interface  B{}
	
	@Retention(RetentionPolicy.RUNTIME)
	@BindingAnnotation
	static @interface  W{}
	

	public static class PersonClient{
		@Inject Person yellow;
		@Inject @Named(value="b")Person black;
		@Inject @W  Person white;
	}
	
	public static class PersonClientMethod{
		Person yellow;
		Person black;
		Person white;
		
		@Inject
		public void speak(Person y, @Named(value="b") Person b, @W  Person w){
			yellow = y;
			black = b;
			white = w;
		}
	}
}

你可能感兴趣的:(Guice 3.0 学习笔记一)