1. 程式人生 > >Spring的bean中注入內部類

Spring的bean中注入內部類

在spring中注入內部類,有可能會遇到如下異常資訊:

2014-5-14 21:52:45 org.springframework.context.support.AbstractApplicationContext prepareRefresh
資訊: Refreshing org[email protected]1c56c60: startup date [Wed May 14 21:52:45 CST 2014]; root of context hierarchy
2014-5-14 21:52:45 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
資訊: Loading XML bean definitions from class path resource [spring.xml]
2014-5-14 21:52:46 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
資訊: Pre-instantiating singletons in

org.s[email protected]7244ca: defining beans [person]; root of factory hierarchy
2014-5-14 21:52:46 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
資訊: Destroying singletons in org.s[email protected]7244ca: defining beans [person]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'person' defined in class path resource [spring.xml]: Cannot create inner bean 'cn.outofmemory.spring.Person$Hand#ab7165' of type [cn.outofmemory.spring.Person$Hand] while setting bean property 'hands' with key [0]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.outofmemory.spring.Person$Hand#ab7165' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [cn.outofmemory.spring.Person$Hand]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.outofmemory.spring.Person$Hand.<init>()
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:281)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:120)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:153)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1325)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
 at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
 at cn.outofmemory.spring.App.main(App.java:14)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cn.outofmemory.spring.Person$Hand#ab7165' defined in class path resource [spring.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [cn.outofmemory.spring.Person$Hand]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.outofmemory.spring.Person$Hand.<init>()
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:965)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:911)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
 at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
 ... 17 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [cn.outofmemory.spring.Person$Hand]: No default constructor found; nested exception is java.lang.NoSuchMethodException: cn.outofmemory.spring.Person$Hand.<init>()
 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:70)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:958)
 ... 21 more
Caused by: java.lang.NoSuchMethodException: cn.outofmemory.spring.Person$Hand.<init>()
 at java.lang.Class.getConstructor0(Class.java:2715)
 at java.lang.Class.getDeclaredConstructor(Class.java:1987)
 at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:65)
 ... 22 more這個異常資訊很長,他的意思是說我們沒有給內部類指定建構函式,我們看下我們的程式碼和spring配置檔案:

我們定義了一個Person類,這個類中有一個內部類Hand,Person類有一個hands的屬性的型別是內部類陣列

package cn.outofmemory.spring;

public class Person { 
 private Hand[] hands;
 
 public Hand[] getHands() {
  return hands;
 }

 public void setHands(Hand[] hands) {
  this.hands = hands;
 }

 public class Hand {
  private int strength;

  public int getStrength() {
   return strength;
  }
  public void setStrength(int strength) {
   this.strength = strength;
  }
  
 }
}
我們的spring配置檔案如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean class="cn.outofmemory.spring.Person" id="person">
  <property name="hands">
   <list>
    <bean class="cn.outofmemory.spring.Person$Hand">
     <property name="strength" value="90"/>
    </bean>
   </list>
  </property>
 </bean>
</beans>配置檔案很簡單,內部類的類名由外部類的全稱+$+內部類的名稱,這是無疑的。

內部類注入方式一:新增內部類預設建構函式引數

我們遇到上面的錯誤是因為非靜態的內部類預設的建構函式有一個引數,這個引數指向其外部類的例項,所以我們需要給此內部類的bean新增constructor-arg節點,並指向外部類即可,我們修改下配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean class="cn.outofmemory.spring.Person" id="person">
  <property name="hands">
   <list>
    <bean class="cn.outofmemory.spring.Person$Hand">
     <constructor-arg ref="person"></constructor-arg>
     <property name="strength" value="90"/>
    </bean>
   </list>
  </property>
 </bean>
</beans>App類程式碼如下:

package cn.outofmemory.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello  spring from OutOfMemory.CN
 *
 */
public class App
{
    public static void main( String[] args )
    {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("/spring.xml");
        Person person = appContext.getBean(Person.class);
        for (Person.Hand hand : person.getHands()) {
         System.out.println("hand strength is " + hand.getStrength());
        }
    }
}App類的main方法獲得spring中定義的person類,然後列印每一個hand的strength屬性。輸出如下:

hand strength is 90


內部類注入方式二:將內部類修改為static


對於內部類,如果沒必要訪問外部類,我們可以將其定義為static的,這樣在spring配置檔案中配置時,就不需要設定預設構造函數了。

如下修改後的Person類:

package cn.outofmemory.spring;

public class Person { 
 private Hand[] hands;
 
 public Hand[] getHands() {
  return hands;
 }

 public void setHands(Hand[] hands) {
  this.hands = hands;
 }

 public static class Hand {
  private int strength;

  public int getStrength() {
   return strength;
  }

  public void setStrength(int strength) {
   this.strength = strength;
  }
  
 }
}
修改後的spring配置檔案:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean class="cn.outofmemory.spring.Person" id="person">
  <property name="hands">
   <list>
    <bean class="cn.outofmemory.spring.Person$Hand">
     <property name="strength" value="90"/>
    </bean>
   </list>
  </property>
 </bean>
</beans>App類保持不變,我們再次執行程式,依然會得到如下的輸出結果:

hand strength is 90
總結:
對於內部類的注入,要注意非靜態內部類,其預設建構函式有一個引數,是其外部類的例項,記住這一點在spring中定義內部類就沒有問題了。

如果內部類不需要訪問外部類的例項,可以將其定義為static的,這樣也就不需要額外的建構函式引數設定了。