如果Bean构造函数具有源Object的参数,那么如何编写XML配置?

时间:2014-06-30 23:37:49

标签: java xml spring

基本通知类

public class Notice {

   public Notice (Object source)
   {
      //some constructor initialization.
   }

}

特殊情况 OfferNotice

public class OfferNotice  extends Notice {

   public OfferNotice  (Object source)
   {
      super(source);
      //some constructor initialization.
   }

}

GenericNoticeRecord

abstract public class GenericNoticeRecord <E extends Notice> 
{
        abstract public void recordNotice();     
}

特殊情况 OfferNoticeRecord

class OfferNoticeRecord extends GenericNoticeRecord <OfferNotice> 
{

    private OfferNotice notice;

    public void setNotice(OfferNotice notice) {
        this.notice= notice;
    }

    public void recordNotice() {        
        // some implementation on Notice
    }


}

MainService

public class Mainservice {

    private OfferNoticeRecord offerNoticeRecord;

    public void send()
    {
       offerNoticeRecord.recordNotice();
    }
}

App

public class App {

   ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
   serv = context.getBean("MainService");

   serv.send()

}

现在如何通过XML为此示例应用程序注入依赖项? 以下是我在XML Config中尝试的内容

<bean id="MainService" class="com.test.MainService">    
    <property name="offerNoticeRecord">
         <ref bean="OfferNoticeRecordBean"/>
    </property>
</bean>

<bean id="OfferNoticeRecordBean" class="com.test.OfferNoticeRecord">    
    <property name="notice">
        <ref bean="OfferNoticeBean"/>
    </property>
</bean>

<bean id="OfferNoticeBean" class="com.test.OfferNotice">    
    //here is major confusion, constructor is expecting source Object
</bean>

0 个答案:

没有答案