7

I'm trying to make a stack Spring + Spring Data + Log4J2 work. Unfortunately, when deploying the artefact on My Glassfish 4.1 Server, the following error occurs :


org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class com.canal.config.spring.ContextConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory com.canal.config.spring.ContextConfig.entityManagerFactory()] threw exception; nested exception is java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf(Ljava/lang/String;I)V
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:601)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1113)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1008)
    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.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.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:535)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:244)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1583)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1382)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5704)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:5946)
    at com.sun.enterprise.web.WebModule.start(WebModule.java:691)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:1041)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:1024)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:747)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:2286)
    at com.sun.enterprise.web.WebContainer.loadWebModule(WebContainer.java:1932)
    at com.sun.enterprise.web.WebApplication.start(WebApplication.java:139)
    at org.glassfish.internal.data.EngineRef.start(EngineRef.java:122)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.persistence.EntityManagerFactory com.canal.config.spring.ContextConfig.entityManagerFactory()] threw exception; nested exception is java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf(Ljava/lang/String;I)V
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:590)
    ... 69 more
Caused by: java.lang.NoSuchMethodError: org.jboss.logging.Logger.debugf(Ljava/lang/String;I)V
    at org.hibernate.internal.NamedQueryRepository.checkNamedQueries(NamedQueryRepository.java:149)
    at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:764)
    at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:495)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
    at com.canal.config.spring.ContextConfig.entityManagerFactory(ContextConfig.java:52)
    at com.canal.config.spring.ContextConfig$$EnhancerBySpringCGLIB$$db0b8696.CGLIB$entityManagerFactory$0()
    at com.canal.config.spring.ContextConfig$$EnhancerBySpringCGLIB$$db0b8696$$FastClassBySpringCGLIB$$bca83b68.invoke()
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
    at com.canal.config.spring.ContextConfig$$EnhancerBySpringCGLIB$$db0b8696.entityManagerFactory()
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
    ... 70 more

My pom.xml is :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.springapp</groupId>
    <artifactId>advance</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>advance</name>

    <properties>
        <java.version>1.8</java.version>
        <springframework.version>4.2.1.RELEASE</springframework.version>
        <log4j.version>2.4</log4j.version>
        <javaee.version>7.0</javaee.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <!-- Utilise Log4J à la place du logger par défaut -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <!-- Utilise Log4J à la place du logger par défaut -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
            <exclusions>
                <!-- Utilise Log4J à la place du logger par défaut -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Log4J2 Dependance -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.4</version>
        </dependency>
        <!-- Bridge Log Spring vers Log4J -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>3.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.9.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>${javaee.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.1.Final</version>
        </dependency>


    </dependencies>

    <build>
        <finalName>${artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Does anyone have a clue ?

Florian Tavares
  • 243
  • 2
  • 3
  • 6

6 Answers6

7

hibernate-entitymanager brought in jboss-logging as a dependency, but it is not the most up-to-date version and does not have that method implemented. It looks like you do not need jboss-logging, so try excluding it:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>5.0.1.Final</version>
    <exclusions>
        <exclusion>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Or if it turns out that you need jboss-logging for hibernate, then add a later version of jboss-logging as an additional dependency.

<dependency>
    <groupId>org.jboss.logging</groupId>
    <artifactId>jboss-logging</artifactId>
    <version>3.3.0.Final</version>
</dependency>
LucasP
  • 1,425
  • 12
  • 22
  • 5
    Thank you @LucasP. Your solution didn't work, the jar in target/{artifact}/WEB-INF/lib was already the good version. I solved the problem by remplacing a jar under glassfish\modules by the jar of the target folder. I think Glassfish had an outdated version of the library. But, I don't understand why Glassfish tries to use his own jar when there is the same embedded in the application. Shouldn't be easier if he used the one downloaded by Maven ? – Florian Tavares Oct 07 '15 at 19:22
  • I've never used Glassfish, so I wasn't aware of that possibility. It sounds very similar to a problem I had with JBoss in the past. In the case of JBoss, it is possible to add your updated jar in a separate location and reference it from your application configuration instead of updating it globally. – LucasP Oct 07 '15 at 20:33
  • There are a number of questions similar to this with answers all over the place and none of them worked for me. Your second suggestion of using a newer version did. Thank you so much. My IDE is Netbeans and deploying on tomcat 3.0.27 – OrganicLawnDIY Sep 21 '18 at 04:04
7

I had a similar issue, I was able to fix it by creating a glassfish-web.xml file in the WEB-INF directory. The contents of the file are shown below:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
     <class-loader delegate="false"/>
</glassfish-web-app>

This ensures that glassfish does not load it's internal libraries, but libraries from your project.

Goke Obasa
  • 2,004
  • 1
  • 16
  • 22
6

The method org.jboss.logging.Logger.debugf is only available in the jboss-logging version 3.3.0, if you have this dependency in your project also you have to check that your GlassFish server also have the same version. I had the same error message and I solved it replacing the jboss-logging.jar at the glassfish installation folder because this jar is a previous version and it does not have the needed method. For example, I have the jar in the following path:

C:\Program Files\glassfish-4.1.1\glassfish\modules\jboss-logging.jar

I deleted the old jar and I put the new jar with same name but with the version 3.3.0. Just restart the GlassFish server and it will take the new jar version and that's all.

AngelAvila
  • 418
  • 5
  • 14
2

This problem was caused by conflict in JBoss modules jars. Replacing the file jboss-logging with its latest version solved the problem for me. The file is in the folder modules\system\layers\base\org\jboss\logging\main

Also, you should replace the name of the jar in the file module.xml with the new jar name. The file is located in the same folder.

Ahmed Tawila
  • 789
  • 2
  • 13
  • 19
1

I found the steps at this article useful in resolving the error. http://sanjayingole.blogspot.in/2015/10/caused-by-javalangnosuchmethoderror.html

Steps to follow in short ( If using Eclipse ) --

  1. Check for ALL "jboss-logging" sub-dependencies in the dependency hierarchy in pom.xml, and exclude all those dependencies.

     <dependency> 
         <groupId>org.hibernate</groupId> 
         <artifactId>hibernate-entitymanager</artifactId> 
        <version>${hibernate.version}</version>
        <exclusions>            
            <exclusion>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>
            </exclusion>
        </exclusions>
     </dependency>
    
  2. Use the jboss-logging version which does have the method. As suggested on the link, 3.2.0.Final does.

     <dependency>
                <artifactId>jboss-logging</artifactId>
                <groupId>org.jboss.logging</groupId>            
                <version>3.2.0.Final</version>
     </dependency>
    
Keshav
  • 121
  • 2
  • 5
1

If you are using glassfish jersey then bean-validator comes as dependency and tomcat certainly try to load Logger class from there.

The bean-validator clas also contains Logger class in same package.

Exclude the jar should resolve the issue

<dependency>
            <groupId>org.glassfish.jersey.ext</groupId>
            <artifactId>jersey-spring3</artifactId>
            <version>2.24.1</version>
            <exclusions>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>bean-validator</artifactId>
                    <groupId>org.glassfish.hk2.external</groupId>
                </exclusion>
            </exclusions>
        </dependency>
Tome
  • 2,948
  • 3
  • 30
  • 34