Enable CrudRepository in Spring java config - java

I'm trying to implement Spring's repositories in my DAL. I'm following this guide.
At the third step of "1.2 Query methods" paragraph we need to activate the repository package scanning with the following XML configuration
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<repositories base-package="com.acme.repositories" />
</beans>
In my project I'm using only java config so this declaration is a little problematic for me. What is the right way to make things done in java conf in this case?
Thank you

You need to add the following annotation to your configuration class:
#EnableJpaRepositories("com.acme.repositories")

Related

Spring MVC schemas and corresponding jar files

I am new to Spring and looking for some help trying to figure what Spring schemas correspond to what jar files in my project class path. I need to know that since I am looking to upgrade to the later version of Spring. The following is the schemas I have in my xml files
in my applicationContext-dataaccess.xml
b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
in my applicationContext-resourcebundle.xml
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
in my applicationContext-security.xml
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">
in my app-servlet.xml
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans"
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
The following is the Spring jar files I see in the class path of my project
\WEB-INF\lib\spring-xml-2.0.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.aop-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.asm-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.beans-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.context.support-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.context-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.core-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.expression-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.jdbc-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.transaction-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.web.servlet-3.1.2.RELEASE.jar"
\WEB-INF\lib\org.springframework.web-3.1.2.RELEASE.jar"
\WEB-INF\lib\spring-security-acl-3.0.5.RELEASE.jar"
\WEB-INF\lib\spring-security-config-3.0.5.RELEASE.jar"
\WEB-INF\lib\spring-security-core-3.0.5.RELEASE.jar"
\WEB-INF\lib\spring-security-taglibs-3.0.5.RELEASE.jar"
\WEB-INF\lib\spring-security-web-3.0.5.RELEASE.jar"
\WEB-INF\lib\spring-ws-core-2.0.2.RELEASE.jar"
\WEB-INF\lib\spring-ws-security-2.0.2.RELEASE.jar"
\WEB-INF\lib\spring-ws-support-2.0.2.RELEASE.jar"
What would be the best approach to upgrade to a later version of Spring ?Take the version names out of the schemas and just copy jar files into the class path of my project? What Spring jar files do I need for my project?
Thank you in advance for any information about this!
Take the version names out of the schemas and just copy jar files into the class path of my project?
Yes. By removing the versions from the files, the schemas will be searched and found in the jar files, since spring is stripping the schema version when building the jars. This way if your dependencies are correct, your configuration XML will be validated against the proper schemas
With the build tool. For what concerns your first and the third question. Using a proper build-managment tool, and using it properly is your best choice, the following answer can illustrate a maven based approach that gives you most flexibility when upgrading.
Without the build tool. On the other hand if you can't use any build tool, there's a dedicated github page explaining how to proceed
Although it is strongly recommended that Spring Framework users take
advantage of the transitive dependency management features of build
systems like Gradle, Maven, and Ivy, some teams cannot use these
tools. This is usually due to corporate restrictions or working with
legacy builds. If you're in this boat, you may want to build a Spring
Framework distribution zip that contains all optional and required
runtime dependencies.

IntelliJ IDEA validates toward wrong XSD

I have defined the following transaction manager:
<tx:annotation-driven transaction-manager="txManager" mode="aspectj" />
and have the following root element:
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
Everything works fine, but IntelliJ gives me a error marker on
mode="aspectj"
saying it is not allowed. I have followed where it gets the xsd from, and it links to the tx 2.0 xsd - which explains the error message, as I need 2.5 to use the mode annotation.
Is it possible to somehow give IntelliJ a hint that i should validate toward 2.5 rather than 2.0?
If you open up the jar file that the schemaLocation should point to the xsd according to this screen shot:
Then you'll see that IntelliJ has a bunch of xsd files for different versions of Spring:
This means that you really have all the schemas you need.
If your bean definitions file has problems then your schemaLocation must point to the wrong version in the Spring jar file:
Check the Settings | Schemas and DTDs and verify that you haven't accidentally manually set it to point to the wrong xsd file:
If it is wrong then you'll have to remove that line using the minus sign. This will cause IntelliJ to go back to it's default values:
After that you should be seeing the same thing as in the first screen shot.
I was having a similar problem trying to use spring security and beans in the same xml config. IntelliJ was insisting on validating using the security xsd version 2.0 despite everything telling it to use version 3.1.
I was able get IntelliJ to figure it out by changing the default namespace between security and beans.
I originally had:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
So I switched it to:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">
After that IntelliJ was validating using the correct xsd. I'm not sure if this was a bug with IntelliJ or something I was doing wrong, but it works now.
Hopefully this helps someone in the future.

Spring expression in xml configuration file

It is useful to have different property sets for different users.
<?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: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">
<context:property-placeholder
location="classpath:/path/to/package/default.properties,
classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>
</beans>
When executing the application, spring does not recognize the expression. The context does not start and spring says: class path resource [path/to/package/#{ systemProperties['user.name'] }.properties] cannot be opened
When I replace the expression manually with a string resulting in a valid resource then the behaviour is as expected. The manual states it should work.
The spring-context and spring-core (3.1.2-RELEASE) are in classpath.
How come spring does not pick up the environment variable?
I'm open to alternate solutions solving the same functional problem.
SpEL expressions are not allowed there; you can do what you want indirectly, though...
<context:property-placeholder properties-ref="props"/>
<util:properties id="props" location="classpath:#{systemProperties['foo']}"/>
Here is the complete answer to the question. Keeping the override of user properties over default properties. My edit of the accepted answer got rejected.
<context:property-placeholder properties-ref="springContextCongifurationProperties"
location="classpath:/path/to/package/default.properties"
local-override="true"/>
<util:properties id="springContextCongifurationProperties"
location="classpath:/path/to/package/#{ systemProperties['user.name'] }.properties"/>

Is there a common place out there contains declarations for all Spring XMLs

I need to write a applicationContext.xml for my Spring Framework 3 application, but I cann't find out its XML Declaration. Anyone could show me place to get it?
Likewise, is there a common place defining declarations for all Spring XMLs? (Or other XMLs being around.)
Below is a declaration example for Spring 2.5 I found out by googling:
<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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
This is all described in the Spring documentation see 3.2.1 Configuration metadata for the basic structure and Appendix C. XML Schema-based configuration for details of the other declarations you could use.

cxf webservice in standalone spring application

I try to implement a webservice in an existing standalone spring application.
I configured spring:
<?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:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath:beans/webservice.xml" />
....
webservice.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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="CheckService" class="test.ws.CheckService">
</bean>
<jaxws:endpoint
id="checkService"
implementor="#CheckService"
address="http://localhost:9000/CheckService" />
I made the configuration according to official documentation and samples from books. I get exception:
03/11/2010 09:34:12 WARN Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'http://cxf.apache.org/schemas/jaxws.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.warning(Unknown Source)
...
http://cxf.apache.org/schemas/jaxws.xsd is accessible from browser and I think it is well formed (official xsd).
I suspect that the problem is not related to cxf rather swing configuration related.
Thanks,
Hubidubi
This means that it could not access the url when you are running it. This happens to us when we start our struts/tiles application without internet connection first. What you can do as an alternative is download the xsd and put it somewhere where you application can read it.
and replace http://cxf.apache.org/schemas/jaxws.xsd with /path/to/file/jaxws.xsd
Could it be that you're in an environment where you need to set up a proxy to access the web?
You have set the proxy in you browser and can thus access the .xsd via browser.
You need to set up your IDE appropriately or set a system-wide proxy.
Hope I guessed right :p
Finally I figured out the resolution. I had to add appropirate dependencies to maven pom.xml.
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.2</version>
</dependency>
for me works w/ this.
Clearing the files from the Eclipse cache fixed this problem for me.
In Eclipse preferences, I went to General > Network Connections > Cache and removed the cxf files that were causing the issue. The error went away the next time I validated the file.

Categories

Resources