我正在一个spring app context xmls中配置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接口我可以从数据库中获取数据,所以我不确定为什么我在STS中出现致命错误。我假设既然jpa:存储库和上下文:组件扫描扫描包在使用两者时都存在一些重复/冲突,但我不确定我应该如何解决它(我可能会将服务分成他们的拥有config xml并将其导入app config或将其添加到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
答案 0 :(得分:4)
这是STS中的一个已知问题:
https://jira.spring.io/browse/DATAJPA-160
虽然声称此问题已被关闭,但您可以在this forum中看到它仍然会弹出。
你应该考虑用你的案子打开一张带有Spring的jira票。