`
Wind_ZhongGang
  • 浏览: 260400 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Google Guice Annotation Binding

阅读更多

    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());
    }
}

 

    八。运行结果

    annotation

  • 大小: 17.9 KB
2
5
分享到:
评论
2 楼 Wind_ZhongGang 2011-08-06  
谢谢,呵呵
1 楼 bastengao 2011-08-06  
支持下作者

相关推荐

    Google Guice与MyBatis集成,并实现发送邮件轮询

    Google Guice 这个高效的与Spring类似的依赖注入框架; MyBatis配置和使用; Google Guice与MyBatis集成,支持注解事务,简单的无法想象; Mybatis与mysql集成;实现发送邮件轮询; 源码是个web项目,里面有数据库的...

    google guice 3.0源码

    google guice 3.0源码,官方下载,帮助你更好理解google guice实现的原理

    google Guice 1.0 用户指南 中文

    用 Guice 写 Java Guice 1.0 用户指南 王咏刚 译 Guice (读作"juice")是超轻量级的,下一代的,为Java 5及后续版本设计的依赖注入容器。

    Google Guice: Agile Lightweight Dependency Injection Framework

    Google Guice: Agile Lightweight Dependency Injection Framework will not only tell you "how," it will also tell you "why" and "why not," so that all the knowledge you gain will be as widely applicable ...

    google guice基础例子

    Guice是Google开发的一个轻量级,基于Java5(主要运用泛型与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。Guice是类型安全的,它能够对构造函数,属性,方法(包含任意个参数的任意方法,而不仅仅是setter...

    Learning Google Guice

    谷歌Guice开发英文文档,很详细,对开发很有帮助,可当成工具书使用!

    DI容器框架Google Guice与Spring框架的区别

    DI容器,例如spring,picoContainer,EJB容器等等 与Guice的不同

    Google Guice需要的jar

    Google Guice需要的jar包: Guice-3.0.jar javax.inject.jar

    google-guice用户手册

    google-guice用户手册,据说和spring pk

    Google guice

    NULL 博文链接:https://m635674608.iteye.com/blog/2090042

    Google Guice入世(转 附带一Guice1.0的简单测试代码)

    博文链接:https://avengerbevis.iteye.com/blog/69237

    Google的产品Guice

    用户指南 博文链接:https://hejianjie.iteye.com/blog/83374

    guice.jar/guice.jar

    guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar guice.jar

    google-guice

    Guice (读作"juice")是超轻量级的,下一代的,为Java 5及后续版本设计的依赖注入容器。 <br>

    guice:Google Guice扩展

    该项目包含一组Google Guice扩展程序,可用于开发中。 符合OSGi: 生成状态: 问题: : //github.com/mycila/license-maven-plugin/issues 许可证: Apache License 2.0 贡献者 @mgoellnitz @keeganwitt ...

    Guice中文文档

    Guice中文文档,介绍Guice的基本使用,适合初学者。

    guice-3.0-API文档-中英对照版.zip

    标签:google、inject、guice、jar包、java、API文档、中英对照版; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准...

    guice-multibindings-3.0-API文档-中文版.zip

    标签:google、inject、extensions、guice、multibindings、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变...

    guice-3.0-API文档-中文版.zip

    标签:google、inject、guice、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请...

Global site tag (gtag.js) - Google Analytics