2012-06-28 124 views
0

我想問你是否可以爲bean的子屬性注入一個值。如何將值注入到bean的子屬性(不帶註釋)?

這是我想做的事情,但是Eclipse IDE與抱怨:

Bean屬性beanB.subprop不是父類paq.paq.BeanA發現

<managed-bean> 
    <managed-bean-name>beanA</managed-bean-name> 
    <managed-bean-class>paq.paq.BeanA</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
     <managed-property> 
      <property-name>beanB.subprop</property-name> 
      <value>Hello!</value> 
     </managed-property>   
</managed-bean> 



public class BeanA implements Serializable { 

    public BeanB beanB = null;  

    public Skin getBeanB() { 
     if (this.beanB == null) { 
      this.beanB = new BeanB(); 
     } 
     return this.beanB; 
    }  

    public void setBeanB(BeanB beanB) {  
     this.beanB = beanB; 
    } 

} 


public class BeanB implements Serializable { 

    public String subprop = null;  

    public String getSubprop() { 
     return this.subprop; 
    }  

    public void setSubprop(String subprop) {   
     this.subprop = subprop; 
    } 

} 

謝謝!

回答

0

每個嵌套的bean也需要聲明爲一個託管bean。您需要將其注入嵌套的Bean,然後將嵌套的Bean本身注入到主Bean中。

<managed-bean> 
    <managed-bean-name>beanA</managed-bean-name> 
    <managed-bean-class>paq.paq.BeanA</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
    <managed-property> 
     <property-name>beanB</property-name> 
     <value>#{beanB}</value> 
    </managed-property>   
</managed-bean> 
<managed-bean> 
    <managed-bean-name>beanB</managed-bean-name> 
    <managed-bean-class>paq.paq.BeanB</managed-bean-class> 
    <managed-bean-scope>session</managed-bean-scope> 
    <managed-property> 
     <property-name>subprop</property-name> 
     <value>Hello!</value> 
    </managed-property>   
</managed-bean>