請Spring如何注入帶有建構函式的物件呢?

Antinomy發表於2010-03-23
首先不怕大家見笑的,我之前沒用過Spring框架,只是見過相關介紹(囧 一直都用產品開發 BO+jsp。。。)。。。
事情的起因是我有一個很簡陋的工廠。

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;

/**
* @author Antinomy 規格工廠
*/
public class SpecificationFactory
{
public ActionForm _form;
public HttpServletRequest _request;

public SpecificationFactory(ActionForm form, HttpServletRequest request)
{
this._form = form;
this._request = request;
}

public iSpecification getSqlSpecificationInstance(String factoryType)
{
iSpecification spn = null;

if (factoryType.equals("grsb_jkqk"))
spn = new Grsb_Jkqk_WebiSpecification(_form, _request);

if (factoryType.equals("tjbb_sbrshsjetj"))
spn = new Tjbb_sbrshsjetj_WebiSpecification(_form, _request);

if (factoryType.equals("Yhs_dwsbjk"))
spn = new Yhs_dwsbjk_WebiSpecification(_form, _request);

if (factoryType.equals("Yhs_dwsbjkmx"))
spn = new Yhs_dwsbjkmx_WebiSpecification(_form, _request);

if (factoryType.equals("Zgfx_zb")
|| factoryType.equals("Zgfx_dj")
|| factoryType.equals("Zgfx_rk")
|| factoryType.equals("Zgfx_sb"))
spn = new Zgfx_Tjrq_WebiSpecification(_form, _request);

return spn;
}

}


如果要增加新的規格,就需要修改這個工廠類。所以打算嘗試用依賴注入來解決
配置如下
<bean id="HttpServletRequest" class="javax.servlet.http.HttpServletRequest">
<bean id="ActionForm" class="org.apache.struts.action.ActionForm">

<bean id="Grsb_Jkqk" class="Grsb_Jkqk_WebiSpecificatio">
<constructor-arg > <ref bean="HttpServletRequest"/></constructor-arg>
<constructor-arg > <ref bean="ActionForm"/> </constructor-arg>

</bean>

於是修改getSqlSpecificationInstance方法
public iSpecification getSqlSpecificationInstance(String factoryType)
{
iSpecification spn = null;

InputStream is = new FileInputStream("bean.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
//....
iSpecification spn = (iSpecification) factory.getBean("Grsb_Jkqk");

return spn;
}

囧,於是在new Grsb_Jkqk_WebiSpecification物件的時候,因為沒有傳入構造引數,異常了。
看了一些例子,都是關於一些常用型別了,另外我也很納悶,在factory.getBean("Grsb_Jkqk")的
時候也沒有指明如何傳入 _form, _request這些引數,所以我的第一場spring之旅以失敗告終o(╯□╰)o
Orz,望高手指教

後來我也在想 我不過就是簡單讓spring來幫助管理物件的而已,為什麼非得搞得很複雜。
在忙活了一天之後,我放棄了用spring,改用java 反射來實現。不過只是後話了。。
public iSpecification getWebiSpecificationInstance(
String WebiSpecificationName)
{
iSpecification spn = null;

try
{
String _classCfg = "SpecificationClass.Properties";
_classProps = FileService.LoadProperties(request,_classCfg);//配置檔案
String className = this._classProps.getProperty(WebiSpecificationName);
class targetClass = Class.forName(className);
class [] parmType=new Class []{ActionForm.class,HttpServletRequest.class};
Object [] parmValue= new Object []{_form, _request};
Constructor constructor = targetClass.getConstructor(parmType);
spn = (iSpecification)constructor.newInstance(parmValue);
}
catch ( ClassNotFoundException e ) { e.printStackTrace(); }
catch ( InstantiationException e ) { e.printStackTrace(); }
catch ( IllegalAccessException e ) { e.printStackTrace(); }
catch ( SecurityException e ) { e.printStackTrace(); }
catch ( NoSuchMethodException e ) { e.printStackTrace(); }
catch ( InvocationTargetException e ) { e.printStackTrace(); }
catch ( Exception e ) { e.printStackTrace(); }

return spn;
}

相關文章