How to Solve Error Message Could not find an init method named ‘init’ on bean with name ‘beanName’ in Java Spring Application

Posted on

Introduction

This following article is focusing on how to solve an error message appear upon executing a simple Java Spring Application as an example. Actually, this article has connection with several previous articles. Those articles are mainly an article which is using init method for executing certain process throughout the life cycle of a Java Spring Bean. First of all, it is an article with the title of ‘How to Use Init and Destroy Method in Spring Java Application’ in this link. The next one is an article with the title of ‘How to Define Init and Destroy as Default Method in Spring Java Application’ in this link. Furthermore, there is a base application which is becoming the reference where the following error message appear. It is exist in an article with the title of ‘How to Create a Simple Java Spring Application using NetBeans IDE’ in this link.

So, the following is the output of the compile process execution of the Java Spring Application trying to demonstrate the life cycle of a Java Spring Bean using init and destroy method :

--------------------------< com.test:spring >---------------------------
Building spring 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ spring ---
Sep 13, 2021 7:59:41 PM org.springframework.context.support.AbstractApplicationContext refresh
WARNING: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeBean' defined in class path resource [spring-config.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.support.BeanDefinitionValidationException: Could not find an init method named 'init' on bean with name 'employeeBean'
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeBean' defined in class path resource [spring-config.xml]: Invocation of init method failed; nested exception is org.springframework.beans.factory.support.BeanDefinitionValidationException: Could not find an init method named 'init' on bean with name 'employeeBean'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:144)
    at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85)
    at com.test.spring.SpringMain.main(SpringMain.java:18)
Caused by: org.springframework.beans.factory.support.BeanDefinitionValidationException: Could not find an init method named 'init' on bean with name 'employeeBean'
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1877)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1854)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    ... 12 more
------------------------------------------------------------------------
BUILD FAILURE
------------------------------------------------------------------------
Total time:  1.829 s
Finished at: 2021-09-13T19:59:41+07:00
------------------------------------------------------------------------
Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:exec (default-cli) on project spring: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.
Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Solve the Error Message Could not find an init method named ‘init’

This is a simple case where the error triggering is because there is no method in the bean with the name of ‘init’. It is obvious in the error message appear above. The following is the actual error message providing information :

Could not find an init method named 'init' on bean with name 'employeeBean'

The process for creating the Java Spring Bean with the name of ’employeeBean’ ends in a failure. After carefully looking at the source. The main reason which is causing the error message finally appear. The first step for solving the error message is by looking to the Java Spring Configuration file. The following is the content of the Java Spring Configuration file which exist in ‘src/main/resources’ with the name of ‘spring-config.xml’ as an example :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="employeeBean" class="com.test.spring.Employee" init-method="init" destroy-method="destroy">
     </bean>
</beans>

There is no problem with the content of the ‘spring-config.xml’ acts as a Java Spring Configuration file in the Java Spring Application. But after looking to the Java Spring Bean class with the name of ‘Employee’, the problem exist. The following is the actual content which triggering the error message :

package com.test.spring;
public class Employee {

    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public void identifyID(){
        System.out.println("I am an "+this.id+" !");
    }
}

The problem lies on the non-existent method of ‘init’. Declaring the init-method for the associated Java Spring Bean in the Java Spring Configuration file means there must be an init method also in the Java class file of the Java Spring Bean. In this example, declaring the init method in the Employee Java class file. So, modify the above content of the Java class file as follows :

package com.test.spring;
public class Employee {

    private String id;

    public void setId(String id) {
        this.id = id;
    }
    public void init(){
    }
    public void identifyID(){
        System.out.println("I am an "+this.id+" !");
    }
}

As simple as that, although it is just a simple method without anything inside of it.

Leave a Reply