我正在一个 spring 应用程序上下文 xml 中配置 jpa 和 jpa 存储库。
如果我同时拥有 <jpa:repositories base-package="..." />
和 <context:component-scan base-package="..." />
然后 Spring Tool Suite 在第 1 行显示错误
Referenced file contains errors (http://www.springframework.org/schema/context/spring-context-4.0.xsd). For more information, right click on the message in
the Problems View and select "Show Details..."
点击显示详细信息后,我得到了这个:
The errors below were detected when validating the file "spring-context-4.0.xsd" viathe file "app-config.xml". In most cases these errors can be detected by validating "spring-context-4.0.xsd" directly. However it is possible that errors will only occur when spring-context-4.0.xsd is validated in the context of app-config.xml.
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,propertyPlaceholder'. line 22
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,property-placeholder'. line 91
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,property-override'. line 155
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,annotation.config'. line 174
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,component-scan'. line 194
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,load-time-weaver'. line 315
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,spring-configured'. line 392
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,mbean-export'. line 405
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,mbean-server'. line 458
sch-props-correct.2: A schema cannot contain two global components with the same name; this schema contains two occurrences of 'http://www.springframework.org/schema/context,filterType'. line 488
在<jpa:repositories base-package="..." />
旁边有一条警告说 Referenced bean 'jpaMapppingContext' not found
运行应用程序似乎工作正常。我可以连接到数据库并使用 JpaRepository 接口(interface)从数据库中获取数据,所以我不确定为什么我会在 STS 中遇到 fatal error 。我假设因为 jpa:repositories 和 context:component-scan 扫描包,当两者都使用时存在一些重复/冲突但我不确定我应该如何修复它(我可能会将服务分成他们的自己的配置 xml 并将其导入应用程序配置或将其添加到 web.xml 中的上下文列表)
这是完整的 app-config.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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:context="http://www.springframework.org/schema/context"
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-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="org.h2.Driver"
p:url="jdbc:h2:tcp://localhost/~/test;INIT=CREATE SCHEMA IF NOT EXISTS TEST"
p:username="sa"
p:password="" />
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:showSql="true" />
<util:map id="jpaProperties">
<entry key="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
<entry key="hibernate.default_schema" value="test" />
</util:map>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
p:dataSource-ref="dataSource"
p:packagesToScan="hr.mrplow.test.model"
p:jpaVendorAdapter-ref="jpaVendorAdapter"
p:jpaProperties-ref="jpaProperties" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory"
p:rollbackOnCommitFailure="true" />
<tx:annotation-driven transaction-manager="transactionManager" />
<jpa:repositories base-package="hr.mrplow.test.DAO" />
<!-- Services -->
<context:component-scan base-package="hr.mrplow.test.service" />
</beans>
这是完整的 app-config.xml
最佳答案
这是 STS 中的一个已知问题:
https://jira.spring.io/browse/DATAJPA-160
虽然这个问题声称已关闭,但您可以在 this forum 中看到它仍然弹出。
您应该考虑在您的案例中使用 Spring 打开一个 jira 票证。
关于java - Spring Tool Suite 应用程序上下文 xml 错误,即使所有 bean 似乎都按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497297/