Spring如何自定義XML配置擴展
在Spring中,我們定義一個自己的標簽有如下步驟:
自己定義一個XSD文件。
定義一個和XSD文件所對應的實體類。
創建實現了BeanDefinitionParser的類(其實更好的做法是繼承抽象類AbstractBeanDefinitionParser),去解析我們的自定義標簽。
創建一個繼承了NamespaceHandlerSupport的類,去將我們創建的類注冊到spring容器。編寫自己的Spring.handlers和Spring.schemas
一、定義一個XSD文件
首先我們在resources下創建META-INF目錄。
創建resources/META-INF/model.xsd
<?xml version='1.0'?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns='http://demo1.example.com/schema1' targetNamespace='http://demo1.example.com/schema1'> <xsd:complexType name='billType'> <xsd:attribute name='name' type='xsd:string'> </xsd:attribute> <xsd:attribute name='age' type='xsd:int'> </xsd:attribute> </xsd:complexType> <xsd:element name='bill' type='billType'> </xsd:element></xsd:schema>
首先看到xsd:element這塊,這里面的屬性name就是我們以后標簽的名字,type則指向了上面的標簽xsd:complexType這里,這個標簽里面有兩個子標簽都是xsd:attribute,一個代表string類型的name,另一個代表int類型的age,意思就是bill這個標簽里面有name和age兩個屬性。
再就是要注意最上面的幾行,第二行的xmlns:xsd='http://www.w3.org/2001/XMLSchema'這個是必須的,第三行xmlns='http://demo1.example.com/schema'里面這個url你隨便寫,但是要和第四行的targetNamespace保持一致。
二、定義一個和XSD文件所對應的實體類
public class ModelBean { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; }}
三、實現BeanDefinitionParser,解析標簽
public class BillBeanDefinitionParser implements BeanDefinitionParser { private final Class<?> beanClass; public BillBeanDefinitionParser(Class<?> beanClass) { this.beanClass = beanClass; } @Override public BeanDefinition parse(Element element, ParserContext parserContext) { GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition(); genericBeanDefinition.setBeanClass(beanClass); genericBeanDefinition.setLazyInit(false); genericBeanDefinition.getPropertyValues().add('name', element.getAttribute('name')); genericBeanDefinition.getPropertyValues().add('age', element.getAttribute('age')); parserContext.getRegistry().registerBeanDefinition(beanClass.getName(),genericBeanDefinition); return null; }}
四、繼承NamespaceHandlerSupport,注冊類
public class BillNameSpaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser('bill',new BillBeanDefinitionParser(Model.class)); }}
五、編寫自己的Spring.handlers和Spring.schemas
META-INF/Spring.Handlers
http://demo1.example.com/schema1=com.appst.xmlpc.handler.BillNameSpaceHandlerMETA-INF/Spring.schemas:
http://demo1.example.com/schema1/model.xsd=META-INF/model.xsd
這兩個文件都是properties格式的文件,這兩個文件和開頭的那個xsd都要放在resource目錄下的META-INF文件夾下,再注意Spring.Handlers中的key是要和上面xsd中你自己定義的xmlns一致,value一定要指向你自己定義的NameSpaceHandler的全路徑,Spring.schemas中key前半部分是自己定義的xmlns,后半部分的mytag.xsd就是你自己xsd的文件名。
然后在application-context.xml加上我們的標簽:
<?xml version='1.0' encoding='UTF-8'?><beans xmlns='http://www.springframework.org/schema/beans' xmlns:context='http://www.springframework.org/schema/context' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:billtag='http://demo1.example.com/schema1' xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://demo1.example.com/schema1 http://demo1.example.com/schema1/model.xsd'> <billtag:bill name='bill.li' age='18'/></beans>
然后跑個測試看看:
//指定在單元測試啟動的時候創建spring的工廠類對象@ContextConfiguration(locations = {'classpath:applicationContext.xml'})//RunWith的value屬性指定以spring test的SpringJUnit4ClassRunner作為啟動類//如果不指定啟動類,默認啟用的junit中的默認啟動類@RunWith(value = SpringJUnit4ClassRunner.class)public class SpringTest { @Autowired private ApplicationContext applicationContext; @Test public void testSpring() { ModelBean model = (ModelBean) applicationContext.getBean(ModelBean.class.getName()); System.out.println(model.getAge()); System.out.println(model.getName()); }}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
