I'm configuring jpa and jpa repositories inside one of spring app context xmls.
If I have both <jpa:repositories base-package="..." /> and <context:component-scan base-package="..." /> then Spring Tool Suite shows an error on line 1 saying
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..."
and upon clicking show details i get this:
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
And beside <jpa:repositories base-package="..." /> there's a warning saying Referenced bean 'jpaMapppingContext' not found
Running the application it seems to be working correctly. I can connect to the db and using JpaRepository interface I'm able to fetch data from the db, so I'm unsure why am I presented with a fatal error in STS. I'm assuming that since both jpa:repositories and context:component-scan scan packages that there is some duplication/conflicts when both are used but I'm unsure on how should I approach to fixing it (I could probably split services into their own config xml and either import it in app config or add it to context list in web.xml)
Here is the app-config.xml in its entirety:
<?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>
Here's the full app-config.xml
This is a known issue in STS:
https://jira.spring.io/browse/DATAJPA-160
Though this issue was claimed to be closed, you can see in this forum that it still pops up.
You should consider opening a jira ticket with Spring with your case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With