`
Wind_ZhongGang
  • 浏览: 261560 次
  • 性别: 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 1.0 用户指南 中文

    "google Guice 1.0 用户指南 中文" Guice 是一个超轻量级的、下一代的、为 Java 5 及后续版本设计的依赖注入容器。它可以帮助 Java 企业应用开发社区解决连接对象方面的问题,例如 Web 应用如何访问中间层服务、...

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

    "DI容器框架Google Guice与Spring框架的区别" 这个标题表明我们要探讨的是两种不同的依赖注入(Dependency Injection,简称DI)容器——Google Guice和Spring框架之间的差异。DI是一种设计模式,它帮助开发者在对象...

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

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

    Google guice

    **Google Guice**,全称为Google Injection,是一个轻量级的依赖注入框架,它通过注解(Annotations)来实现对象的自动装配,简化了Java应用的构造和管理。Guice的核心理念是帮助开发者摆脱手动创建对象和管理对象...

    google-guice

    Guice的核心概念包括模块(Module)、注解(Annotation)和绑定(Binding)。模块是Guice配置的载体,它定义了哪些类将被实例化以及它们之间的依赖关系。开发者可以创建自定义模块,并通过`@Provides`注解的方法来...

    google guice 3.0源码

    Google Guice,全称为GoogleInject,是一个轻量级的依赖注入框架,由Google开发并开源。Guice的目标是简化Java应用程序的构造和管理,通过自动装配对象依赖关系,让开发者可以专注于业务逻辑而不是对象的创建和组装...

    Google Guice需要的jar

    Google Guice是一个轻量级的依赖注入框架,由Google开发并维护,主要用于简化Java应用程序的构建和管理。依赖注入(Dependency Injection,简称DI)是一种设计模式,它可以帮助开发者减少代码间的耦合,提高代码的可...

    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开发英文文档,很详细,对开发很有帮助,可当成工具书使用!

    google-guice用户手册

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

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

    Guice的核心组件包括模块(Module)、注解(Annotation)和绑定(Binding)。 1. **模块(Module)** 模块是Guice配置的基石,用于定义一组绑定规则。开发者可以通过实现`com.google.inject.Module`接口并重写`...

    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

    初试Guice(转)

    Guice,全称Google Guice,是一款轻量级的依赖注入(Dependency Injection,简称DI)框架,由Google开发并开源。它主要用于简化Java应用程序的构造和管理,通过DI来解耦代码,使代码更易于测试、维护和扩展。Guice的...

    Apress.Google.Guice.Agile.Lightweight.Dependency.Injection.Framework.Apr.2008

    《Apress.Google.Guice.Agile.Lightweight.Dependency.Injection.Framework.Apr.2008》这本书专注于介绍Google Guice这一轻量级依赖注入框架。依赖注入(Dependency Injection,简称DI)是软件设计模式中的一种,它...

    guice:Google Guice扩展

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

    Guice中文文档

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

Global site tag (gtag.js) - Google Analytics