最近在完成一個小小的framework項目,由于項目中不使用spring,guice,自己實現了一個簡易的依賴注入框架。
- 寫一個xml文件作為配置的實際例子
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.ldd600.com/beanIoc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.ldd600.com/beanIoc beanIoc.xsd">
<bean id="ftpSend" class="com.ldd600.bean.ioc.beans.impl.FTPTransporter">
<params>
<param value="/ldd600" key="uploadFolder"></param>
<param value="com.ldd600.bean.ioc.beans.impl.JugUUIDGenerator" key="generator" converter="com.ldd600.bean.ioc.converters.UUIDGeneratorConverter"></param>
</params>
</bean>
<bean id="jmsSend" class="com.ldd600.bean.ioc.beans.impl.JMSTransporter">
<params>
<param value="Ldd600Queue" key="destName"></param>
</params>
</bean>
</beans>
2.根據這個xml文件,定義一個schema文件,驗證配置文件
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:sample="http://www.ldd600.com/beanIoc" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.ldd600.com/beanIoc" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:complexType name="paramsType">
<xsd:annotation>
<xsd:documentation> of java class</xsd:documentation>
</xsd:annotation>
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="param">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>key is property name, value is property value</xsd:documentation>
</xsd:annotation>
<xsd:simpleContent>
<xsd:extension base="sample:leafType">
<xsd:attribute name="key" type="sample:notEmptyToken" use="required"/>
<xsd:attribute name="value" type="sample:notEmptyToken" use="required"/>
<xsd:attribute name="converter" type="sample:classAttributeType" use="optional"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="idClassType">
<xsd:all minOccurs="0">
<xsd:element name="params" type="sample:paramsType">
<xsd:key name="outerParamsKey">
<xsd:selector xpath="sample:param"/>
<xsd:field xpath="@key"/>
</xsd:key>
</xsd:element>
</xsd:all>
<xsd:attribute name="id" type="sample:notEmptyToken" use="required"/>
<xsd:attribute name="class" type="sample:classAttributeType">
<xsd:annotation>
<xsd:documentation>class name</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="leafType">
<xsd:restriction base="xsd:string">
<xsd:pattern value="(\s)*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="classAttributeType">
<xsd:restriction base="sample:notEmptyToken">
<xsd:pattern value="([a-z|0-9|_]+\.)*[A-Z]([A-Z|a-z|0-9|_])*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="notEmptyToken">
<xsd:restriction base="xsd:token">
<xsd:pattern value="(\S+\s*)+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:element name="beans">
<xsd:complexType>
<xsd:sequence maxOccurs="unbounded">
<xsd:element name="bean" type="sample:idClassType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:key name="beanIdkey">
<xsd:selector xpath="./sample:bean"></xsd:selector>
<xsd:field xpath="@id"></xsd:field>
</xsd:key>
</xsd:element>
</xsd:schema>

類型
|
解釋
|
paramType
|
表示類的字段,key是字段名,value字段值,converter如果有用來將String轉換為所需要的實例。
|
idClassType
|
用來表示bean實例,并且該實例有唯一的id
|
notEmptyToken
|
非空token
|
leafType
|
葉子類型比如<a> </a>,中間允許有space,比如空格、換行符等,但是不能出現其他任何字符。
|
classAttributeType
|
負責java中class全名的string類型,比如com.ldd600.BeanClass
|
- 解析這個文件,并marshall成java對象,用xmlbeans實現
使用xmlbeans生成schema對應的java類和xmlbeans jaxb需要的元數據。
· 創建xsdConfig文件,內容主要是生成的java類的package名,類simple name的前綴和后綴
<xb:config
xmlns:xb=
"http://xml.apache.org/xmlbeans/2004/02/xbean/config">
<xb:namespace uri="http://www.ldd600.com/beanIoc">
<xb:package>com.ldd600.bean.ioc.config.jaxb</xb:package>
</xb:namespace>
<xb:namespace uriprefix="http://www.ldd600.com/beanIoc">
<xb:prefix>Xml</xb:prefix>
</xb:namespace>
<xb:namespace uriprefix="http://www.ldd600.com/beanIoc">
<xb:suffix>Bean</xb:suffix>
</xb:namespace>
</xb:config>
· 創建一個bat可執行文件,不用每次跑都去cmd下敲一堆命令,累啊。bat文件的內容:
scomp -d classes\beanIoc -src src\beanIoc -out beanIoc.jar beanIoc.xsd beanIoc.xsdconfig
· 雙擊bat,所需要的java類的.java文件,class文件和xmlbeans jaxb需要的元數據都橫空出世了,這些元數據xmlbeans會用來對xml文件進行驗證,檢查是否符合schema定義的語義規則。
· 把java文件和元數據信息都拷貝到eclipse中吧,或者直接把生成的jar包發布到maven repository中,然后再pom中dependency它。
- 解析的過程中,需要生成被依賴注入的對象,并完成屬性的動態設定。
4.1 生成依賴注入的對象
生成依賴注入的對象是通過反射直接生成類的實例,在這里要求有public的參數為空的構造函數。
Class clazz = Class.forName(sNamclassNamee);

return clazz.newInstance();

4.2 屬性的動態設定
Commons-BeanUtils就是專門免費做這件事的好同志,我們可以利用它來完成,基本類型和一些經常使用的類型,Commons-BeanUtils責無旁貸的提供了自動轉換的功能,beanutils不需要我們提供參數的類型,它可以自動完成轉換,它是根據get和set方法的類型來決定類型的,可參見PropertyDescriptor.getPropertyType()方法。使用方法如下:

if (!PropertyUtils.isWriteable(object, key))
{
throw new ConfigParseException(object.getClass()
+ " doesn't have the property " + key
+ "'s setter method!");
}
String paramVal = paramBean.getValue();
BeanUtils.setProperty(object, key, paramVal);

isWriteable方法判斷是否有可用的set方法,如果有就完成屬性的動態設置。paramBean就是xml文件中定義的那個param。
但是Beanutils默認幫我們轉換的類型為基本類型和所有它已經提供了Converter的class類型,如果我們有特殊的類需要進行動態設定,必須自己提供converter,注冊到它的converters map中。這樣beanutils兄弟在動態設定屬性值的時候,就會根據屬性的類型去converter map中把取出該屬性類型對應的自定義converter來轉換。因為在這里配置文件中配置的都是String, 所以我們對Converter接口做了修改:

public abstract class BeanConverter implements Converter
{
public abstract Object convert(Class type, String value) throws ConfigParseException;


public Object convert(Class type, Object value)
{
return this.convert(type, (String)value);
}
}

我們強制規定了convert方法的參數必須是String,自己提供的converter必須繼承BeanConverter抽象類。
String key = paramBean.getKey();
String converterClsName = paramBean.getConverter();

if (StringUtils.isNotEmpty(converterClsName))
{
Class converterClazz = Class.forName(converterClsName);
if (!BeanConverter.class

.isAssignableFrom(converterClazz))
{
throw new ConfigParseException(
"converter must extend BeanConverter!");
}

if(!this.converters.containsKey(converterClsName))
{
this.converters.put(converterClsName, converterClazz);
// get property type
Class propertyClazz = PropertyUtils.getPropertyType(
object, key);
// register converter
ConvertUtils.register((Converter) converterClazz
.newInstance(), propertyClazz);
}
} }

4.3屬性邏輯規則的檢查
在設置好屬性以后,這個屬性的值并不一定配置的正確,也不一定滿足邏輯規則,比如希望int值在3到5之間,比如希望String 不要為空等等。為了在動態設定完屬性后進行邏輯規則的校驗,提供了InitializingBean接口


public interface InitializingBean
{
void afterPropertiesSet() throws Exception;
}
實現該接口的類,它們的邏輯規則需要在afterProperties提供,不符合規則的請拋異常,并會在屬性設定完后檢查

public void afterPropertiesSet() throws Exception
{

if (StringUtils.isEmpty(uploadFolder))
{
throw new IllegalArgumentException(
"upload folder is an empty string!");
}

if (null == generator)
{
throw new IllegalArgumentException("generator is null!");
}
}


if (object instanceof InitializingBean)
{
((InitializingBean) object).afterPropertiesSet();
}

4.4將bean注冊到BeanContext中
String id = idClassTypeBean.getId();
BeanContextFactory.getBeanContext().setBean(id, object);

4.5清理環境
完成屬性的動態注入后,還需要清理環境

private void cleanConfig()
{
ConvertUtils.deregister();
this.converters = null;
}

5.如何做到基于接口設計
· Converter提供了基于接口設計的功能:我們可以動態的設置不同的實現。
· 用了該框架,本身就基于接口,我們可以在配置文件中修改bean的實現類。應用程序代碼它不關心具體的實現類,它只關心id
Transporter transporter = (Transporter) BeanContextFactory.getBeanContext().getBean(TransporterParser.getTransportName());
不過,這里沒有象spring和juice那么強大的bean factory功能。因為這個東東只是一個小項目的一小部分,所以功能上滿足小項目的需求就足夠了。
6. Test
就簡單的測了一下,可以看源代碼。
7.總結
主要是項目是基于接口設計的,所以一些類的實現需要在配置文件里設定,實現類的實例屬性也要是可以擴展的,并且提供屬性值的邏輯校驗,所以就有了這么一個東東。
beanIoc源代碼