在线等 怎么win10开侠盗猎车手5win7秘籍一点反应都没有

spring 配置文件 beans属性声明部分网络原因 引用不到 saxpasexml exception 解决方法
我的图书馆
spring 配置文件 beans属性声明部分网络原因 引用不到 saxpasexml exception 解决方法
Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:&
Line 10 in XML document from class path resource [app-config.xml]&
nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
然后去看看app-config.xml里的schema声明部分,是这样写的:
&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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd” &
很明显,有关’beans’的声明都放在http://www.springframework.org/schema/beans/spring-beans-3.0.xsd里面了。可能是网络原因,访问不倒导致解析app-config.xml失败,致使程序抛出异常。
打开spring-beans-3.0.1.RELEASE.jar一看,那个xsd文件就在org.springframework.beans.factory.xml包下面,这下子好办了,把xsd文件的引用全改为本地的就行了:
&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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.0.xsd” &
TA的最新馆藏[转]&[转]&[转]&[转]&[转]&[转]&spring&配置文件&--&&xmlns命名空间
spring&xmlns&命名空间可从:&&&根据需求选择&
& & & 1、选择 :
& & & 2、复制浏览器地址
, 粘入 配置文件&xmlns:aop=""中,如果是contest
则&xmlns:contest=""
xmlns="http://www.springframework.org/schema/beans"
&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&xmlns:aop="http://www.springframework.org/schema/aop"
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&xsi:schemaLocation="
& & &&&&&&&&&&&&&&&&&&&&&&&&&&&&http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
&标签属性中
& & & 3、选择对应的版本
,进入后,再次复制浏览器地址,粘入配置文件紧跟在 2 项标签xsi:schemaLocation=“”
标签属性中
&&&&xsi:schemaLocation=“http://www.springframework.org/schema/aop/spring-aop-3.1.xsd”&&
注意:严禁URL后面带 "/"可能会出错
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
&xmlns:mvc="http://www.springframework.org/schema/mvc"&
&xmlns:task="http://www.springframework.org/schema/task"
&xsi:schemaLocation="
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。&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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.1.xsd&http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd&http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.1.xsd"&
(spring.3.1.1)
因为xml文档都有格式,为了spring的配置文件增加的节点能满足要求、合法,所以引入校验该xml的格式文件。
xmlns是xml命名空间的意思,而xmlns:xsi是指xml所遵守的标签规范。
1.xmlns:关于初始化bean的格式文件地址
2.xmlns:xsi:辅助初始化bean
3.xsi:context:关于spring上下文,包括加载资源文件
4.xsi:schemaLocation:用于声明了目标名称空间的模式文档
阅读(...) 评论()下次自动登录
现在的位置:
& 综合 & 正文
Spring配置文件(xml)中的xmlns解析
以前一直没注意spring对xml的解析过程,
特别是xml文件头上的一堆xmlns:
version="1.0"
encoding="UTF-8"?&
&?XML:NAMESPACE
PREFIX = [default]
xsi:schemaLocation="
xmlns:context=""
xmlns:tx=""
xmlns:aop=""
xmlns:xsi=""&
这些命名空间中是怎么解析的,
大概可以分为下面这个步骤:
1. 解析XML, 找到所有的 命名空间 如: http://www.springframework.org/schema/context
2. 在Classpath中查找所有的 spring.handlers 并解析其中配置的 命名空间 与 对应的处理类, 如:
http\://www.springframework.org/schema/aop=org.springframework.aop.config.AopNamespaceHandler
3. 根据查找到的处理Handler去解析配置文件中相应的结点.
而命名空间对应的xsd文件, 则是在 spring.schemas 中指定的, 如:
http\://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd
http\://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd
http\://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
http\://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd
&&&&推荐文章:
【上篇】【下篇】spring.schemas和spring.handlers对xmlns配置文件作用
在很多情况下,我们需要为系统提供可配置化支持,简单的做法可以直接基于Spring的标准Bean来配置,但配置较为复杂或者需要更多丰富控制的
时候,会显得非常笨拙。一般的做法会用原生态的方式去解析定义好的xml文件,然后转化为配置对象,这种方式当然可以解决所有问题,但实现起来比较繁琐,
特别是是在配置非常复杂的时候,解析工作是一个不得不考虑的负担。Spring提供了可扩展Schema的支持,这是一个不错的折中方案,完成一个自定义
配置一般需要以下步骤:
设计配置属性和JavaBean
编写XSD文件
编写NamespaceHandler和BeanDefinitionParser完成解析工作
编写spring.handlers和spring.schemas串联起所有部件
在Bean文件中应用
下面结合一个小例子来实战以上过程
1)设计配置属性和JavaBean
首先当然得设计好配置项,并通过JavaBean来建模,本例中需要配置People实体,配置属性name和age(id是默认需要的)
public&class&People&{&&
&&&&private&String&&&
&&&&private&String&&&
&&&&private&Integer&&&
2)编写XSD文件
为上一步设计好的配置项编写XSD文件,XSD是schema的定义文件,配置的输入和解析输出都是以XSD为契约,本例中XSD如下:
&?xml&version="1.0"&encoding="UTF-8"?&&&
&xsd:schema&&&
&&&&xmlns="http://blog.csdn.net/cutesource/schema/people"&&
&&&&xmlns:xsd="http://www.w3.org/2001/XMLSchema"&&&
&&&&xmlns:beans="http://www.springframework.org/schema/beans"&&
&&&&targetNamespace="http://blog.csdn.net/cutesource/schema/people"&&
&&&&elementFormDefault="qualified"&&&
&&&&attributeFormDefault="unqualified"&&&
&&&&&xsd:import&namespace="http://www.springframework.org/schema/beans"&/&&&
&&&&&xsd:element&name="people"&&&
&&&&&&&&&xsd:complexType&&&
&&&&&&&&&&&&&xsd:complexContent&&&
&&&&&&&&&&&&&&&&&xsd:extension&base="beans:identifiedType"&&&
&&&&&&&&&&&&&&&&&&&&&xsd:attribute&name="name"&type="xsd:string"&/&&&
&&&&&&&&&&&&&&&&&&&&&xsd:attribute&name="age"&type="xsd:int"&/&&&
&&&&&&&&&&&&&&&&&/xsd:extension&&&
&&&&&&&&&&&&&/xsd:complexContent&&&
&&&&&&&&&/xsd:complexType&&&
&&&&&/xsd:element&&&
&/xsd:schema&&
关于xsd:schema的各个属性具体含义就不作过多解释,可以参见
&xsd:element
name="people"&对应着配置项节点的名称,因此在应用中会用people作为节点名来引用这个配置
&xsd:attribute name="name" type="xsd:string"
/&和&xsd:attribute name="age"
type="xsd:int"
/&对应着配置项people的两个属性名,因此在应用中可以配置name和age两个属性,分别是string和int类型
完成后需把xsd存放在classpath下,一般都放在META-INF目录下(本例就放在这个目录下)
3)编写NamespaceHandler和BeanDefinitionParser完成解析工作
下面需要完成解析工作,会用到NamespaceHandler和BeanDefinitionParser这两个概念。具体说来
NamespaceHandler会根据schema和节点名找到某个BeanDefinitionParser,然后由
BeanDefinitionParser完成具体的解析工作。因此需要分别完成NamespaceHandler和
BeanDefinitionParser的实现类,Spring提供了默认实现类NamespaceHandlerSupport和
AbstractSingleBeanDefinitionParser,简单的方式就是去继承这两个类。本例就是采取这种方式:
import&org.springframework.beans.factory.xml.NamespaceHandlerS&&
public&class&MyNamespaceHandler&extends&NamespaceHandlerSupport&{&&
&&&&public&void&init()&{&&
&&&&&&&&registerBeanDefinitionParser("people",&new&PeopleBeanDefinitionParser());&&
其中registerBeanDefinitionParser("people", new
PeopleBeanDefinitionParser());就是用来把节点名和解析类联系起来,在配置中引用people配置项时,就会用
PeopleBeanDefinitionParser来解析配置。PeopleBeanDefinitionParser就是本例中的解析类:
import&org.springframework.beans.factory.support.BeanDefinitionB&&
import&org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionP&&
import&org.springframework.util.StringU&&
import&org.w3c.dom.E&&
public&class&PeopleBeanDefinitionParser&extends&AbstractSingleBeanDefinitionParser&{&&
&&&&protected&Class&getBeanClass(Element&element)&{&&
&&&&&&&&return&People.class;&&
&&&&protected&void&doParse(Element&element,&BeanDefinitionBuilder&bean)&{&&
&&&&&&&&String&name&=&element.getAttribute("name");&&
&&&&&&&&String&age&=&element.getAttribute("age");&&
&&&&&&&&String&id&=&element.getAttribute("id");&&
&&&&&&&&if&(StringUtils.hasText(id))&{&&
&&&&&&&&&&&&bean.addPropertyValue("id",&id);&&
&&&&&&&&}&&
&&&&&&&&if&(StringUtils.hasText(name))&{&&
&&&&&&&&&&&&bean.addPropertyValue("name",&name);&&
&&&&&&&&}&&
&&&&&&&&if&(StringUtils.hasText(age))&{&&
&&&&&&&&&&&&bean.addPropertyValue("age",&Integer.valueOf(age));&&
&&&&&&&&}&&
其中element.getAttribute就是用配置中取得属性值,bean.addPropertyValue就是把属性值放到bean中。
4)编写spring.handlers和spring.schemas串联起所有部件
上面几个步骤走下来会发现开发好的handler与xsd还没法让应用感知到,就这样放上去是没法把前面做的工作纳入体系中的,spring提供了
spring.handlers和spring.schemas这两个配置文件来完成这项工作,这两个文件需要我们自己编写并放入META-INF文件夹
中,这两个文件的地址必须是META-INF/spring.handlers和META-INF/spring.schemas,spring会默认去
载入它们,本例中spring.handlers如下所示:
http\://blog.csdn.net/cutesource/schema/people=study.schemaExt.MyNamespaceHandler
以上表示当使用到名为""的schema引用时,会通过study.schemaExt.MyNamespaceHandler来完成解析
spring.schemas如下所示:
http\://blog.csdn.net/cutesource/schema/people.xsd=META-INF/people.xsd
以上就是载入xsd文件
5)在Bean文件中应用
到此为止一个简单的自定义配置以完成,可以在具体应用中使用了。使用方法很简单,和配置一个普通的spring
bean类似,只不过需要基于我们自定义schema,本例中引用方式如下所示:
&beans&xmlns="http://www.springframework.org/schema/beans"&&
&&&&xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&&&
&&&&xmlns:cutesource="http://blog.csdn.net/cutesource/schema/people"&&
&&&&xsi:schemaLocation="&&
http://www.springframework.org/schema/beans&http://www.springframework.org/schema/beans/spring-beans-2.5.xsd&&
http://blog.csdn.net/cutesource/schema/people&http://blog.csdn.net/cutesource/schema/people.xsd"&&&
&&&&&cutesource:people&id="cutesource"&name="袁志俊"&age="27"/&&&
其中xmlns:cutesource=""
是用来指定自定义schema,xsi:schemaLocation用来指定xsd文件。&cutesource:people
id="cutesource" name="zhijun.yuanzj"
age="27"/&是一个具体的自定义配置使用实例。
最后就可以在具体程序中使用基本的bean载入方式来载入我们的自定义配置对象了,如:
ApplicationContext&ctx&=&new&ClassPathXmlApplicationContext("application.xml");&&
People&p&=&(People)ctx.getBean("cutesource");&&
System.out.println(p.getId());&&
System.out.println(p.getName());&&
System.out.println(p.getAge());&&
cutesource
以上就是一个基于Spring可扩展Schema提供自定义配置支持实战过程,一些复杂应用和技巧还有待挖掘
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 侠盗飞车win10能玩吗 的文章

 

随机推荐