Google Guice Annotation Binding-5

来源:互联网 发布:单片机射频通讯 编辑:程序博客网 时间:2024/06/07 19:00
  Google Guice提供Annotation Binding,可以使用注解来对依赖进行绑定并同时进行赋值。

 

    一。@DriverClassName

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.BindingAnnotation;  
  4.   
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7.   
  8. import static java.lang.annotation.ElementType.*;  
  9. import static java.lang.annotation.RetentionPolicy.RUNTIME;  
  10.   
  11. /** 
  12.  * Created by IntelliJ IDEA. 
  13.  * User: Zhong Gang 
  14.  * Date: 11-8-6 
  15.  * Time: 下午3:23 
  16.  */  
  17. @BindingAnnotation  
  18. @Target({ METHOD, CONSTRUCTOR, FIELD })  
  19. @Retention(RUNTIME)  
  20. public @interface DriverClassName {  
  21. }  

 

    二。@Username

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.BindingAnnotation;  
  4.   
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7.   
  8. import static java.lang.annotation.ElementType.CONSTRUCTOR;  
  9. import static java.lang.annotation.ElementType.FIELD;  
  10. import static java.lang.annotation.ElementType.METHOD;  
  11. import static java.lang.annotation.RetentionPolicy.RUNTIME;  
  12.   
  13. /** 
  14.  * Created by IntelliJ IDEA. 
  15.  * User: Zhong Gang 
  16.  * Date: 11-8-6 
  17.  * Time: 下午3:23 
  18.  */  
  19. @BindingAnnotation  
  20. @Target({ METHOD, CONSTRUCTOR, FIELD })  
  21. @Retention(RUNTIME)  
  22. public @interface Username {  
  23. }  

 

    三。@Url

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.BindingAnnotation;  
  4.   
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7.   
  8. import static java.lang.annotation.ElementType.*;  
  9. import static java.lang.annotation.RetentionPolicy.RUNTIME;  
  10.   
  11. /** 
  12.  * Created by IntelliJ IDEA. 
  13.  * User: Zhong Gang 
  14.  * Date: 11-8-6 
  15.  * Time: 下午3:23 
  16.  */  
  17. @BindingAnnotation  
  18. @Target({ METHOD, CONSTRUCTOR, FIELD })  
  19. @Retention(RUNTIME)  
  20. public @interface Url {  
  21. }  

 

    四。@Password

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.BindingAnnotation;  
  4.   
  5. import java.lang.annotation.Retention;  
  6. import java.lang.annotation.Target;  
  7.   
  8. import static java.lang.annotation.ElementType.*;  
  9. import static java.lang.annotation.RetentionPolicy.RUNTIME;  
  10.   
  11. /** 
  12.  * Created by IntelliJ IDEA. 
  13.  * User: Zhong Gang 
  14.  * Date: 11-8-6 
  15.  * Time: 下午3:23 
  16.  */  
  17. @BindingAnnotation  
  18. @Target({ METHOD, CONSTRUCTOR, FIELD })  
  19. @Retention(RUNTIME)  
  20. public @interface Password {  
  21. }  

 

    五。DBConnection

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.Inject;  
  4. import com.google.inject.Singleton;  
  5.   
  6. /** 
  7.  * Created by IntelliJ IDEA. 
  8.  * User: Zhong Gang 
  9.  * Date: 11-8-6 
  10.  * Time: 下午3:28 
  11.  */  
  12. @Singleton  
  13. public class Connection {  
  14.   
  15.     @Inject  
  16.     @DriverClassName  
  17.     private String driverClassName;  
  18.   
  19.     @Inject  
  20.     @Url  
  21.     private String url;  
  22.   
  23.     @Inject  
  24.     @Username  
  25.     private String username;  
  26.   
  27.     @Inject  
  28.     @Password  
  29.     private String password;  
  30.   
  31.     public String driverClassName() {  
  32.         return driverClassName;  
  33.     }  
  34.   
  35.     public String url() {  
  36.         return url;  
  37.     }  
  38.   
  39.     public String username() {  
  40.         return username;  
  41.     }  
  42.   
  43.     public String password() {  
  44.         return password;  
  45.     }  
  46. }  

 

    六。ConnectionModule

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.Binder;  
  4. import com.google.inject.Module;  
  5.   
  6. /** 
  7.  * Created by IntelliJ IDEA. 
  8.  * User: Zhong Gang 
  9.  * Date: 11-8-6 
  10.  * Time: 下午3:32 
  11.  */  
  12. public class ConnectionModule implements Module {  
  13.   
  14.     @Override  
  15.     public void configure(Binder binder) {  
  16.   
  17.         binder.bind(String.class).annotatedWith(DriverClassName.class).toInstance("com.mysql.jdbc.Driver");  
  18.         binder.bind(String.class).annotatedWith(Url.class).toInstance("jdbc:mysql://localhost:3306/demo");  
  19.         binder.bind(String.class).annotatedWith(Username.class).toInstance("root");  
  20.         binder.bind(String.class).annotatedWith(Password.class).toInstance("root");  
  21.   
  22.     }  
  23. }  

 

    七。Main

 

Java代码  收藏代码
  1. package com.template.guice;  
  2.   
  3. import com.google.inject.Guice;  
  4. import com.google.inject.Injector;  
  5. import com.google.inject.Module;  
  6. import org.slf4j.Logger;  
  7. import org.slf4j.LoggerFactory;  
  8.   
  9. /** 
  10.  * Created by IntelliJ IDEA. 
  11.  * User: Zhong Gang 
  12.  * Date: 11-8-6 
  13.  * Time: 下午3:36 
  14.  */  
  15. public class Main {  
  16.   
  17.     public static final Logger logger = LoggerFactory.getLogger(Main.class);  
  18.   
  19.     public static void main(String[] args) {  
  20.         Module module = new ConnectionModule();  
  21.         Injector injector = Guice.createInjector(module);  
  22.         Connection instance1 = injector.getInstance(Connection.class);  
  23.         Connection instance2 = injector.getInstance(Connection.class);  
  24.   
  25.         logger.info("instance1 is " + instance1);  
  26.         logger.info("instance2 is " + instance2);  
  27.         logger.info("instance1's hashcode is " + instance1.hashCode());  
  28.         logger.info("instance2's hashcode is " + instance2.hashCode());  
  29.         logger.info("driverClassName is " + instance1.driverClassName());  
  30.         logger.info("url is " + instance1.url());  
  31.         logger.info("username is " + instance1.username());  
  32.         logger.info("password is " + instance1.password());  
  33.     }  
  34. }  

 

    八。运行结果

    annotation

0 0
原创粉丝点击