Setting up Spring MVC using pom.xml, with slfj logging and leaks - java

Below is what I have in my pom.xml, any comments or improvments? (this is for a spring mvc application)
Do I still need to exclude commons logging to use slf4j?
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework-version}</version>
<exclusions>
<!-- Exclude Commons Logging in favor of SLF4j -->
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework-version}</version>
</dependency>
I've had this in a project like this for about 1 year now, and not sure if things have changed or need updating (especially around setting up the logging).
One thing I hate is when using IntelliJ and jetty (that reloads the project every x seconds), is that I get java out of memory (perm gen) errors and I believe it is because of a leak in the logging in the spring framework.

You should exclude commons-logging only when you have jcl-over-slf4j included. See for more info: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/overview.html#d0e787
About leaks: as far I understand there is not proper fix -- you could increase memory, but it will crash later. This is one of problems which JRebel may help to reduce.

Related

Maven project sourcing old spring dependencies not part of the pom.xml in Eclipse

My maven java project is sourcing versions of spring libraries that are not defined in my pom.xml
My pom.xml does not have any references to v3.0.5 for spring dependencies.
A snippet of my pom.xml:
<properties>
<org.springframework.version>3.2.4.RELEASE</org.springframework.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${org.springframework.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</exclusion>
<exclusion>
<artifactId>spring-webmvc</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
However when I do a mvn clean -U compile package, I am getting v3.0.5 of the spring libraries in my target:
Can someone help me with how I can remove the outdated/ out of version spring libraries which seem to be sourced 'automatically' without being included in my pom please.
Thanks
Add an element in the element by which the problematic jar is getting fetched or included.these unwanted version of the libs are coming as transitive dependency from one of the mentioned dependency entry in your POM.xml. Use you eclipse or IntelliJ dependence tree to din out the jar that is bringing those transitive dependency and then exclude them fro that element .
<project>
...
<dependencies>
<dependency>
<groupId>sample.ProjectA</groupId>
<artifactId>Project-A</artifactId>
<version>1.0</version>
<scope>compile</scope>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>sample.ProjectB</groupId>
<artifactId>Project-B</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</project>
For finding transitive dependency tree / list : How can I view transitive dependencies of a Maven pom.xml file?

MAVEN BUILD FAILURE - Could not resolve dependencies for project XYZ

I am integrating jersey and spring, for which I have following dependencies, but I am unable to build due to following error:
My POM file:
<dependencies>
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
CONSOLE SCREEN SAYS:
Failed to execute goal on project SpringRestMavenCalc: Could not resolve dependencies for project SpringRestMavenCalc:SpringRestMavenCalc:war:0.0.1-SNAPSHOT: Failed to collect dependencies for [com.sun.jersey:jersey-server:jar:1.8 (compile), org.springframework:spring-core:jar:3.0.5.RELEASE (compile), org.springframework:spring-context:jar:3.0.5.RELEASE (compile), org.springframework:spring-web:jar:3.0.5.RELEASE (compile), com.sun.jersey.contribs:jersey-spring:jar:1.8 (compile)]: No versions available for org.springframework:spring-aop:jar:[2.5.2,3) within specified range -> [Help 1]
Is this versions problem ?
try adding maven dependency for spring-aop with the below:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>2.5.2</version>
</dependency>
or
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
i guess one of this should work
No versions available for org.springframework:spring-aop:jar:[2.5.2,3)
Meaning jersey-spring needs spring-aop 2.5<= version < 3
So jersey-spring 1.8 and spring 3.0.5 are incompatible
I would try the following:
Look for newer version of jersey-spring that works with spring 3.0.5.
Maybe jersey-spring3 as suggested by peeskillet above.
Try my luck with spring 3.0.5 by adding exclusions to jersey-spring dependency.
Consult the pom here and including them with version 3.0.5 if needed.
Thanks All for your answers, Learnt lot from your answers.
BUT FOLLOWING CHANGE IN MY POM MADE MY CODE RUN:
Addition of ,
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>

AbstractMethodError in Spring

I'm trying to store data with H2 and hibernate in my Spring project,
and i cant get rid of this:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.AbstractMethodError: ch.qos.logback.classic.Logger.log(Lorg/slf4j/Marker;Ljava/lang/String;ILjava/lang/String;Ljava/lang/Throwable;)V
at org.apache.commons.logging.impl.SLF4JLocationAwareLog.info(SLF4JLocationAwareLog.java:159)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:187)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
my pom file:
http://pastebin.com/hxXyZi9b
Whats wrong?
There must be version mismatch between your mentioned slf4j dependencies and slf4j lib used spring-core in pom.xml.
If you are using eclipse, you can easily check(by searching) in Depedency Hierarchy tab in pom.xml.
From Spring references documentation:
A common choice might be to bridge Spring to SLF4J, and then provide explicit binding from SLF4J to
Log4J. You need to supply 4 dependencies (and exclude the existing commons-logging): the bridge,
the SLF4J API, the binding to Log4J, and the Log4J implementation itself. In Maven you would do that
like this
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.0.BUILD-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

How to Overcome Spring and Jbehave Versioning Problem?

In our project we are using Spring 3.0.0.RELEASE, and now we are trying to add JBehave to our project.
Here the problem is JBehave latest 3.4.5(Release) version uses Spring 2.5.6 (spring-context, spring-test).
then we got problem in dependencies in maven.
is there any solution to continue our project with Spring version 3.0 and Jbehave Spring version 2.5.6 without any conflicts?
JBehave will probably also work with Spring 3.0. You could simply try to exclude the transitive dependency from JBehave. I didn't test this but it should look similar to this:
<dependencyManagement>
<dependencies>
...
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0</version>
<scope>compile</scope>
</dependency>
...
<dependency>
<groupId>org.jbehave</groupId>
<artifactId>jbehave</artifactId>
<version>3.4.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</exclusion>
</exclusions>
</dependency>
....
</dependencies>
</dependencyManagement>

what is debug logging in spring MVC

My spring MVC is not working and i am getting error resource not found.
I have heard about debug logging.
Is it something that i can turn on and i can see more detail that where is the problem or
is it something i need to program in every file only that message will be shown which i hard coded in file
Spring uses the Apache Commons Logging API, which in turn uses either internal Java logging, or log4j (if available). See this part of the docs for a fuller explanation.
"debug logging" refers to the fact that Spring does a lot of verbose logging at "debug level", which is normally not recorded. You can reconfigure your logging, however, to show this level of information if required. Again, see the above link.
In your log4j.properties, set the logging level for Spring to DEBUG, something along the lines of
log4j.logger.org.springframework = DEBUG, <Some appender>
From a personal blog post, the required maven dependencies are:
<properties>
...
<spring.version>3.1.2.RELEASE</spring.version>
<slf4j.version>1.7.1</slf4j.version>
<logback.version>0.9.30</logback.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
</exclusions>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
The above enables Logback. Check the corresponding documentation to set the desired logging level.

Categories