From efaf76b46fcd255008e020b19107c29cc21710e3 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 7 Sep 2009 18:37:01 +0000 Subject: [PATCH] polishing --- .../org/springframework/beans/BeanUtils.java | 2 - .../config/AutowireCapableBeanFactory.java | 2 +- .../AbstractAutowireCapableBeanFactory.java | 71 +++++++++---------- 3 files changed, 35 insertions(+), 40 deletions(-) diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java b/org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java index 11fcc86be41..fc9b7ce2f9e 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/BeanUtils.java @@ -65,8 +65,6 @@ public abstract class BeanUtils { * Convenience method to instantiate a class using its no-arg constructor. * As this method doesn't try to load classes by name, it should avoid * class-loading issues. - *

Note that this method tries to set the constructor accessible - * if given a non-accessible (that is, non-public) constructor. * @param clazz class to instantiate * @return the new instance * @throws BeanInstantiationException if the bean cannot be instantiated diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/AutowireCapableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/AutowireCapableBeanFactory.java index 9dffd88b3dd..050ca889cae 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/AutowireCapableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/config/AutowireCapableBeanFactory.java @@ -121,7 +121,7 @@ public interface AutowireCapableBeanFactory extends BeanFactory { * @return the new bean instance * @throws BeansException if instantiation or wiring failed */ - Object createBean(Class beanClass) throws BeansException; + T createBean(Class beanClass) throws BeansException; /** * Populate the given bean instance through applying after-instantiation callbacks diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java index 4d7afdc25a4..f10b56af256 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java @@ -278,11 +278,12 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac // Typical methods for creating and populating external bean instances //------------------------------------------------------------------------- - public Object createBean(Class beanClass) throws BeansException { + @SuppressWarnings("unchecked") + public T createBean(Class beanClass) throws BeansException { // Use prototype bean definition, to avoid registering bean as dependent bean. RootBeanDefinition bd = new RootBeanDefinition(beanClass); bd.setScope(SCOPE_PROTOTYPE); - return createBean(beanClass.getName(), bd, null); + return (T) createBean(beanClass.getName(), bd, null); } public void autowireBean(Object existingBean) { @@ -338,7 +339,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac return autowireConstructor(beanClass.getName(), bd, null, null).getWrappedInstance(); } else { - Object bean = null; + Object bean; final BeanFactory parent = this; if (System.getSecurityManager() != null) { @@ -419,38 +420,38 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac protected Object createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) throws BeanCreationException { - if (logger.isDebugEnabled()) { - logger.debug("Creating instance of bean '" + beanName + "'"); - } - // Make sure bean class is actually resolved at this point. - resolveBeanClass(mbd, beanName); + if (logger.isDebugEnabled()) { + logger.debug("Creating instance of bean '" + beanName + "'"); + } + // Make sure bean class is actually resolved at this point. + resolveBeanClass(mbd, beanName); - // Prepare method overrides. - try { - mbd.prepareMethodOverrides(); - } - catch (BeanDefinitionValidationException ex) { - throw new BeanDefinitionStoreException(mbd.getResourceDescription(), - beanName, "Validation of method overrides failed", ex); - } + // Prepare method overrides. + try { + mbd.prepareMethodOverrides(); + } + catch (BeanDefinitionValidationException ex) { + throw new BeanDefinitionStoreException(mbd.getResourceDescription(), + beanName, "Validation of method overrides failed", ex); + } - try { - // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. - Object bean = resolveBeforeInstantiation(beanName, mbd); - if (bean != null) { - return bean; - } - } - catch (Throwable ex) { - throw new BeanCreationException(mbd.getResourceDescription(), beanName, - "BeanPostProcessor before instantiation of bean failed", ex); - } + try { + // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. + Object bean = resolveBeforeInstantiation(beanName, mbd); + if (bean != null) { + return bean; + } + } + catch (Throwable ex) { + throw new BeanCreationException(mbd.getResourceDescription(), beanName, + "BeanPostProcessor before instantiation of bean failed", ex); + } - Object beanInstance = doCreateBean(beanName, mbd, args); - if (logger.isDebugEnabled()) { - logger.debug("Finished creating instance of bean '" + beanName + "'"); - } - return beanInstance; + Object beanInstance = doCreateBean(beanName, mbd, args); + if (logger.isDebugEnabled()) { + logger.debug("Finished creating instance of bean '" + beanName + "'"); + } + return beanInstance; } /** @@ -917,7 +918,7 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac */ protected BeanWrapper instantiateBean(final String beanName, final RootBeanDefinition mbd) { try { - Object beanInstance = null; + Object beanInstance; final BeanFactory parent = this; if (System.getSecurityManager() != null) { beanInstance = AccessController.doPrivileged(new PrivilegedAction() { @@ -1467,10 +1468,6 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac * Called by invokeInitMethods. *

Can be overridden in subclasses for custom resolution of init * methods with arguments. - * @param beanName the bean name in the factory (for debugging purposes) - * @param bean the new bean instance we may need to initialize - * @param initMethodName the name of the custom init method - * @param enforceInitMethod indicates whether the defined init method needs to exist * @see #invokeInitMethods */ protected void invokeCustomInitMethod(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable {