spring-context and spring-web 4.2.4.RELEASE - java

Why I need to include spring-context in my new spring boot project to work ?
I have this in my pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
I can see inside spring-web dependency, the dependency spring-context
if I remove spring-context from my pom.xml, the application does not run.
Below is the error I get :
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'nameOfController'

Do you see the spring-context to be provided with spring-web or really that it just depends on it? You might want to do yourself a favor and use the spring boot starter artefacts that actually come with all required dependencies and don't mark any of them as "provided".
In your case, it would be spring-boot-starter-web.

When we go through the compile dependencies of spring-web , then spring-context is not included in the latest-version.
https://frontbackend.com/maven/artifact/org.springframework/spring-web/5.2.6.RELEASE
Though it was present in the previous versions.
https://frontbackend.com/maven/artifact/org.springframework/spring-web/4.3.20.RELEASE
Since, I cannot see the specific version that you'r adding to your pom.xml file, I am assuming it is getting the latest version, where the dependency of spring-context is not included in spring-web.
Also, the error stack trace that you have provided is very less and generic in nature, so it becomes difficult to debug your problem further, though I think it is a problem with version.
You can give the specific version in this way :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>

Related

Spring MVC 4.2.2 and Hibernate 5.0.2 giving error

Below are the dependency version details on the pom.xml
<!-- spring -->
<org.springframework-version>4.2.2.RELEASE</org.springframework-version>
<org.springframework.data-version>1.11.0.RELEASE</org.springframework.data-version>
<!-- hibernate -->
<org.hibernate.hibernate-core>5.0.2.Final</org.hibernate.hibernate-core>
<org.hibernate.hibernate-annotations>3.5.6-Final</org.hibernate.hibernate-annotations>
<org.hibernate.hibernate-commons-annotations>3.2.0.Final</org.hibernate.hibernate-commons-annotations>
<org.hibernate.hibernate-validator>5.2.2.Final</org.hibernate.hibernate-validator>
I'd picked the Hibernate version details from the "spring-orm" pom and found the Hibernate 5.0.2 is compatible with Spring 4.2.2
With the same dependencies in-case of Spring core and Hibernate it's working fine and giving desired result.
But with Spring MVC and Hibernate integration, it's giving below error
Invocation of init method failed; nested exception is
java.lang.NoSuchMethodError:
org.hibernate.cfg.annotations.reflection.JPAMetadataProvider.(Lorg/hibernate/boot/spi/MetadataBuildingOptions;)V
If any further information is needed, please put in the comment section. I will provide the entire configuration details.
Thanks in Advance. :-)
On removing the below dependencies from pom.xml has resolved the issue.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
Because of this dependencies, there was conflict on the Resolved dependencies.(Refer the pic attached on the question).
hibernate-annotations is not required, It was dependent on older version of hibernate-core and hibernate-commons-annotations
hibernate-commons-annotations (version 5.0.2) is present in hibernate-core as a dependent jar. So it's not required to mention older version in pom.xml

Unresolved Dependency in Maven

Can anyone tell me please what's wrong with my dependency Hamcrest in Maven?
That's a pity but I can't attach screenshot here.
The screenshot from IntelliJ IDEA
One the right side one can see that the dependency hamcrest-all:1.3 is inside dependency junit:4.11.
Maybe something wrong with my pom.xml file?
There is also a problem with hamcrest version 1.3 - dependency hamcrest:hamcrest-all:1.3 not found.
When I try to update Maven indices, nothing happens.
There is a slight difference between the group ids. Notice in the maven repository group id is org.hamcrest
http://mvnrepository.com/artifact/org.hamcrest/hamcrest-all/1.3
Sometimes, not specifying the dependency's version can give this Error
Unresolved Dependency
just specify the version of the dependency
example:
instead of this:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
use this:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.2</version>
</dependency>

java.lang.NoClassDefFoundError: Could not initialize class org.apache.commons.logging.LogFactory

I am new to Google App Engine. I am getting this error :
java.lang.NoClassDefFoundError: Could not initialize class
org.apache.commons.logging.LogFactory at
org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:282) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) at
org.mortbay.jetty.handler.ContextHandler.startContext(ContextHandler.java:548) at
org.mortbay.jetty.servlet.Context.startContext(Context.java:136) at ...
I have added slf4j dependencies and excluded commons-logging in spring-context dependency but still getting this error. The app works perfectly fine on my local machine but gives me this error when deployed to the App Engine.
Thanks to this blog , I was able to resolve this issue :
Commons-logging is a dependency of many frameworks, Spring included. On the local server, everything runs fine. In the cloud, Google App Engine infrastructure replaces the commons-logging-1.1.1.jar with a JAR of its own that has a different package structure. In effect, that means you get funny NoClassDefFoundError on org.apache.commons.logging.LogFactory even though you included the JAR as a dependency. The solution is to still include the classes, but to give the JAR another name.
Since I use Maven, I removed the commons-logging dependency from the WAR with the exclusion tag for Spring and MyFaces artifact. Then, I added a dependency on commons-logging:commons-logging-api:1.1:jar with the runtime scope. This jar won’t be replaced.
So you should exclude commons-logging from Spring :
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Then add a dependency on commons-logging-1.1 with runtime scope:
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
<scope>runtime</scope>
</dependency>
You are missing a required jar in your /war/WEB-INF/lib/ folder. It's not enough to have it in your classpath.
If you use Eclipse, you should see a warning in the Problems tab. Right click on it, Quick Fix, select " Copy ...". Or add this jar to the /lib folder manually.

Do I need to download the dependencies that Spring need in Maven?

I am starting out with Spring and I am reading Pro Spring 2.5. On page 17 they talk about Spring dependencies and I wonder if I need to add this myself in the POM, or does the dependency I have added below do this? Such as CGLib, dom4j etc?
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
You shouldn't need to. Maven will read the pom for spring-context and get any necessary dependencies that it has too, so you won't need to specifically put them in your own pom.
Check out this link it's really handy.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
This will just instruct maven that the current POM in which this is declared is dependent on this artfact, so when you compile the app it would make it available for you in classpath

How do I override the spring framework version in Spring Batch?

I'm starting a new project with Spring Batch 2.1.6.RELEASE and I use Maven for depemdency management.
By default, it imports spring framework 2.5.6, but I'd like to use 3.0.5.RELEASE instead.
This post says it's compatible, but I don't know how to declare that in my maven pom file.
I tried just putting the dependencies for spring-core, spring-beans and spring-context versions 3.0.5.RELEASE, but that adds the libraries to the project without removing the 2.5.6 version.
I had a look at spring-batch-parent pom file, and there is a profile called "spring3" that uses the spring version I want. How do I activate a profile in my project's pom file ?
Thanks in advance,
Philippe
You can exclude the transient dependency on Spring Framework v2.5.6 of Spring Batch by using the dependency exclusions element of the spring-batch dependency in maven. Something like...
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>2.1.6.RELEASE</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<!-- Other exclusions here -->
</exclusions>
</dependency>

Categories