Showing posts with label BeanCreationException. Show all posts
Showing posts with label BeanCreationException. Show all posts

Thursday, February 4, 2016

java.lang.LinkageError: loader constraint violation org/slf4j/ILoggerFactory

Getting following exception bringing up a Spring Application on Weblogic 11g while trying to instantiate a Spring Controller.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testController' defined in
URL [zip:D:/weblogic/servers/AdminServer/tmp/_WL_user/cservice-ws-ear-1/fd4930/war/WEB-INF/lib/_wl_cls_gen.jar!/
com/testapp/web/controller/TestController.class]:
Instantiation of bean failed; nested exception is java.lang.LinkageError: loader constraint violation:
when resolving method "org.slf4j.impl.StaticLoggerBinder.getLoggerFactory()Lorg/slf4j/ILoggerFactory;"
the class loader (instance of weblogic/utils/classloaders/ChangeAwareClassLoader) of the current class, org/slf4j/LoggerFactory,
and the class loader (instance of weblogic/utils/classloaders/GenericClassLoader) for resolved class, org/slf4j/impl/StaticLoggerBinder,
have different Class objects for the type org/slf4j/ILoggerFactory used in the signature
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1076)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1021)

Fix:

This is happening due to the conflict between my ear lib and weblogic provided slf4j jars. I found a workaround to run this ear on my local. I set the prefer-web-inf-classes to false in weblogic.xml under WEB-INF

<wls:prefer-web-inf-classes>false</wls:prefer-web-inf-classes>

The above would use the file provided by weblogic even if it exists in ear lib.

Here is sample weblogic.xml

<?xml version="1.0" encoding="UTF-8"?>
<wls:weblogic-web-app xmlns:wls="http://xmlns.oracle.com/weblogic/weblogic-web-app" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee       http://java.sun.com/xml/ns/javaee/javaee_5.xsd       http://xmlns.oracle.com/weblogic/weblogic-web-app       http://xmlns.oracle.com/weblogic/weblogic-web-app/1.3/weblogic-web-app.xsd">

<wls:container-descriptor>
<wls:prefer-web-inf-classes>false</wls:prefer-web-inf-classes>
</wls:container-descriptor>
    <wls:context-root>deskrev</wls:context-root>

</wls:weblogic-web-app>

Caused By: org.springframework.beans.factory.BeanCreationException: Could not autowire field:

Getting following exception bringing up a Spring Application while trying to instantiate a Spring bean. I have defined the bean and autowired it in my spring controller:

Here is my service. Spring should be instantiated when I bring up the application.

@Service("nameFinderService")
@Lazy(true)
public class NameFinderServiceImpl implements INameFinderService {

}

Here is my Controller where I have autowired the above service:

@Controller
public class MainController{

@Autowired
private INameFinderService nameFinderService;

}

Full Stacktrace:

Caused By: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nmservice.web.service.INameFinderService com.nmservice.web.base.controller.MainController.nameFinderService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nmservice.web.service.INameFinderService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:700)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:241)



Fix : The package specified in component-scan in spring configuration was specified to a more specific package as I had my spring files in that package, The new spring bean existed one level higher in package. That was the reason it could not find the bean.

I change the following one level up,

from this
<context:component-scan base-package="com.nmservice.web.base" />
to 
<context:component-scan base-package="com.nmservice.web" />

And the issue got resolved.