Spring.xml 错误配置

Posted

技术标签:

【中文标题】Spring.xml 错误配置【英文标题】:Spring.xml error config 【发布时间】:2018-05-11 00:26:09 【问题描述】:

通过 -cp 运行 spring 项目时出现此错误

java -cp "parser.jar" hu.daniel.hari.learn.spring.orm.main.SpringOrmMain

Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/p]

违规资源:类路径资源[spring.xml]

    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(FailFastProblemReporter.java:70)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:85)
    at org.springframework.beans.factory.parsing.ReaderContext.error(ReaderContext.java:80)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.error(BeanDefinitionParserDelegate.java:309)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateIfRequired(BeanDefinitionParserDelegate.java:1464)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1440)
    at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1428)

我的 beans.xml

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

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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">

<!-- Scans the classpath for annotated components that will be auto-registered 
    as Spring beans -->
<context:component-scan base-package="hu.daniel.hari.learn.spring" />
<!-- Activates various annotations to be detected in bean classes e.g: @Autowired -->
<context:annotation-config />

<!-- <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> <property 
    name="url" value="jdbc:hsqldb:mem://productDb" /> <property name="username" 
    value="sa" /> <property name="password" value="" /> </bean> -->
<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/newparser" />
    <property name="username" value="postgres" />
    <property name="password" value="postgres" />
    <!--<property name="socketTimeout" value="10"/> -->

    <property name="connectionProperties">
        <props>
            <prop key="socketTimeout">10</prop>
        </props>
    </property>
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:packagesToScan="hu.daniel.hari.learn.spring.orm.model"
    p:dataSource-ref="dataSource">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<!-- Transactions -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />

【问题讨论】:

你是不是缺少在最顶层打开和关闭标签? 【参考方案1】:

为速记属性添加命名空间 (p:)

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

【讨论】:

以上是关于Spring.xml 错误配置的主要内容,如果未能解决你的问题,请参考以下文章

配置spring-web.xml文件时报错时

如何为 SQL Server(Windows 身份验证)配置 Spring XML?

Spring xml配置bean

这几天做spring与mybatis整合,配置文件到是会用了,但是否很明白spring.xml 与spring.mvc.xml区别?

Spring xml配置文件相对路径问题

最小化Spring XML配置