Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 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.
 import static;
Integrates Guice with Spring.

Author(s): (Bob Lee)
 public class SpringIntegration {
   private SpringIntegration() {}

Creates a provider which looks up objects from Spring using the given name. Expects a binding to org.springframework.beans.factory.BeanFactory. Example usage:
   .toProvider(fromSpring(DataSource.class, "dataSource"));
   public static <T> Provider<T> fromSpring(Class<T> typeString name) {
     return new InjectableSpringProvider<T>(typename);

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 @Named("foo").

   public static void bindAll(Binder binderListableBeanFactory beanFactory) {
     binder = binder.skipSources(SpringIntegration.class);
     for (String name : beanFactory.getBeanDefinitionNames()) {
       Class<?> type = beanFactory.getType(name);
   static <T> void bindBean(Binder binderListableBeanFactory beanFactory,
       String nameClass<T> type) {
     SpringProvider<T> provider
         = SpringProvider.newInstance(typename);
     try {
     catch (Exception e) {
   static class SpringProvider<T> implements Provider<T> {
     boolean singleton;
     final Class<T> type;
     final String name;
     public SpringProvider(Class<T> typeString name) {
       this. = checkNotNull(type"type");
       this. = checkNotNull(name"name");
     static <T> SpringProvider<T> newInstance(Class<T> typeString name) {
       return new SpringProvider<T>(typename);
    void initialize(BeanFactory beanFactory) {
      this. = beanFactory;
      if (!beanFactory.isTypeMatch()) {
        throw new ClassCastException("Spring bean named '" + 
            + "' does not implement " + .getName() + ".");
       = beanFactory.isSingleton();
    public T get() {
      return  ? getSingleton() : .cast(.getBean());
    volatile T instance;
    private T getSingleton() {
      if ( == null) {
         = .cast(.getBean());
      return ;
  static class InjectableSpringProvider<T> extends SpringProvider<T> {
    InjectableSpringProvider(Class<T> typeString name) {
    void initialize(BeanFactory beanFactory) {
New to GrepCode? Check out our FAQ X