2024-02-26T15:26:26.316-06:00 WARN 1434 --- [main] o.s.c.s.ClassPathXmlApplicationContext:
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.code2care.demo.Employee#0' defined in class path resource [applicationConfig.xml]:
Could not find an init method named 'cool' on bean with name 'org.code2care.demo.Employee#0'
Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.code2care.demo.Employee#0' defined in class path resource [applicationConfig.xml]:
Could not find an init method named 'cool' on bean with name 'org.code2care.demo.Employee#0'
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:522)
You will get the above exception when you have a bean defined in the spring config (applicationContext.xml) file that has a init-method defined, but no corresponding method in the Java bean.
Example: applicationContext.xml<?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.xsd">
<bean name="employee" class="org.code2care.demo.Employee" init-method="initMethod">
</bean>
</beans>
Employee.java
package org.code2care.demo;
public class Employee {
private int empId;
private String empName;
}
Solution:
To fix this issue, you will need to either remove init-method attribute from the bean Employee, or add the initMethod() method in the Employee class.
package org.code2care.demo;
public class Employee {
private int empId;
private String empName;
public void initMethod() {
//your bean initialization logic
System.out.println("Employee bean init method called...");
}
}
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- C#.Net error The underlying connection was closed: An unexpected error occurred on a send - SharePoint
- [fix] Error response from daemon: conflict unable to remove repository reference ubuntu container is using its referenced image - Docker
- Type R is already defined error : Android Error - Android
- Building library Gradle Project Info: Downloading services.gradle.org - Android-Studio
- Safari appends .html extension to files that are downloaded - Mac-OS-X
- Mac OS X Taking Screen Capture using Terminal - Mac-OS-X
- Fix: ModuleNotFoundError: No module named azure-core - Azure
- How to Indent Python code in Notepad++ - Python