Announcement Announcement Module
Collapse
No announcement yet.
Autowire byType does not recognise a java.lang.String bean as a String property Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire byType does not recognise a java.lang.String bean as a String property

    Or it doesn't seem to me to.

    With this emp.xml , EmpMain correctly prints 7900 SMITH 350000.

    Code:
     <bean id="eName" class="java.lang.String" >
               <constructor-arg type="java.lang.String" value="SMITH" />
         </bean>
    
          <bean id="myPension" class="gbpackage.FinalSalaryPension" >
               <property name="pensionValue" value="350000" />
         </bean>
               
          <bean id="emp" class="gbpackage.Emp" >
               <property name="empNo" value="7900" />
               <property name="eName" ref="eName" /> 
             <property name="pension" ref="myPension" />   
    
          </bean>
    Add autowire=byType and 7900 null 350000 is printed :
    Code:
    <bean id="eName" class="java.lang.String" >
               <constructor-arg type="java.lang.String" value="SMITH" />
         </bean>
    
          <bean id="myPension" class="gbpackage.FinalSalaryPension" >
               <property name="pensionValue" value="350000" />
         </bean>
               
          <bean id="emp" class="gbpackage.Emp" autowire="byType" >
               <property name="empNo" value="7900" />
                
    
          </bean>
    Code:
    public class  EmpMain {
           public static void main (String[] args) {
    
                  ApplicationContext context = new ClassPathXmlApplicationContext
                                              (new String[] {"emp.xml"}) ;
                                                                       
                  Emp      emp      = (Emp)      context.getBean("emp") ;
                  System.out.println (emp.getEmpNo() + " " + emp.geteName() + " " + " " +
                                             emp.getPension().getPensionValue()) ;  
    }      } 
    
    public class Pension { 
           int pensionValue ;
           public void setPensionValue(int pensionValue) { this.pensionValue = pensionValue ; }
           public int getPensionValue  ()                      { return pensionValue ; }
    }
    
    class FinalSalaryPension    extends Pension  { }
    class DefinedBenefitPension extends Pension { }
    
    public class Emp {                                    
           private int empNo ;  public String eName ; Pension pension ;
    
           public Pension getPension() { return pension ; }
           public void setPension(Pension pension) { this.pension = pension ; }
    
           public Emp() { }
           public Emp (int empNo , String eName) { this.empNo = empNo ; this.eName = eName ; } 
    
           public int    getEmpNo() { return empNo ; } 
           public void   setEmpNo(int empNo) { this.empNo = empNo ; }  
    
           public String geteName() { return eName ; }
           public void   seteName(String eName) { this.eName = eName ; }  
    }

  • #2
    Please read following part of the reference documentation http://static.springsource.org/sprin...red-exceptions

    You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation is by-design.

    Comment


    • #3
      So declaring java.lang.String as a &lt;bean&gt; does not make it one ?

      If this means that to auto-wire a property it must be a true JavaBean (getters , setters and no-arg constructor) then the looser notion of Spring bean has been tightened up.

      Comment

      Working...
      X