Google Guice @Singleton @Named

阅读更多

  Google Guice提供了一个名为@Singleton的注解,只要在类上加上这个注解,就可以实现一个单例类,不需要自己手动编写单例实现类。@Named注解提供了为属性赋值的功能。

 

  一。Scope

 

package com.template.juice;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-5
 * Time: 下午8:26
 */
public enum Scope {
    REQUEST,
    SESSION,
    GLOBAL,
    UNSCOPE
}

 

  二。Database

 

package com.template.juice;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.inject.name.Named;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-5
 * Time: 下午8:33
 */
@Singleton
public class Database {

    @Named("driverClassName")
    @Inject
    private String driverClassName;

    @Named("url")
    @Inject
    private String url;

    @Named("username")
    @Inject
    private String username;

    @Named("password")
    @Inject
    private String password;

    /**
     * Just for test and study,it does not make sense.
     */
    @Named("scope")
    @Inject
    private Scope scope;

    public String driverClassName() {
        return driverClassName;
    }

    public String url() {
        return url;
    }

    public String username() {
        return username;
    }

    public String password() {
        return password;
    }

    public Scope scope() {
        return scope;
    }
}

 

  三。DatabaseModule

 

package com.template.juice;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

/**
 * Created by IntelliJ IDEA.
 * User: Zhong Gang
 * Date: 11-8-5
 * Time: 下午8:38
 */
public class DatabaseModule extends AbstractModule {

    @Override
    protected void configure() {

        bind(String.class).annotatedWith(Names.named("driverClassName")).toInstance("com.mysql.jdbc.Driver");

        bind(String.class).annotatedWith(Names.named("url")).toInstance("jdbc:mysql://localhost:3306/demo");

        bind(String.class).annotatedWith(Names.named("username")).toInstance("root");

        bind(String.class).annotatedWith(Names.named("password")).toInstance("root");

        bind(Scope.class).annotatedWith(Names.named("scope")).toInstance(Scope.GLOBAL);

    }
}

 

  四。Main

 

package com.template.juice;

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-5
 * Time: 下午8:42
 */
public class Main {
    private static Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) {
        Module module = new DatabaseModule();
        Injector injector = Guice.createInjector(module);
        Database instance1 = injector.getInstance(Database.class);
        Database instance2 = injector.getInstance(Database.class);

        logger.info("driverClassName is " + instance1.driverClassName());
        logger.info("url is " + instance1.url());
        logger.info("username is " + instance1.username());
        logger.info("password is " + instance1.password());

        Boolean isSingleton = instance1.equals(instance2);
        logger.info(instance1.toString());
        logger.info(instance2.toString());
        logger.info(isSingleton.toString());
    }

}

 

  五。运行结果

  Google Guice @Singleton @Named_第1张图片

 

  可以看到instance1和instance2的hashcode相同,并且两者相比为true,说明injector返回的是同一个实例。

 

  去除Database类的@Singleton注解,运行结果为

  Google Guice @Singleton @Named_第2张图片

 

  可以看到去除@Singleton注解后,instance1和instance2的hashcode不同,并且两者相比为false,说明instance1和instance2为不同的实例。

  • Google Guice @Singleton @Named_第3张图片
  • 大小: 10.5 KB
  • Google Guice @Singleton @Named_第4张图片
  • 大小: 10.5 KB
  • 查看图片附件

你可能感兴趣的:(Google,Guice,@Singleton,@Named)