1 16 17 package org.springframework.beans.factory.support; 18 19 import java.lang.reflect.Constructor ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 24 import org.springframework.beans.BeanUtils; 25 import org.springframework.beans.factory.BeanDefinitionStoreException; 26 import org.springframework.beans.factory.BeanFactory; 27 import org.springframework.util.StringUtils; 28 29 38 public class SimpleInstantiationStrategy implements InstantiationStrategy { 39 40 public Object instantiate( 41 RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { 42 43 if (beanDefinition.getMethodOverrides().isEmpty()) { 45 return BeanUtils.instantiateClass(beanDefinition.getBeanClass()); 46 } 47 else { 48 return instantiateWithMethodInjection(beanDefinition, beanName, owner); 50 } 51 } 52 53 59 protected Object instantiateWithMethodInjection( 60 RootBeanDefinition beanDefinition, String beanName, BeanFactory owner) { 61 62 throw new UnsupportedOperationException ( 63 "Method Injection not supported in SimpleInstantiationStrategy"); 64 } 65 66 public Object instantiate( 67 RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, 68 Constructor ctor, Object [] args) { 69 70 if (beanDefinition.getMethodOverrides().isEmpty()) { 71 return BeanUtils.instantiateClass(ctor, args); 72 } 73 else { 74 return instantiateWithMethodInjection(beanDefinition, beanName, owner, ctor, args); 75 } 76 } 77 78 84 protected Object instantiateWithMethodInjection( 85 RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, 86 Constructor ctor, Object [] args) { 87 88 throw new UnsupportedOperationException ( 89 "Method Injection not supported in SimpleInstantiationStrategy"); 90 } 91 92 public Object instantiate( 93 RootBeanDefinition beanDefinition, String beanName, BeanFactory owner, 94 Object factoryBean, Method factoryMethod, Object [] args) { 95 96 try { 97 if (!Modifier.isPublic(factoryMethod.getModifiers()) || 99 !Modifier.isPublic(factoryMethod.getDeclaringClass().getModifiers())) { 100 factoryMethod.setAccessible(true); 101 } 102 return factoryMethod.invoke(factoryBean, args); 103 } 104 catch (IllegalArgumentException ex) { 105 throw new BeanDefinitionStoreException( 106 "Illegal arguments to factory method [" + factoryMethod + "]; " + 107 "args: " + StringUtils.arrayToCommaDelimitedString(args)); 108 } 109 catch (IllegalAccessException ex) { 110 throw new BeanDefinitionStoreException( 111 "Cannot access factory method [" + factoryMethod + "]; is it public?"); 112 } 113 catch (InvocationTargetException ex) { 114 throw new BeanDefinitionStoreException( 115 "Factory method [" + factoryMethod + "] threw exception", ex.getTargetException()); 116 } 117 } 118 119 } 120 | Popular Tags |