Setting up spring app with spring data repositories and mongo db - java
I am facing an issue when defining mongo repository in application-context.xml
Following is the error i get in xml
Error occured processing XML tried to access method org.springframework.context.annotation.AnnotationConfigUtils.processCommonDefinitionAnnotations
(Lorg/springframework/beans/factory/annotation/AnnotatedBeanDefinition;)V from class org.springframework.data.repository.config.RepositoryComponentProvider'. See Error Log for more details servlet-context.xml /master/WebContent/WEB-INF/config line 24 Spring Beans Problem
I am attaching a screenshot of env for reference.
I am using eclipse Kepler version and pom properties File is like this
<java-version>1.7</java-version>
<org.springframework-version>4.0.1.RELEASE</org.springframework-version>
<org.jackson-version>2.3.0</org.jackson-version>
<spring-data-mongodb>1.4.0.RELEASE</spring-data-mongodb>
Spring data commons version is 1.7
spring data mongo db version 1.4.
I see the error in eclipse project when I open context xml.
Interestingly I have another project that works well.Only difference is that it doesn't have spring MVC and jackson binaries otherwise its similar project.
exception stack trace:
!ENTRY org.springframework.ide.eclipse.beans.core 1 0 2014-03-01
00:04:11.839 !MESSAGE Error occured processing
'/master/WebContent/WEB-INF/config/servlet-context.xml' !STACK 0
java.lang.IllegalAccessError: tried to access method
org.springframework.context.annotation.AnnotationConfigUtils.processCommonDefinitionAnnotations(Lorg/springframework/beans/factory/annotation/AnnotatedBeanDefinition;)V
from class
org.springframework.data.repository.config.RepositoryComponentProvider
at
org.springframework.data.repository.config.RepositoryComponentProvider.findCandidateComponents(RepositoryComponentProvider.java:121)
at
org.springframework.data.repository.config.RepositoryConfigurationSourceSupport.getCandidates(RepositoryConfigurationSourceSupport.java:69)
at
org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport.getRepositoryConfigurations(RepositoryConfigurationExtensionSupport.java:54)
at
org.springframework.data.repository.config.RepositoryConfigurationDelegate.registerRepositoriesIn(RepositoryConfigurationDelegate.java:88)
at
org.springframework.data.repository.config.RepositoryBeanDefinitionParser.parse(RepositoryBeanDefinitionParser.java:67)
at
org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:74)
at
org.springframework.ide.eclipse.beans.core.internal.model.namespaces.DelegatingNamespaceHandlerResolver$ElementTrackingNamespaceHandler.parse(DelegatingNamespaceHandlerResolver.java:177)
at
org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1427)
at
org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$ErrorSuppressingBeanDefinitionParserDelegate.parseCustomElement(BeansConfig.java:1400)
at
org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1417)
at
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:187)
at
org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$ToolingFriendlyBeanDefinitionDocumentReader.doRegisterBeanDefinitions(BeansConfig.java:1330)
at
org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:110)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:494)
at
org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$2.registerBeanDefinitions(BeansConfig.java:402)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:335)
at
org.springframework.ide.eclipse.beans.core.internal.model.BeansConfig$2.loadBeanDefinitions(BeansConfig.java:388)
at
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at
servlet context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Enables the Spring MVC #Controller programming model -->
<mvc:annotation-driven />
<context:component-scan base-package="com.xxxx.yyyyy" />
<!-- Mongo DB Configuration -->
<mongo:mongo id="mongo" host="monopolyvm3" port="27017" />
<mongo:db-factory dbname="test" mongo-ref="mongo" />
<mongo:db-factory id="mongoDbFactory" dbname="cloud" mongo-ref="mongo" />
<mongo:repositories base-package="com.xxxx.yyyyy" />
<bean id="mappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="defaultMongoTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mappingMongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultMongoTypeMapper" />
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingMongoConverter" />
<property name="writeConcern" value="SAFE" />
</bean>
</beans>
error is seen at the following line..
In my case it was spring data jpa version which was causing problem. I am not using spring data mongodb but spring data jpa instead. I started this projected today with latest versions (spring-framework 4.0.2.RELEASE, spring-data-jpa 1.5.0.RELEASE). I ensured that all transitive dependencies from maven (from spring side as well as from spring data side) are that of latest version but no lock.
Following this thread, tried to downgrade spring version to 4.0.0.RELEASE but no luck. I even explored the org.springframework.context.annotation.AnnotationConfigUtils class from spring-context-4.0.2.RELEASE.jar (and also in spring-context-4.0.0.RELEASE.jar) from within my workspace and confirmed that indeed processCommonDefinitionAnnotations is a public method thus IllegalAccessError can not be an issue resulting from these jars.
Finally I downgraded my spring-data-jpa from 1.5.0.RELEASE to 1.4.4.RELEASE and voila all problems are solved on maven update. I am using STS 3.4 if it helps anyone.
Since this was the first post I found on googling this error, thought of posting it here so that others who are facing same problems can potentially solve it with this tip. I have opened bug report at https://jira.springsource.org/browse/DATAJPA-490
#Oliver, tried the dependency management suggested but no luck. I have also added dependency:list output to the bug report as requested.
Finally I changed the spring jar version to 4.0.0 and then removed all spring jars from the maven repository and tried to (updated maven first)build again..It worked. I am pretty sure that it will work with 4.0.1 spring jars also.( I was having another project with the same configuration and it was working fine with 4.0.1 jars:)) I contribute this issue to maven and eclipse. Some issue that I don't have any clue at all.
Make sure you don't accidentally pull in an older Spring version (something before 3.2.5) into the classpath. The older methods don't have the method listed in the exception public. This is then causing the exception. I recommend to use the following Maven config snippet to enforce all Spring libraries to be in 4.0.2:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.0.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
I ran into the same issue. After spending several hours fighting with the problem #Avnish's answer pointed me in the right direction. More specifically, the issue he opened at https://jira.springsource.org/browse/DATAJPA-490 really cleared things up.
Spring-Data-JPA 1.5.1.BUILD-SNAPSHOT or 1.6.0.BUILD-SNAPSHOT has a guard that indicates exactly which jar is causing the problem.
In my case, I was using Eclipse Juno (4.2) with Spring IDE 3.4.0 which was causing the problem. I had to upgrade to Spring IDE 3.5.0 to make the issue go away. I had to use the http://dist.springsource.com/snapshot/TOOLS/nightly/e4.2 update site to update Juno to Spring IDE 3.5.0.
It also appears that STS 3.5.0RC1 and beyond get this working as well.
The real solution is to upgrade your eclipse/STS spring plugin to the latest.
This is mentioned in https://jira.springsource.org/browse/DATAJPA-490, but not that clearly.
Related
Upgrade Shiro to 1.6.0 fails on duplicate EHCache
I try to upgrade Shiro in a spring web application from 1.2.3 to 1.6.0. I have just modified the version in gradle and recompiled the war. But it does not start: Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following: 1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary 2. Shutdown the earlier cacheManager before creating new one with same name. The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ] at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.java:529) I do not use EHCache in Shiro configuration: <import resource="file:${CONFIG_PATH}/ldap-realm.xml"/> <!-- SHIRO Access control --> <bean id="securityManager" class="com.cgi.apps.centaur.engine.access.realm.IPOverridingSecurityManager"> <property name="realms" ref="shiroRealms"/> <property name="cacheManager" ref="memoryConstrainedCacheManager"/> <property name="sessionManager" ref="sessionManager"/> </bean> <bean id="sessionManager" class="com.cgi.apps.centaur.engine.access.realm.IPOverridingSessionManager"> </bean> <bean id="memoryConstrainedCacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager"/> Where is the problem?
I had to explicitely select the packages in gradle. Shiro all imported ehcache module with its own bundled ehcache.xml that was conflicting with my config. compile("org.apache.shiro:shiro-core:${versionShiro}") { exclude group: 'org.sonatype.sisu.inject', module: 'cglib' } compile "org.apache.shiro:shiro-web:${versionShiro}", "org.apache.shiro:shiro-spring:${versionShiro}"
Migrating a project with JasperReports from Spring 4 to Spring 5 [duplicate]
This question already has answers here: Why was Jasper support dropped out from Spring Framework 5.x? (3 answers) Closed 2 years ago. Hi all we are planning to upgrade our project from Java 8 to Java 11. Therefore Spring version of the project shall be changed from Spring 4x to Spring 5x The view resolving was done in Spring 4x as follows 1) spring xml configuration: <bean class="org.springframework.web.servlet.view.XmlViewResolver"> <property name="location" value="/WEB-INF/jasper-view.xml"/> <property name="order" value="0"/> </bean> 2) jasper-view.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:p="http://www.springframework.org/schema/p" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!--here all the url value should contains the valid path for the jrxml file--> <bean id="purchasePdf" class="org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView" p:url="classpath:reports/purchaseReport.jrxml" p:reportDataKey="datasource" /> So with the upgrade Spring version was updated to 5.2.5.RELEASE and jasper version was updated as follows: <dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>6.4.0</version> </dependency> The project is building without and error but gives the following error when deploying in Tomcat server: Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView] for bean with name 'purchasePdf' defined in ServletContext resource [/WEB-INF/jasper-view.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView It seems Spring 5 has dropped it's Jasper support https://github.com/spring-projects/spring-framework/issues/17884 Is there an alternative solution for this?
It recommends to use the native JasperReports API: As a consequence, we rather recommend native use of the JasperReports API in Spring MVC handler methods, generating reports from specifically designed RESTful endpoints. We are dropping our now semi-useless JasperReports view class hierarchy as of Spring Framework 5.0. Note that our existing support against the deprecated JRExporter API remains around in the Spring Framework 4.3.x line until 2019, in particular for existing applications. However, even with 4.3, native use of the JasperReports API is worth considering. I suppose these are part of the challenges / pains of doing an upgrade.
Upgrade from spring 4.3.2 to 5.0.2 is causing missing #AliasFor error
When I try to upgrade a spring web-mvc application from version 4.3.2 to 5.0.2, I get an error in servlet xml. The error happens in the following line, <context:component-scan base-package="com.mypackage" /> Error is as follows, Error occured processing XML '#AliasFor declaration on attribute [value] in annotation [org.springframework.stereotype.Controller] is missing required 'attribute' value.'. See Error Log for more details This is how my servlet xml looks like. <?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.2.xsd"> <security:global-method-security secured-annotations="enabled"/> <context:component-scan base-package="com.mypackage" /> <mvc:view-controller path="/" view-name="index"/> <mvc:resources mapping="/resources/**" location="/resources/" /> </beans> If I use 5.0.0 this error is not happening. As it is complaining about a missing annotation in org.springframework.stereotype.Controller, I tried to find out what is newly added or missing in the Controller. I could see that a new line is added in version 5.0.2 and it has a missing 'attribute'. #AliasFor(annotation = Component.class) I am sure I am missing something in my bean class definition which is required from version 5.0.2.
I'd check for three things. First, you need to make sure that your dependency management is right. You can run mvn dependency:tree and check that all Spring dependencies are using the same version. If you want to avoid issues like this in the future, you can use the Spring Framework BOM and let it manage versions for Spring Framework artifacts: <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> If your dependency management is right, this might be a corrupted JAR. You can clear things and re-download dependencies with mvn dependency:purge-local-repository. Finally, using versioned schemas in your XML is not advised anymore - in fact, it's not supported anymore as of Spring Framework 5.0, see SPR-13499.
javax.naming.ConfigurationException NamingManager.getURLContext cannot find the factory for this scheme: java after migrating to WAS 8.5 and java 1.7
After migrating the application from Java 5 to 7 and WAS 6.1 to WAS 8.5.5.1, while deploying the application, I am getting following exception: Caused by: javax.naming.ConfigurationException: NamingManager.getURLContext cannot find the factory for this scheme: java at com.ibm.ws.naming.util.Helpers.checkForUrlContext(Helpers.java:1631) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:160) at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCtx.java:179) at org.apache.aries.jndi.DelegateContext.lookup(DelegateContext.java:161) at javax.naming.InitialContext.lookup(InitialContext.java:422) at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:124) at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:86) at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:122) at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:147) at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:90) at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:101) at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:164) at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:151) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1057) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1024) This is coming from a spring context file which is inside a Jar which I can't modify. Here is the context file contents: <beans xmlns="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-2.0.xsd"> <bean lazy-init="true" id="test-jndiLocalContext" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="java:comp/env"/> </bean> </beans> I have followed the suggestion given in javax.naming.ConfigurationException after migrating to WAS 8.5 and java 1.7 and in https://www.link-intersystems.com/blog/2014/06/15/how-to-fix-ibm-websphere-jndi-exception-cannot-find-the-factory-for-this-scheme-java/ Changed the web-app version from 2.4 to 2.5 and converted ibm-web-bnd.xmi to ibm-web-bnd.xml file, but no luck. It still gives me the same problem. Looks like the issue is in the Jar file that contains the spring context xml. Can anyone suggest possible advise or solution. Many thanks
Spring's Json not being resolved with appropriate response
I've tried to have a controller in Spring return a JSON response to no avail using the Jackson classes as recommended with 3.0. I've got the jackson jar files(jackson-core-asl-1.5.5.jar & jackson-mapper-asl-1.5.5.jar) in my class path of course. As for the appconfig.xml entries, I'm not sure I need these. I've put them in there as a last act of desperation before returning to ol' fashion non-json ajax. In debug, I watch the controller get the request, return the foo and then, in firebug, get a 406. The error messages are as follows: From the logger when set to debug: org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation From the response: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers (). My appconfig.xml is here: <!-- Configures support for #Controllers --> <mvc:annotation-driven /> <!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="mediaTypes"> <map> <entry key="html" value="text/html"/> <entry key="json" value="application/json"/> </map> </property> <property name="viewResolvers"> <list> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> </list> </property> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"></property> </bean> My controller #RequestMapping(value="foo/bar", method=RequestMethod.GET) public #ResponseBody foo getFoo(#RequestParam String fooId) { return new foo(fooId); } On the jsp, where the ajax call is made: function addRow() { $.getJSON("foo/bar", { fooId: 1 }, function(data) { alert("it worked."); }); } Let me know if there's any more info that is needed.
Get rid of all Jackson beans, and of the json mapping in the negotiating resolver. the mvc:annotation-driven should configure everything you need for the Jackson serialization to work.
Make sure the POJO you return has get()ers, one for each field. Make sure the appserver (Tomcat) has the libraries even if you are sure your build system (Eclipse/Maven) does. I've had this error twice now. Just now I added getters to my pojo. The 406 error went away and I got JSON as expected. I assume that because my fields were package-protected (the default access), it would grab them, but I guess not. For the record, in case it matters, I also made the POJO implement Serializable, toString(), serialVersionUID, no-arg constructor, and explicit constructors. The prior time I cleaned/cleared/refreshed my Tomcat cache and did whatever else to force it to reload. I believe when I added the Jackson dependencies, it fixed my compile time errors, but since tomcat missed them, at runtime Spring MVC did not discover the Jackson libraries, and produced the 406 error about unacceptable response type.
Also, make sure that you add two jackson related jar files. jackson-core-asl-1.9.8.jar jackson-mapper-asl-1.9.8.jar The version can be different.
I know that this is an old thread, but maybe someone will encounter the same problem as me. I got that exception when migrating an application from Spring 4.0.3.RELEASE to Spring 4.1.0.RELEASE. In my case updating Jackson from 1.9.x to 2.4.x did the trick.
I have got same exception when migrating from spring 3.x to spring 4.x. I solved it with updating jackson dependencies from <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> to <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.5.1</version> </dependency> Nothing else was needed for me.
Sending the Accept: application/xml header does not work. But sending Accept: application/json actually works, and jackson mapper kicks in. I got rid of my 406 and got my serialized java object in json format with no more config than #ResponseBody and return new MyObject() :) Thanks skaffman for this information, and thanks Bozho for the working header value :D
This question pops up everywhere on the net and I got bitten by it a couple of times. Spring 3.0.6 (and 5 possibly) has some issues in rendering json. Once I changed to 3.1.0.RELEASE version eveyrything worked AS IS. Without any config changes. Things to note is, the return method must have #ResponseBody (as in the example before) and must be in servlet-context.xml or your spring context configuration file.
May be my answer is a bit late, but it may help some one else visiting this question. I got my problem resolved by adding hibernate-validator-4.0.2 and gave me another exception (class not found exception: org.slf4j.LoggerFactory) which i resolved by adding slf4j-api-1.5.6.jar I hope it will help someone.