1.XML驗證模式的認識
首先XML的驗證模式有兩種:DTD和XSD。
DTD文檔類型定義,是XML約束模式語言。它是為了保證XML文檔格式正確有效的方法。通過XML文檔和DTD文檔的比較來判斷XML是否符合規范。(現在我很少見,不知道是不是淘汰了)
舉個例子:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//Spring//DTD BEAN 2.0//EN""http://www.Springframework.org/dtd/Spring-beans-2.0.dtd">
<beans>
...
</beans>
XSD(XML Schemas Definition),它描述了XML文檔的結構和規范,可以指定一個XML文檔允許的結構和內容,通過這樣就可以校驗某一個XML文檔是否有效。(這種現在最常見!)
舉個例子:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd">
上面的http://www.springframework.org/schema/beans/spring-beans.xsd,這個命名空間,就指定了xml中bean的驗證方式。 具體的校驗我就不介紹了,每一個命名空間都可以查看對應的校驗內容 。
2.通過ClassPathXmlApplicationContext來認識spring
下面以我們剛接觸spring時,通過ClassPathXmlApplicationContext獲取bean的方式來認識spring源碼。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><bean id="user" class="support.mode.User"><property name="useId" value="00232"></property><property name="userName" value="topsnowwolf"/></bean></beans>
public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-mvc.xml");User user = applicationContext.getBean(User.class);System.out.println(user.getUseId()+":"+user.getUserName());}
認識前先了解類,接口的關系:
現在我們初步認識一下下面這幾個對象:
BeanFactory:定義獲取bean以及bean的各種屬性。
ListableBeanFactory:根據各種條件獲取bean的配置清單。
ResourceLoader:資源加載器
AbstractApplicationContext:這個抽象類的refresh方法為是解析xml,獲取,加載bean的入口。
在實例化ClassPathXmlApplicationContext對象時,會調用ClassPathXmlApplicationContext的構造方法創建對象,在創建
ClassPathXmlApplicationContext時,就會進行一系列的操作。
經過分析入口就是AbstractApplicationContext類的refresh方法。
public void refresh() throws BeansException, IllegalStateException {Object var1 = this.startupShutdownMonitor;synchronized(this.startupShutdownMonitor) {this.prepareRefresh();ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();//獲取BeanFactory配置清單this.prepareBeanFactory(beanFactory);try {this.postProcessBeanFactory(beanFactory);this.invokeBeanFactoryPostProcessors(beanFactory);this.registerBeanPostProcessors(beanFactory);this.initMessageSource();this.initApplicationEventMulticaster();this.onRefresh();this.registerListeners();this.finishBeanFactoryInitialization(beanFactory);this.finishRefresh();} catch (BeansException var9) {if (this.logger.isWarnEnabled()) {this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);}this.destroyBeans();this.cancelRefresh(var9);throw var9;} finally {this.resetCommonCaches();}}}
ConfigurableListableBeanFactory對象的創建。
ConfigurableListableBeanFactory:是BeanFactory配置清單。
ConfigurableListableBeanFactory和DefaultListableBeanFactory的關系圖。
獲取BeanFactory配置清單
通過obtainFreshBeanFactory()方法:
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {this.refreshBeanFactory();//獲取DefaultListableBeanFactory對象。ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();if (this.logger.isDebugEnabled()) {this.logger.debug("Bean factory for " + this.getDisplayName() + ": " + beanFactory);}return beanFactory;}
refreshBeanFactory方法的認識: 由于AbstractApplicationContext類的refreshBeanFactory方法是抽象方法,實現類是AbstractRefreshableApplicationContext。
關系圖:
protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;
protected final void refreshBeanFactory() throws BeansException {if (this.hasBeanFactory()) {this.destroyBeans();this.closeBeanFactory();}try {DefaultListableBeanFactory beanFactory = this.createBeanFactory();beanFactory.setSerializationId(this.getId());//給DefaultListableBeanFactory設置序列化IDthis.customizeBeanFactory(beanFactory);//當我們配置了兩個name屬性相同的Bean時,spring默認會后面配置的Bean會覆蓋掉前面配置的Bean對象this.loadBeanDefinitions(beanFactory);//Object var2 = this.beanFactoryMonitor;synchronized(this.beanFactoryMonitor) {this.beanFactory = beanFactory;}} catch (IOException var5) {throw new ApplicationContextException("I/O error parsing bean definition source for " + this.getDisplayName(), var5);}}
AbstractRefreshableApplicationContext的方法loadBeanDefinitions實現由AbstractXmlApplicationContext。
關系圖:
protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);beanDefinitionReader.setEnvironment(this.getEnvironment());beanDefinitionReader.setResourceLoader(this);beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));this.initBeanDefinitionReader(beanDefinitionReader);this.loadBeanDefinitions(beanDefinitionReader);}
最后loadBeanDefinitions這方法繞來繞去,會調用到XmlBeanDefinitionReader中的loadBeanDefinitions方法。 到現在還沒真正的解析XML,之前的工作全部是準備。是不是很繞啊。
3.Spring解析XML成document的過程
XmlBeanDefinitionReader類中的loadBeanDefinitions就是重點了!!!
大概思路:
xml等配置會被注冊到容器中,xml文件最終都會通過ResourceLoader加重成Resource對象,通Reader進行解析讀取,最后注冊到容器中,spring以sax的方式解析xml。
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {Assert.notNull(encodedResource, "EncodedResource must not be null");if (this.logger.isInfoEnabled()) {this.logger.info("Loading XML bean definitions from " + encodedResource.getResource());}Set<EncodedResource> currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();if (currentResources == null) {currentResources = new HashSet(4);this.resourcesCurrentlyBeingLoaded.set(currentResources);}if (!((Set)currentResources).add(encodedResource)) {throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");} else {int var5;try {InputStream inputStream = encodedResource.getResource().getInputStream();try {InputSource inputSource = new InputSource(inputStream);if (encodedResource.getEncoding() != null) {inputSource.setEncoding(encodedResource.getEncoding());}var5 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());} finally {inputStream.close();}} catch (IOException var15) {throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var15);} finally {((Set)currentResources).remove(encodedResource);if (((Set)currentResources).isEmpty()) {this.resourcesCurrentlyBeingLoaded.remove();}}return var5;}}
第一步:
XmlBeanDefinitionReader類的loadBeanDefinitions(Resource resource)方法將Resource對象封裝成EncodedResource對象。(EncodedResource的作用是對資源文件進行編碼格式處理,處理方法看EncodedResource類的getReader())
public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {return this.loadBeanDefinitions(new EncodedResource(resource));}
public class EncodedResource implements InputStreamSource {private final Resource resource;private final String encoding;private final Charset charset;public EncodedResource(Resource resource) {this(resource, (String)null, (Charset)null);}public EncodedResource(Resource resource, String encoding) {this(resource, encoding, (Charset)null);}public EncodedResource(Resource resource, Charset charset) {this(resource, (String)null, charset);}private EncodedResource(Resource resource, String encoding, Charset charset) {Assert.notNull(resource, "Resource must not be null");this.resource = resource;this.encoding = encoding;this.charset = charset;}
....
第二步:
從EncodedResource中獲取InputStream ,之后將InputStream 轉化為InputSource。
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {Assert.notNull(encodedResource, "EncodedResource must not be null");if (this.logger.isInfoEnabled()) {this.logger.info("Loading XML bean definitions from " + encodedResource.getResource());}Set<EncodedResource> currentResources = (Set)this.resourcesCurrentlyBeingLoaded.get();//通過屬性來記錄已經加載的資源。if (currentResources == null) {currentResources = new HashSet(4);this.resourcesCurrentlyBeingLoaded.set(currentResources);}if (!((Set)currentResources).add(encodedResource)) {throw new BeanDefinitionStoreException("Detected cyclic loading of " + encodedResource + " - check your import definitions!");} else {int var5;try {InputStream inputStream = encodedResource.getResource().getInputStream();//從EncodedResource中獲取InputStream 。try {InputSource inputSource = new InputSource(inputStream);//將InputStream 轉化為InputSource?為何要轉化為InputSource呢?下面解析xml時再解釋。if (encodedResource.getEncoding() != null) {//設置編碼格式inputSource.setEncoding(encodedResource.getEncoding());//}var5 = this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());//進入解析XML的核心代碼} finally {inputStream.close();}} catch (IOException var15) {throw new BeanDefinitionStoreException("IOException parsing XML document from " + encodedResource.getResource(), var15);} finally {((Set)currentResources).remove(encodedResource);if (((Set)currentResources).isEmpty()) {this.resourcesCurrentlyBeingLoaded.remove();}}return var5;}}
第三步:
進入解析XML(小知識 xml的解析方式:jdk的提供的dom解析,dom4j,sax常用的三種。)
由于spring對xml的解釋是采用sax方式進行解析的,所以現在大概知道為什么要將InputStream 轉化為InputSource了吧。因為sax解析xml時,parse方法入參就必須是sax提供的InputSource流對象。
this.doLoadBeanDefinitions(inputSource, encodedResource.getResource());//進入解析XML的核心代碼
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource) throws BeanDefinitionStoreException {try {Document doc = this.doLoadDocument(inputSource, resource);//sax方式解釋xmlreturn this.registerBeanDefinitions(doc, resource);//注冊bean} catch (BeanDefinitionStoreException var4) {throw var4;} catch (SAXParseException var5) {throw new XmlBeanDefinitionStoreException(resource.getDescription(), "Line " + var5.getLineNumber() + " in XML document from " + resource + " is invalid", var5);} catch (SAXException var6) {throw new XmlBeanDefinitionStoreException(resource.getDescription(), "XML document from " + resource + " is invalid", var6);} catch (ParserConfigurationException var7) {throw new BeanDefinitionStoreException(resource.getDescription(), "Parser configuration exception parsing XML from " + resource, var7);} catch (IOException var8) {throw new BeanDefinitionStoreException(resource.getDescription(), "IOException parsing XML document from " + resource, var8);} catch (Throwable var9) {throw new BeanDefinitionStoreException(resource.getDescription(), "Unexpected exception parsing XML document from " + resource, var9);}}
初略說一下:doLoadDocument
protected Document doLoadDocument(InputSource inputSource, Resource resource) throws Exception {return this.documentLoader.loadDocument(inputSource, this.getEntityResolver(), //向SAX 驅動器注冊一個實例EntityResolver,至于為何要一定要這個EntityResolver這個實例給SAX,就不多說了。this.errorHandler, //SimpleSaxErrorHandler對象this.getValidationModeForResource(resource), //XML驗證方式的讀取this.isNamespaceAware() //);}
protected int getValidationModeForResource(Resource resource) {int validationModeToUse = this.getValidationMode();if (validationModeToUse != VALIDATION_AUTO) {//如果手動指定了驗證模式則使用指定的驗證模式return validationModeToUse;} else {//沒有自動檢測int detectedMode = this.detectValidationMode(resource);return detectedMode != VALIDATION_AUTO ? detectedMode : VALIDATION_XSD;}}
調用DefaultDocumentLoader類中的loadDocument解析xml
public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception {DocumentBuilderFactory factory = this.createDocumentBuilderFactory(validationMode, namespaceAware);//創建一個工廠(工廠模式)if (logger.isDebugEnabled()) {logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");}DocumentBuilder builder = this.createDocumentBuilder(factory, entityResolver, errorHandler);//獲取建造者(建造者模式)return builder.parse(inputSource);//解釋XML成Document}
sax如何解析xml成Document這里就不廢話了。
第四步:
將解析的document對應的bean注冊到spring容器中。
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {BeanDefinitionDocumentReader documentReader = this.createBeanDefinitionDocumentReader();//實例化BeanDefinitionDocumentReaderint countBefore = this.getRegistry().getBeanDefinitionCount();//總結已經存在的BeanDefinition個數documentReader.registerBeanDefinitions(doc, this.createReaderContext(resource));//加載注冊bean 核心return this.getRegistry().getBeanDefinitionCount() - countBefore;//返回本次加載的BeanDefinition個數}
這里就是最最最核心的代碼了。
BeanDefinitionDocumentReader是一個接口,而實例化由createBeanDefinitionDocumentReader()完成!
protected BeanDefinitionDocumentReader createBeanDefinitionDocumentReader() {return (BeanDefinitionDocumentReader)BeanDefinitionDocumentReader.class.cast(BeanUtils.instantiateClass(this.documentReaderClass));}
通過這個可以知道,BeanDefinitionDocumentReader的實現類是DefaultBeanDefinitionDocumentReader。 下面我們重點看一下DefaultBeanDefinitionDocumentReader類中的registerBeanDefinitions()方法。
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {this.readerContext = readerContext;this.logger.debug("Loading bean definitions");Element root = doc.getDocumentElement();//獲取document的元素。this.doRegisterBeanDefinitions(root);}
此時我們才真真正正的看到了底層的實現方法。繞來繞去,終于看到了曙光!!!下面我們接著分析doRegisterBeanDefinitions(root)方法。
protected void doRegisterBeanDefinitions(Element root) {BeanDefinitionParserDelegate parent = this.delegate;this.delegate = this.createDelegate(this.getReaderContext(), root, parent);//實例化BeanDefinitionParserDelegate,是一個解析器代理類。if (this.delegate.isDefaultNamespace(root)) {String profileSpec = root.getAttribute("profile");//處理profile屬性,profile來實現動態生成相應的bean,如何實現網上很多例子,這里就不介紹了。https://www.cnblogs.com/yw0219/p/5990056.html,https://www.jianshu.com/p/948c303b2253這兩篇文章介紹得很具體。if (StringUtils.hasText(profileSpec)) {String[] specifiedProfiles = StringUtils.tokenizeToStringArray(profileSpec, ",; ");if (!this.getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {return;}}}this.preProcessXml(root);this.parseBeanDefinitions(root, this.delegate);this.postProcessXml(root);this.delegate = parent;}
此時會發現,這里才是真真正正對XML的每一個節點進行解析! 大略看一下BeanDefinitionParserDelegate的,你發現它實際就是封裝了bean的各種屬性。
public class BeanDefinitionParserDelegate {public static final String BEANS_NAMESPACE_URI = "http://www.springframework.org/schema/beans";public static final String MULTI_VALUE_ATTRIBUTE_DELIMITERS = ",; ";public static final String TRUE_VALUE = "true";public static final String FALSE_VALUE = "false";public static final String DEFAULT_VALUE = "default";public static final String DESCRIPTION_ELEMENT = "description";public static final String AUTOWIRE_NO_VALUE = "no";public static final String AUTOWIRE_BY_NAME_VALUE = "byName";public static final String AUTOWIRE_BY_TYPE_VALUE = "byType";public static final String AUTOWIRE_CONSTRUCTOR_VALUE = "constructor";public static final String AUTOWIRE_AUTODETECT_VALUE = "autodetect";public static final String DEPENDENCY_CHECK_ALL_ATTRIBUTE_VALUE = "all";public static final String DEPENDENCY_CHECK_SIMPLE_ATTRIBUTE_VALUE = "simple";public static final String DEPENDENCY_CHECK_OBJECTS_ATTRIBUTE_VALUE = "objects";public static final String NAME_ATTRIBUTE = "name";public static final String BEAN_ELEMENT = "bean";public static final String META_ELEMENT = "meta";public static final String ID_ATTRIBUTE = "id";public static final String PARENT_ATTRIBUTE = "parent";public static final String CLASS_ATTRIBUTE = "class";public static final String ABSTRACT_ATTRIBUTE = "abstract";public static final String SCOPE_ATTRIBUTE = "scope";private static final String SINGLETON_ATTRIBUTE = "singleton";public static final String LAZY_INIT_ATTRIBUTE = "lazy-init";public static final String AUTOWIRE_ATTRIBUTE = "autowire";public static final String AUTOWIRE_CANDIDATE_ATTRIBUTE = "autowire-candidate";public static final String PRIMARY_ATTRIBUTE = "primary";public static final String DEPENDENCY_CHECK_ATTRIBUTE = "dependency-check";public static final String DEPENDS_ON_ATTRIBUTE = "depends-on";public static final String INIT_METHOD_ATTRIBUTE = "init-method";public static final String DESTROY_METHOD_ATTRIBUTE = "destroy-method";public static final String FACTORY_METHOD_ATTRIBUTE = "factory-method";public static final String FACTORY_BEAN_ATTRIBUTE = "factory-bean";public static final String CONSTRUCTOR_ARG_ELEMENT = "constructor-arg";public static final String INDEX_ATTRIBUTE = "index";public static final String TYPE_ATTRIBUTE = "type";public static final String VALUE_TYPE_ATTRIBUTE = "value-type";public static final String KEY_TYPE_ATTRIBUTE = "key-type";public static final String PROPERTY_ELEMENT = "property";public static final String REF_ATTRIBUTE = "ref";public static final String VALUE_ATTRIBUTE = "value";public static final String LOOKUP_METHOD_ELEMENT = "lookup-method";public static final String REPLACED_METHOD_ELEMENT = "replaced-method";public static final String REPLACER_ATTRIBUTE = "replacer";public static final String ARG_TYPE_ELEMENT = "arg-type";public static final String ARG_TYPE_MATCH_ATTRIBUTE = "match";public static final String REF_ELEMENT = "ref";public static final String IDREF_ELEMENT = "idref";public static final String BEAN_REF_ATTRIBUTE = "bean";public static final String LOCAL_REF_ATTRIBUTE = "local";public static final String PARENT_REF_ATTRIBUTE = "parent";public static final String VALUE_ELEMENT = "value";public static final String NULL_ELEMENT = "null";public static final String ARRAY_ELEMENT = "array";public static final String LIST_ELEMENT = "list";public static final String SET_ELEMENT = "set";public static final String MAP_ELEMENT = "map";public static final String ENTRY_ELEMENT = "entry";public static final String KEY_ELEMENT = "key";public static final String KEY_ATTRIBUTE = "key";public static final String KEY_REF_ATTRIBUTE = "key-ref";public static final String VALUE_REF_ATTRIBUTE = "value-ref";public static final String PROPS_ELEMENT = "props";public static final String PROP_ELEMENT = "prop";public static final String MERGE_ATTRIBUTE = "merge";public static final String QUALIFIER_ELEMENT = "qualifier";public static final String QUALIFIER_ATTRIBUTE_ELEMENT = "attribute";public static final String DEFAULT_LAZY_INIT_ATTRIBUTE = "default-lazy-init";public static final String DEFAULT_MERGE_ATTRIBUTE = "default-merge";public static final String DEFAULT_AUTOWIRE_ATTRIBUTE = "default-autowire";public static final String DEFAULT_DEPENDENCY_CHECK_ATTRIBUTE = "default-dependency-check";public static final String DEFAULT_AUTOWIRE_CANDIDATES_ATTRIBUTE = "default-autowire-candidates";public static final String DEFAULT_INIT_METHOD_ATTRIBUTE = "default-init-method";public static final String DEFAULT_DESTROY_METHOD_ATTRIBUTE = "default-destroy-method";protected final Log logger = LogFactory.getLog(this.getClass());private final XmlReaderContext readerContext;private final DocumentDefaultsDefinition defaults = new DocumentDefaultsDefinition();private final ParseState parseState = new ParseState();private final Set<String> usedNames = new HashSet();......
parseBeanDefinitions()這個方法必須重點認識!!!
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {if (delegate.isDefaultNamespace(root)) {NodeList nl = root.getChildNodes();for(int i = 0; i < nl.getLength(); ++i) {Node node = nl.item(i);if (node instanceof Element) {Element ele = (Element)node;if (delegate.isDefaultNamespace(ele)) {this.parseDefaultElement(ele, delegate);//解析默認的bean} else {delegate.parseCustomElement(ele);//解析自定義的bean}}}} else {delegate.parseCustomElement(root);}}
不同bean的認識:
默認的bean:
<bean id="user" class="support.mode.User"><property name="useId" value="00232"></property><property name="userName" value="topsnowwolf"/></bean>
自定義的bean:
<tx:annotation-driven />
這兩者區別很大,如果是spring默認配置的bean,spring肯定知道如何處理,自定義的就要實現一下接口和配置。 問題spring是如何知道是默認的還是自定義的呢?
public boolean isDefaultNamespace(String namespaceUri) {return !StringUtils.hasLength(namespaceUri) || "http://www.springframework.org/schema/beans".equals(namespaceUri);}public boolean isDefaultNamespace(Node node) {return this.isDefaultNamespace(this.getNamespaceURI(node));}
判斷是默認的還是自定義的,通過標簽對應的命名空間去判斷。
當命名空間是http://www.springframework.org/schema/beans時,就是默認的。下次我將總結一下如何自定義spring的標簽,這樣就能更好的認識自定義的解析,這里就不多說了。不過為了有點了解還是舉個例子吧。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 自動掃描 --><context:component-scan base-package="support.*"/><tx:annotation-driven /><!-- 第一種方式:加載一個properties文件 --><bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="classpath:jdbc.properties"/></bean>
上面這一部分配置:
我們可以看到 context標簽對應的命名空間就是http://www.springframework.org/schema/context。
tx的是http://www.springframework.org/schema/tx。
差不多了!!!
最后總結一下整個過程: