| 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 |
| 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; |