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());
- }
- }
八。运行结果