How to explain the different behaviour of spring in eclipse and standalone application - eclipse

We have a web application using spring framework which works well. We try to use the same spring context in a standalone application built with maven. We managed to get the context by launching the application within eclipse, a main class instantiates the Spring context with the following lines:
ApplicationContext ap = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
But when we build a jar with maven and launch it with a command line we encounter errors. Here is the command line:
java -jar application.jar
The jar contains a META-INF that defines the classpath generated by maven-jar-plugin.
First spring failed on this error
Unable to locate NamespaceHandler when using context:annotation-config
We solved the problem by merging the different spring.handler files from maven spring jars and putting the result in our application.jar. This is a common solution to this problem but this doesn't explain why this failed in standalone mode but worked in a web app and in eclipse. Eclipse and standalone use the same code (java and spring configuration files).
Now we are facing a second problem, some beans instantiations fail. Some of our beans are declared with XML, some other beans are declared with annotations. Beans declared with annotations are not instantiated in standalone mode, traces in constructors show it, so instantiation of XML beans referencing these beans fails. We didn't solve this problem yet.
We don't understand why we observe a different behavior in eclipse or in standalone mode. What can explain the difference ? How does eclipse call java, how does-it set the classpath ?
Thanks for any response,
Mickaƫl

Related

Facing Spring Roo issue while converting JPA project to non-JPA project

I have a Spring - JPA project generated using Spring Roo. Now for some requirement we are replacing our JPA layer with some other framework (MyBatis) which is not JPA-compliant. I have done the changes and they are working fine also. I have removed all the JPA dependencies from my pom.xml file.
I am facing issues with my JUnit test project which is used to test DAO layer. Spring Roo is looking for the javax.persistence.Entity class which is part of JPA specific jar.
Please find below the error details below:
can't determine annotations of missing type javax.persistence.Entity
when weaving type ****.**.***Test
when weaving classes
when weaving
when batch building BuildConfig[null] #Files=12 AopXmls=#0
[Xlint:cantFindType]
error at (no source information available
Can anyone please advise me how to remove this error without adding a JPA dependency?
You could try to make a push-in from your test .aj file to the related .java file and then remove the necessary code (imports, annotations) that makes that your Spring Roo shell shows that error.
If you are not sure about how to make a push-in, you could read Spring Roo 1.3.2.RELEASE documentation http://docs.spring.io/spring-roo/docs/1.3.2.RELEASE/reference/html/removing.html#removing-step-by-step-1
Regards,

Walkthrough the spring source code

I wanted to walkthrough the spring code. For that I cloned the code and built it using ./import-into-eclipse.sh command. There are some instructions given in README.md file for checking out source in eclipse.
After following above instructions I was able to see the spring code in my eclipse workspace. To refer the spring source from my project I added all of spring projects (like spring-beans, spring-aop etc) in my project's Java build path. I was able to use spring code in my project.
Now the issue is that whenever I run the test cases in my project, I get initialization error. I removed spring projects from my java build path and junit test started working.
I tried to dig into the issue. For that I tried to run some of the test cases of spring projects itself like BeanUtilsTests.
but I got:
Class not found org.springframework.beans.BeanUtilsTests
java.lang.ClassNotFoundException: org.springframework.beans.BeanUtilsTests
I verified that classpath is set:
Don't know what is the issue. Can someone please tell me that what I did wrong.
Is there any better way to view/edit spring code?
Usually the below error,
Class not found org.springframework.beans.BeanUtilsTests
java.lang.ClassNotFoundException: org.springframework.beans.BeanUtilsTests
occurs when the corresponding jar is missing in the classpath. Since you have already added the spring-beans jar manually, I suspect you are using an incompatible version of the spring-beans jar with your spring-boot.
You can check the Maven repo Spring Beans to check the compatible version. But I would suggest using a dependency management tool like Gradle or Maven to include the required dependency jars. Spring-Boot Gradle file

How to View Classpath While Debugging in Eclipse

I'm trying to troubleshoot a GWT-based app I'm writing in Eclipse. It currently uses Spring Framework 3.1.1 and Hibernate 4.1.6 on the back-end side. I'm currently having troubles with the dreaded "javax.validation.ValidationException: Unable to find a default provider" that seems to plague a lot of folks but is caused by different problems. I've tried the various solutions of using different versions of the JSR 303 implementation (e.g. diff. versions of Hibernate Validator) but it doesn't seem to make a difference.
And after debugging, I'm seeing why. Once execution gets to javax.validation.Validation.getValidationProviders():317 (in validation-api-1.0.0.GA), the app (running on an Eclipse internal Jetty server) attempts to read the META-INF/services/javax.validation.spi.ValidationProvider resource from the classpath and comes back empty. I am absolutely certain that the different validator implementations I've put (e.g. hibernate-validator-4.3.0.Final.jar) have that resource and it does contain a value (e.g. org.hibernate.validator.HibernateValidator), but is not appearing to the classloader in question. The way I've included the JAR in the classpath is by adding it to the project's Build Path which seems to add it to the Jetty runtime when I execute the applications.
My question is: Is there a way to view the classpath in Eclipse debug mode visible to a certain classloader? Secondly, does anyone know why the Hibernate Validator's resource is not first and foremost in the classloader that Validation is using?
The webapp classpath is composed by the directory WEB-INF/classes and by all the jars in WEB-INF/lib. If you want a jar to be available at runtime, you must NOT add it to the build path, but to WEB-INF/lib.
Dropping a jar in WebContent/WEB-INF/lib in Eclipse will make it automatically part of the buid path of your webapp, and available at runtime.

Spring IDE / STS : Can I get autocompletions of beans from other files?

When writing XML beans and using "ref=" everything works well as long as beans are declared in one file. But when bean is in other file, I can't get any autocompletion.
Doesn't work in Spring Tools Suite 2.8.1 or Eclipse 3.7.1 with Spring IDE plugin.
Either import the other file or create a config set that groups all the files.
Spring/Eclipse 'referenced bean not found' warning when using <import>?

Eclipse and Tomcat: Unable to start a War project

My eclipse setup for web project worked before. But all of the sudden it stops working.
Eclipse version Build id: 20100218-1602
It is a very standard setup. I have a servers project setup in eclipse. I have defined an tomcat server in it. I was able to run my web project this way.
Suddenly for some reason today I saw this exception:
org.springframework.beans.factory.CannotLoadBeanClassException:
Error loading class [some.company.persistence.dao.hbm.ProductDaoImpl]
for bean with name 'productDao' defined in class path resource [SomeDB.xml]:
problem with class file or dependent class; nested exception is
java.lang.NoClassDefFoundError: org/springframework/orm/hibernate3/support
/HibernateDaoSupport
Based on the message above, can anyone suggest what may be broken?
Looks like the hibernate library is missing from the project's classpath. Figure out which one of the spring jars contains HibernateDaoSupport and add it to your project's classpath.