⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 springintegration.java

📁 Guice是轻量级的依赖注入框架。简而言之
💻 JAVA
字号:
/** * Copyright (C) 2006 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.google.inject.spring;import static com.google.inject.util.Objects.nonNull;import com.google.inject.Provider;import com.google.inject.Inject;import com.google.inject.Binder;import com.google.inject.name.Names;import com.google.inject.spi.SourceProviders;import com.google.inject.util.Objects;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.ListableBeanFactory;/** * Integrates Guice with Spring. * * @author crazybob@google.com (Bob Lee) */public class SpringIntegration {  static {    SourceProviders.skip(SpringIntegration.class);  }  private SpringIntegration() {}  /**   * Creates a provider which looks up objects from Spring using the given name.   * Expects a binding to {@link   * org.springframework.beans.factory.BeanFactory}. Example usage:   *   * <pre>   * bind(DataSource.class)   *   .toProvider(fromSpring(DataSource.class, "dataSource"));   * </pre>   */  public static <T> Provider<T> fromSpring(Class<T> type, String name) {    return new InjectableSpringProvider<T>(type, name);  }  /**   * Binds all Spring beans from the given factory by name. For a Spring bean   * named "foo", this method creates a binding to the bean's type and   * {@code @Named("foo")}.   *   * @see com.google.inject.name.Named   * @see com.google.inject.name.Names#named(String)    */  public static void bindAll(Binder binder, ListableBeanFactory beanFactory) {    for (String name : beanFactory.getBeanDefinitionNames()) {      Class<?> type = beanFactory.getType(name);      bindBean(binder, beanFactory, name, type);    }  }  static <T> void bindBean(Binder binder, ListableBeanFactory beanFactory,      String name, Class<T> type) {    SpringProvider<T> provider        = SpringProvider.newInstance(type, name);    try {      provider.initialize(beanFactory);    }    catch (Exception e) {      binder.addError(e);      return;    }    binder.bind(type)        .annotatedWith(Names.named(name))        .toProvider(provider);  }  static class SpringProvider<T> implements Provider<T> {    BeanFactory beanFactory;    boolean singleton;    final Class<T> type;    final String name;    public SpringProvider(Class<T> type, String name) {      this.type = nonNull(type, "type");      this.name = nonNull(name, "name");    }    static <T> SpringProvider<T> newInstance(Class<T> type, String name) {      return new SpringProvider<T>(type, name);    }    void initialize(BeanFactory beanFactory) {      this.beanFactory = beanFactory;      if (!beanFactory.isTypeMatch(name, type)) {        throw new ClassCastException("Spring bean named '" + name            + "' does not implement " + type.getName() + ".");      }      singleton = beanFactory.isSingleton(name);    }    public T get() {      return singleton ? getSingleton() : type.cast(beanFactory.getBean(name));    }    volatile T instance;    private T getSingleton() {      if (instance == null) {        instance = type.cast(beanFactory.getBean(name));      }      return instance;    }  }  static class InjectableSpringProvider<T> extends SpringProvider<T> {    InjectableSpringProvider(Class<T> type, String name) {      super(type, name);    }    @Inject    @Override    void initialize(BeanFactory beanFactory) {      super.initialize(beanFactory);    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -