Changeset 6098

Show
Ignore:
Timestamp:
12/19/11 15:00:23 (5 months ago)
Author:
mtld
Message:

removed automatic bean creation on injection

Location:
branches/release-0.17.x/pustefix-core/src/main/java/org/pustefixframework/container
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/release-0.17.x/pustefix-core/src/main/java/org/pustefixframework/container/annotations/Inject.java

    r4512 r6098  
    3434@Target(ElementType.METHOD) 
    3535@Retention(RetentionPolicy.RUNTIME) 
     36@Deprecated 
    3637public @interface Inject { 
    3738} 
  • branches/release-0.17.x/pustefix-core/src/main/java/org/pustefixframework/container/spring/beans/AnnotationBeanDefinitionPostProcessor.java

    r5032 r6098  
    2727import org.pustefixframework.container.annotations.ImplementedBy; 
    2828import org.pustefixframework.container.annotations.Inject; 
    29 import org.pustefixframework.container.annotations.Scope; 
    30 import org.springframework.aop.scope.ScopedProxyUtils; 
    3129import org.springframework.beans.PropertyValue; 
    3230import org.springframework.beans.factory.BeanInitializationException; 
    33 import org.springframework.beans.factory.NoSuchBeanDefinitionException; 
    3431import org.springframework.beans.factory.config.BeanDefinition; 
    35 import org.springframework.beans.factory.config.BeanDefinitionHolder; 
    3632import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 
    3733import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 
    3834import org.springframework.beans.factory.config.RuntimeBeanReference; 
    39 import org.springframework.beans.factory.support.BeanDefinitionBuilder; 
    4035import org.springframework.beans.factory.support.BeanDefinitionRegistry; 
    4136import org.springframework.beans.factory.support.BeanDefinitionValidationException; 
     
    157152     
    158153    /** 
    159      * Generates the bean name for a bean that is created by this tool. 
    160      *  
    161      * @param beanClassName name of the bean class 
    162      * @return a bean name that is always that is unique for different types  
    163      * but always the same for the same type 
    164      */ 
    165     private String generateAutoBeanName(String beanClassName) { 
    166         return this.getClass().getName() + "#" + beanClassName; 
    167     } 
    168      
    169     /** 
    170154     * Checks wether the bean name represents a bean that was automatically 
    171155     * created by this tool. 
     
    230214         
    231215        if (matchingBeanName == null) { 
    232             Class<?> beanClass = findClassName(wantedType); 
    233             matchingBeanName = generateAutoBeanName(beanClass.getName()); 
    234             try { 
    235                 beanFactory.getBeanDefinition(matchingBeanName); 
    236             } catch (NoSuchBeanDefinitionException e) { 
    237                 if(!(beanFactory instanceof BeanDefinitionRegistry)) { 
    238                     throw new BeanFactoryImplNotSupportedException("Automatically creating bean definitions for " + 
    239                                 "beans injected using the @Inject annotation requires a BeanFactory which implements " + 
    240                                 "the BeanDefinitionRegistry interface."); 
    241                 } 
    242                 BeanDefinitionRegistry beanRegistry = (BeanDefinitionRegistry)beanFactory; 
    243                 Scope annotation = beanClass.getAnnotation(Scope.class); 
    244                 BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(beanClass); 
    245                 String scopeName = null; 
    246                 if (annotation != null) { 
    247                     scopeName = annotation.value(); 
    248                     builder.setScope(scopeName); 
    249                 } 
    250                 BeanDefinition beanDefinition = builder.getBeanDefinition(); 
    251                 processBeanDefinition(matchingBeanName, beanDefinition, beanFactory); 
    252                 BeanDefinitionHolder beanHolder = new BeanDefinitionHolder(beanDefinition, matchingBeanName); 
    253                 if (scopeName != null && !(scopeName.equals("singleton") || scopeName.equals("prototype"))) { 
    254                     beanHolder = ScopedProxyUtils.createScopedProxy(beanHolder, beanRegistry, true); 
    255                 } 
    256                 beanRegistry.registerBeanDefinition(beanHolder.getBeanName(), beanHolder.getBeanDefinition()); 
    257             } 
     216            throw new BeanInitializationException("No Spring bean of type '" + wantedType.getName() + "' found for injection."); 
     217            //Disabled automatic creation of Spring bean definitions as first step to 
     218            //getting rid of the @Inject annotation at all in the future 
    258219        } 
    259220         
     
    261222        beanRefCache.put(wantedType, beanRef); 
    262223        return beanRef; 
    263     } 
    264      
    265     /** 
    266      * Inspects wether the supplied type can be used as a bean class and  
    267      * returns the right type, when the supplied type is an interface with 
    268      * an {@link ImplementedBy} annotation. 
    269      *  
    270      * @param wantedType type that is to be inspected 
    271      * @return type that can be used as a bean class 
    272      * @throws BeanInitializationException if no type suited for usage as a 
    273      * bean class could be found 
    274      */ 
    275     private Class<?> findClassName(Class<?> wantedType) { 
    276         if (wantedType.isInterface()) { 
    277             ImplementedBy annotation = wantedType.getAnnotation(ImplementedBy.class); 
    278             if (annotation != null) { 
    279                 if (annotation.value().isInterface()) { 
    280                     throw new BeanInitializationException("Type \"" + annotation.value().getName() + "\" referenced by ImplementedBy annotation on type \"" + wantedType.getName() + "\" is an interface."); 
    281                 } else { 
    282                     wantedType = annotation.value(); 
    283                 } 
    284             } 
    285         } 
    286          
    287         try { 
    288             wantedType.getConstructor(new Class<?>[] {}); 
    289         } catch (SecurityException e) { 
    290             throw new BeanInitializationException("Type \"" + wantedType.getName() + "\" does not have a public default constructor."); 
    291         } catch (NoSuchMethodException e) { 
    292             throw new BeanInitializationException("Type \"" + wantedType.getName() + "\" does not have a public default constructor."); 
    293         } 
    294          
    295         return wantedType; 
    296224    } 
    297225