比如说有一个Person类,一个Cat类
我想在创建Person时给他注入一个Cat数组,表示他养的猫
即Person中有属性private Cat[] cats;
而Cat中有name属性
在给Person注入Cat数组时,我想给每个Cat指定name属性
能否实现?

解决方案 »

  1.   

    在applicationcontext.xml配置文件中注入属性就可以了
      

  2.   

    如果这样要求的话
    可以指令Person类的Lazy=true
    然后在主动注入person的类上实现ApplicationContextAware接口
    通过context接口的getBean(beanname,param...)方法生产person对象
      

  3.   

    <bean id="" class="" >         
            <constructor-arg value="" />         
            <constructor-arg value=""/>        
            <constructor-arg value=""/>     
    </bean>   
      

  4.   

    注入时候注意注入的成员变量添加set()方法,剩下的同上,或用注解注入