Google Guice Annotation Binding-5
来源:互联网 发布:单片机射频通讯 编辑:程序博客网 时间:2024/06/07 19:00
Google Guice提供Annotation Binding,可以使用注解来对依赖进行绑定并同时进行赋值。
一。@DriverClassName
- package com.template.guice;
- import com.google.inject.BindingAnnotation;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import static java.lang.annotation.ElementType.*;
- import static java.lang.annotation.RetentionPolicy.RUNTIME;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:23
- */
- @BindingAnnotation
- @Target({ METHOD, CONSTRUCTOR, FIELD })
- @Retention(RUNTIME)
- public @interface DriverClassName {
- }
二。@Username
- package com.template.guice;
- import com.google.inject.BindingAnnotation;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import static java.lang.annotation.ElementType.CONSTRUCTOR;
- import static java.lang.annotation.ElementType.FIELD;
- import static java.lang.annotation.ElementType.METHOD;
- import static java.lang.annotation.RetentionPolicy.RUNTIME;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:23
- */
- @BindingAnnotation
- @Target({ METHOD, CONSTRUCTOR, FIELD })
- @Retention(RUNTIME)
- public @interface Username {
- }
三。@Url
- package com.template.guice;
- import com.google.inject.BindingAnnotation;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import static java.lang.annotation.ElementType.*;
- import static java.lang.annotation.RetentionPolicy.RUNTIME;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:23
- */
- @BindingAnnotation
- @Target({ METHOD, CONSTRUCTOR, FIELD })
- @Retention(RUNTIME)
- public @interface Url {
- }
四。@Password
- package com.template.guice;
- import com.google.inject.BindingAnnotation;
- import java.lang.annotation.Retention;
- import java.lang.annotation.Target;
- import static java.lang.annotation.ElementType.*;
- import static java.lang.annotation.RetentionPolicy.RUNTIME;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:23
- */
- @BindingAnnotation
- @Target({ METHOD, CONSTRUCTOR, FIELD })
- @Retention(RUNTIME)
- public @interface Password {
- }
五。DBConnection
- package com.template.guice;
- import com.google.inject.Inject;
- import com.google.inject.Singleton;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:28
- */
- @Singleton
- public class Connection {
- @Inject
- @DriverClassName
- private String driverClassName;
- @Inject
- @Url
- private String url;
- @Inject
- @Username
- private String username;
- @Inject
- @Password
- private String password;
- public String driverClassName() {
- return driverClassName;
- }
- public String url() {
- return url;
- }
- public String username() {
- return username;
- }
- public String password() {
- return password;
- }
- }
六。ConnectionModule
- package com.template.guice;
- import com.google.inject.Binder;
- import com.google.inject.Module;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:32
- */
- public class ConnectionModule implements Module {
- @Override
- public void configure(Binder binder) {
- binder.bind(String.class).annotatedWith(DriverClassName.class).toInstance("com.mysql.jdbc.Driver");
- binder.bind(String.class).annotatedWith(Url.class).toInstance("jdbc:mysql://localhost:3306/demo");
- binder.bind(String.class).annotatedWith(Username.class).toInstance("root");
- binder.bind(String.class).annotatedWith(Password.class).toInstance("root");
- }
- }
七。Main
- package com.template.guice;
- import com.google.inject.Guice;
- import com.google.inject.Injector;
- import com.google.inject.Module;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Created by IntelliJ IDEA.
- * User: Zhong Gang
- * Date: 11-8-6
- * Time: 下午3:36
- */
- public class Main {
- public static final Logger logger = LoggerFactory.getLogger(Main.class);
- public static void main(String[] args) {
- Module module = new ConnectionModule();
- Injector injector = Guice.createInjector(module);
- Connection instance1 = injector.getInstance(Connection.class);
- Connection instance2 = injector.getInstance(Connection.class);
- logger.info("instance1 is " + instance1);
- logger.info("instance2 is " + instance2);
- logger.info("instance1's hashcode is " + instance1.hashCode());
- logger.info("instance2's hashcode is " + instance2.hashCode());
- logger.info("driverClassName is " + instance1.driverClassName());
- logger.info("url is " + instance1.url());
- logger.info("username is " + instance1.username());
- logger.info("password is " + instance1.password());
- }
- }
八。运行结果
0 0
- Google Guice Annotation Binding-5
- Google Guice Provider Binding--3
- Google Guice入门教程5
- Google Guice
- Google guice
- Google Guice
- Google Guice入门教程1
- Google Guice入门教程2
- Google Guice入门教程3
- Google Guice入门教程4
- Google-Guice入门介绍
- Google - Guice 入门介绍
- Google Guice 简介
- Google Guice简介
- Google-Guice入门介绍
- Google Guice使用入门
- Google Guice 绑定2
- Google Guice Provider绑定
- RFC 6719中文版: The Minimum Rank with Hysteresis Objective Function
- CSDN博客文章转载
- 怎样使用FreeMarker来实现动态网页静态化
- LeetCode解题报告 107. Binary Tree Level Order Traversal II [easy]
- 位运算汇总
- Google Guice Annotation Binding-5
- 394. Decode String 难度:medium
- 字符串函数
- 内存泄露问题总结--static修饰的静态View
- Google Gson 基本用法
- java动态代理实现步骤解析
- 关于java中返回ResultSet结果集行数
- caffe 训练之学习率
- 单点登录SSO的实现原理