SEVERE: Exception sending context initialized event to listener instance of class

SEVERE: Exception sending context initialized event to listener instance of class

This issue typically appear during upgrading java from java 1.7 to java 1.8, and for Java 8 spring 3 does’t work, spring 4 upgrade is compulsory.

 

Exception :

			




SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in ServletContext resource [/WEB-INF/Spring-servlet.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:548)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:725)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:403)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4994)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5492)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.lang.Class.getDeclaredMethods(Unknown Source)
    at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:570)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:489)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:502)
    at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:473)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:241)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1059)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1032)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1471)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538)
    ... 20 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.engine.FilterDefinition
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    ... 41 more

Solution
Hibernate 3
———————————
<bean id=”transactionManager”
        class=”org.springframework.orm.hibernate3.HibernateTransactionManager”>
        <property name=”sessionFactory” ref=”sessionFactory” />
</bean>
    
<bean id=”sessionFactory”
    class=”org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean”>
</bean>

Hibernate 4
———————————
<bean id=”transactionManager”
        class=”org.springframework.orm.hibernate4.HibernateTransactionManager”>
        <property name=”sessionFactory” ref=”sessionFactory” />
</bean>

<bean id=”sessionFactory”
    class=”org.springframework.orm.hibernate4.LocalSessionFactoryBean”>
</bean>

pom.xml changes

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>4.0.0.Final</version>
</dependency>

Lambda Expressions in java – A Java 8 feature

What are Lambda Expression – Java 8 new feature?

Lambda expressions refers to what we had anonymous classes in the older versions of java.

So the very next straight forward thing comes to our mind, if already java is having anonymous classes why a replacement for the same is needed?

Why are Lambda Expressions needed, even if anonymous classes already exist?

The answer goes very simple, Lambda expressions when used makes our code more lighter as compared to when we use anonymous classes.

For Example:

Consider there is a class called Person
To sort the Person class by name if we use anonymous class we have to use the following code

Collections.sort(list, new Comparator() {
  @Override
  public int compare(Person p1, Person p2) {
       return p1.getName().compareTo(p2.getName());
  }
 });

But if we use Lambda expressions it will reduce to just 1 line, as show below,

Collections.sort(list, (Person p1, Person p2) -> p1.getName().compareTo(p2.getName()));

reduces lot of bulky codes.

Lets Discuss one example in detail:

We all are aware of Service Oriented Architecture if not please go through the tutorial 

Calling services will always be time consuming (in general all network calls are considered slow), so we handle those requests asynchronously. If the call is asynchronous then in order to get the response from the service we expect a call back to be called which will return us the result.

The following example demonstrates rather tries to simulate such a scenario,
Consider the following class “ThreadCallableClass” which will make the call to service,

//CallBack.java
package com.dhi.callback;

@FunctionalInterface
public interface CallBack {
	void operationCompelte(String args);
}


//ThreadCallableClass.java

package com.dhi.thread;

import java.util.concurrent.Callable;

import com.dhi.callback.CallBack;

public class ThreadCallableClass implements Callable{
	
	public ThreadCallableClass(CallBack callBack) {
		this.callBack = callBack;
	}

	CallBack callBack;

	String str;
	
	public String getStr() {
		return str;
	}

	public void setStr(String str) {
		this.str = str;
	}
	
	@Override
	public String call() throws Exception {
		System.out.println("service is getting called with the value: " + str);
		//code to call the service to fetch some details
		callBack.operationCompelte(str);
		return "return " + str;
	}

}

//MAIN.JAVA

package com.dhi.main;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.dhi.thread.ThreadCallableClass;

public class MainClass {

	public static void main(String[] args) {
		List list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add("dhi" + i);
		}

		ArrayBlockingQueue arrayBlockingQueue = new ArrayBlockingQueue(
				1000);
		ExecutorService executorService = new ThreadPoolExecutor(10, 100, 20,
				TimeUnit.SECONDS, arrayBlockingQueue);
		for (String eachStr : list) {
			ThreadCallableClass callable = new ThreadCallableClass((String test1) -> {System.out.println(" call back getting called for the service for "+ test1);});
			callable.setStr(eachStr);
			executorService.submit(callable);
		}
		System.out.println("after thread pool");
		executorService.shutdown();
	}

}

//Person.java
package com.dhi.main;

import java.io.Serializable;

public class Person implements Serializable{
	
	String name;
	String mob;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMob() {
		return mob;
	}
	public void setMob(String mob) {
		this.mob = mob;
	}

}


Download Code
DhiJava8LambdaFeature

Spring3 Issue with java8

Spring3 Issue with java8

Most common problem every one face during upgrading java to java8

Problem: All is well in your java spring3 web application, suddenly you start getting,






java.lang.IllegalArgumentException
at org.springframework.asm.ClassReader.(Unknown Source)
at org.springframework.asm.ClassReader.(Unknown Source)
at org.springframework.asm.ClassReader.(Unknown Source)
at org.springframework.core.type.classreading.SimpleMetadataReader.(SimpleMetadataReader.java:52)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:76)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:298)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.getImports(ConfigurationClassParser.java:300)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:230)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:153)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:130)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:285)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:223)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:630)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:461)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)


			




Solution:

This issue is due to update of java to java 8, Following are the solutions for the same,

  1. You still intend to use java8 then spring must be upgraded to 4 or
  2. Change back the version of java to java7.

Steps to change in eclipse,

  1. Right click on the project and goto properties.
  2. Select “Java Build Path”.
  3. Goto “Libraries” tab.
  4. Select JRE System Lib (sometimes the name will mislead, it will be showing java7 but internally it will be pointing to java8).
  5. Click on “Edit” button present on the right side.
  6. Select “Alternate JRE” and click on “Installed JRE”.
  7. Click on “Add” è “Standard VM”.
  8. Click on “Directory” and select the JRE7 home.
  9. An entry for jre7 will be present, check the check box present against jre7.
  10. Click “Ok”
  11. Click on “Finish”.
  12. Click “Ok” again.
  13. Click on “Project” menu and then “Clean”
  14. Wait till the build completes.

You are good with all now again.