<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    閑人野居
    好好學習,天天向上
    posts - 57,  comments - 137,  trackbacks - 0
    看了spring test 用例,其實實現這一功能還算比較簡單,主要分以下的步驟,具體的實例可以去參考spring 自帶的testcase

    首先定義相關xsd文件,用于驗證相應的行為:
    ?
    主要增加了4個自定義元素和1個屬性:

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>

    ??? <xsd:schema xmlns="http://www.springframework.org/schema/beans/test"
    ??? ??? ??? ??? ??? ??? xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    ??? ??? ??? ??? ??? ??? targetNamespace="http://www.springframework.org/schema/beans/test"
    ??? ??? ??? ??? ??? ??? elementFormDefault="qualified">

    ??? <xsd:element name="testBean">
    ??? ??? <xsd:complexType>
    ??? ??? ??? <xsd:attribute name="id" type="xsd:string" use="required" form="unqualified"/>
    ??? ??? ??? <xsd:attribute name="name" type="xsd:string" use="required" form="unqualified"/>
    ??? ??? ??? <xsd:attribute name="age" type="xsd:integer" use="required" form="unqualified"/>
    ??? ??? </xsd:complexType>
    ??? </xsd:element>

    ??? <xsd:element name="set">
    ??? ??? <xsd:complexType>
    ??? ??? ??? <xsd:attribute name="name" type="xsd:string" use="required" form="unqualified"/>
    ??? ??? ??? <xsd:attribute name="age" type="xsd:integer" use="required" form="unqualified"/>
    ??? ??? </xsd:complexType>
    ??? </xsd:element>

    ??? <xsd:element name="debug"/>
    ??? <xsd:element name="nop"/>

    ??? <xsd:attribute name="object-name" type="xsd:string"/>

    </xsd:schema>

    接著定義handler映射文件:customNamespace.properties

    http\://www.springframework.org/schema/beans/test=org.springframework.beans.factory.xml.support.TestNamespaceHandler

    定義Handler:

    ??? 主要注冊相應的解析類和裝飾類

    ??

    ?publicclass TestNamespaceHandler extends NamespaceHandlerSupport {

    ???????? publicvoid init() {

    ?????????????????? //相對于每個xsd中定義的元素

    ?????? registerBeanDefinitionParser("testBean", new TestBeanDefinitionParser());

    ?????? registerBeanDefinitionDecorator("set", new PropertyModifyingBeanDefinitionDecorator());

    ?????? registerBeanDefinitionDecorator("debug", new DebugBeanDefinitionDecorator());

    ?????? registerBeanDefinitionDecorator("nop", new NopInterceptorBeanDefinitionDecorator());

    ?????? registerBeanDefinitionDecoratorForAttribute("object-name", new ObjectNameBeanDefinitionDecorator());

    ??? }

    ??? }

    ?

    定義各個解析類:

    privatestaticclass TestBeanDefinitionParser implements BeanDefinitionParser {

    ?????? public BeanDefinition parse(Element element, ParserContext parserContext) {

    ?????????? RootBeanDefinition definition = new RootBeanDefinition();

    ?????????? definition.setBeanClass(TestBean.class);

    ?

    ?????????? MutablePropertyValues mpvs = new MutablePropertyValues();

    ?????????? mpvs.addPropertyValue("name", element.getAttribute("name"));

    ?????????? mpvs.addPropertyValue("age", element.getAttribute("age"));

    ?????????? definition.setPropertyValues(mpvs);

    ?

    ?????????? parserContext.getRegistry().registerBeanDefinition(element.getAttribute("id"), definition);

    ?

    ?????????? returnnull;

    ?????? }

    ??? }

    ??? privatestaticclassPropertyModifyingBeanDefinitionDecorator implements BeanDefinitionDecorator {

    ?????? public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition,

    ???????????????????????????????????????????????????????????????????????????????????????????? ParserContext parserContext) {

    ?????????? Element element = (Element)node;

    ?????????? BeanDefinition def = definition.getBeanDefinition();

    ?

    ?????????? MutablePropertyValues mpvs = (def.getPropertyValues() == null) ?

    ???????????????????????????????????????????????????????????????????????????????????????????? new MutablePropertyValues() : def.getPropertyValues();

    ?????????? mpvs.addPropertyValue("name", element.getAttribute("name"));

    ?????????? mpvs.addPropertyValue("age", element.getAttribute("age"));

    ?

    ?????????? ((AbstractBeanDefinition) def).setPropertyValues(mpvs);

    ?????????? return definition;

    ?????? }

    ??? }

    ??? privatestaticclassDebugBeanDefinitionDecorator extends AbstractInterceptorDrivenBeanDefinitionDecorator {

    ?

    ?????? protected BeanDefinition createInterceptorDefinition(Node node) {

    ?????????? returnnew RootBeanDefinition(DebugInterceptor.class);

    ?????? }

    ??? }

    ??? privatestaticclassNopInterceptorBeanDefinitionDecorator extends

    ?????????????????????????????????????????????? AbstractInterceptorDrivenBeanDefinitionDecorator {

    ?

    ?????? protected BeanDefinition createInterceptorDefinition(Node node) {

    ?????????? returnnew RootBeanDefinition(NopInterceptor.class);

    ?????? }

    ??? }

    ??? privatestaticclassObjectNameBeanDefinitionDecorator implements BeanDefinitionDecorator {

    ?????? public BeanDefinitionHolder decorate(Node node, BeanDefinitionHolder definition,

    ???????????????????????????????????????????????????????????????????????????????????????????? ParserContext parserContext) {

    ?????????? Attr objectNameAttribute = (Attr)node;

    ?????????? definition.getBeanDefinition().setAttribute("objectName", objectNameAttribute.getValue());

    ?????????? return definition;

    ?????? }

    ??? }

    ?

    可以定義EntityResolver,用于驗證相應的xsd

    ?????? privateclass DummySchemaResolver extends PluggableSchemaResolver {

    ?

    ?????? public DummySchemaResolver() {

    ?????????? super(CustomNamespaceHandlerTests.this.getClass().getClassLoader());

    ?????? }

    ?

    ?????? public InputSource resolveEntity(String publicId, String systemId) throws IOException {

    ?????????? InputSource source = super.resolveEntity(publicId, systemId);

    ?????????? if (source == null) {

    ????????????? Resource resource =
    ????????????????????????????????? new ClassPathResource("org/springframework/beans/factory/xml/support/spring-test.xsd");

    ????????????? source = new InputSource(resource.getInputStream());

    ????????????? source.setPublicId(publicId);

    ????????????? source.setSystemId(systemId);

    ?????????? }

    ?????????? return source;

    ?????? }

    ??? }

    關鍵的一步,如何生效:

    ?????? ?????????? String location = "org/springframework/beans/factory/xml/support/customNamespace.properties";

    ?????? NamespaceHandlerResolver resolver = new DefaultNamespaceHandlerResolver(
    ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? getClass().getClassLoader(), location);

    ???????? DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

    ?????? XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(beanFactory);

    ?????? reader.setNamespaceHandlerResolver(resolver);

    ?????? reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);

    ?????? reader.setEntityResolver(new DummySchemaResolver());

    ?????? reader.loadBeanDefinitions(getResource());


    寫一個測試xml文件:

    <?xmlversion="1.0"encoding="UTF-8"?>

    <beansxmlns="http://www.springframework.org/schema/beans"

    ?????????? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    ?????????? ?xmlns:test="http://www.springframework.org/schema/beans/test"

    ?????????? ?xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

    ?????? http://www.springframework.org/schema/beans/testhttp://www.springframework.org/schema/beans/factory/xml/support/spring-test.xsd">

    ??? <test:testBeanid="testBean"name="Rob Harrop"age="23"/>

    ??? <beanid="customisedTestBean"class="org.springframework.beans.TestBean">

    ?????? <test:setname="Rob Harrop"age="23"/>

    ??? </bean>

    ??? <beanid="debuggingTestBean"class="org.springframework.beans.TestBean">

    ?????? <test:debug/>

    ?????? <propertyname="name"value="Rob Harrop"/>

    ?????? <propertyname="age"value="23"/>

    ??? </bean>

    ??? <beanid="chainedTestBean"class="org.springframework.beans.TestBean">

    ?????? <test:debug/>

    ?????? <test:nop/>

    ?????? <propertyname="name"value="Rob Harrop"/>

    ?????? <propertyname="age"value="23"/>

    ??? </bean>

    ??? <beanid="decorateWithAttribute"class="org.springframework.beans.TestBean"test:object-name="foo"/>

    </beans>


    相關的測試方法:

    ?publicvoid testSimpleParser() throws Exception {

    ?????? TestBean bean = (TestBean) this.beanFactory.getBean("testBean");

    ?????? assetTestBean(bean);

    ??? }

    ??? publicvoid testSimpleDecorator() throws Exception {

    ?????? TestBean bean = (TestBean) this.beanFactory.getBean("customisedTestBean");

    ?????? assetTestBean(bean);

    ??? }

    ??? publicvoid testProxyingDecorator() throws Exception {

    ?????? ITestBean bean = (ITestBean) this.beanFactory.getBean("debuggingTestBean");

    ?????? assetTestBean(bean);

    ?????? assertTrue(AopUtils.isAopProxy(bean));

    ?????? Advisor[] advisors = ((Advised) bean).getAdvisors();

    ?????? assertEquals("Incorrect number of advisors", 1, advisors.length);

    ?????? assertEquals("Incorrect advice class.", DebugInterceptor.class, advisors[0].getAdvice().getClass());

    ??? }

    ??? publicvoid testChainedDecorators() throws Exception {

    ?????? ITestBean bean = (ITestBean) this.beanFactory.getBean("chainedTestBean");

    ?????? assetTestBean(bean);

    ?????? assertTrue(AopUtils.isAopProxy(bean));

    ?????? Advisor[] advisors = ((Advised) bean).getAdvisors();

    ?????? assertEquals("Incorrect number of advisors", 2, advisors.length);

    ?????? assertEquals("Incorrect advice class.", DebugInterceptor.class, advisors[0].getAdvice().getClass());

    ?????? assertEquals("Incorrect advice class.", NopInterceptor.class, advisors[1].getAdvice().getClass());

    ??? }

    ??? publicvoid testDecorationViaAttribute() throws Exception {

    ?????? RootBeanDefinition beanDefinition
    ?????????????????????????? = (RootBeanDefinition)this.beanFactory.getBeanDefinition("decorateWithAttribute");

    ?????? assertEquals("foo", beanDefinition.getAttribute("objectName"));

    ??? }

    ??? privatevoid assetTestBean(ITestBean bean) {

    ?????? assertEquals("Invalid name", "Rob Harrop", bean.getName());

    ?????? assertEquals("Invalid age", 23, bean.getAge());

    ??? }

    ?

    posted on 2006-10-27 18:18 布衣郎 閱讀(837) 評論(0)  編輯  收藏 所屬分類: spring

    <2006年10月>
    24252627282930
    1234567
    891011121314
    15161718192021
    22232425262728
    2930311234

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357872
    • 排名 - 156

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲人成自拍网站在线观看| 亚洲乱色熟女一区二区三区丝袜| 亚洲国产美女福利直播秀一区二区 | 无码精品一区二区三区免费视频 | 日本人的色道免费网站| 亚洲视频.com| 99re这里有免费视频精品| 亚洲Aⅴ无码专区在线观看q| 国产一级a毛一级a看免费视频| 国产AV无码专区亚洲AWWW| 久久精品成人免费国产片小草| 在线精品亚洲一区二区三区| 国产精品小视频免费无限app| 亚洲日韩乱码中文无码蜜桃臀网站 | 国产在线不卡免费播放| 国产精品亚洲综合天堂夜夜| 一区国严二区亚洲三区| 一级做a毛片免费视频| 亚洲爆乳无码专区| 88av免费观看| 亚洲卡一卡二卡乱码新区| 国产免费变态视频网址网站| 一级毛片免费播放男男| 亚洲成AV人片在线观看ww| 美丽的姑娘免费观看在线播放| 伊人久久五月丁香综合中文亚洲| 四虎影视在线永久免费看黄 | 中文字幕成人免费视频| 四虎亚洲精品高清在线观看| 免费一看一级毛片人| 全黄大全大色全免费大片| 91亚洲国产成人精品下载| 成人一a毛片免费视频| 人妻仑乱A级毛片免费看| 亚洲av日韩av高潮潮喷无码| 最近中文字幕无吗免费高清| 精品无码一级毛片免费视频观看| 亚洲黄网在线观看| 又爽又黄无遮挡高清免费视频| 久9久9精品免费观看| 亚洲另类自拍丝袜第五页|