Home > Enterprise >  Can not create singleton in Spring Framework
Can not create singleton in Spring Framework

Time:07-21

I'm beginner at Spring Framework. I faced the problem that I cann't create bean with scope Singleton in Spring because there is the problem with next stack trace of error:

/home/roman/Downloads/jdk/jdk-18_linux-x64_bin/data/usr/lib/jvm/jdk-18/bin/java -javaagent:/snap/intellij-idea-community/372/lib/idea_rt.jar=44951:/snap/intellij-idea-community/372/bin -Dfile.encoding=UTF-8 -classpath /home/roman/Desktop/Cafe/target/classes:/home/roman/.m2/repository/org/springframework/spring-context/5.3.22/spring-context-5.3.22.jar:/home/roman/.m2/repository/org/springframework/spring-aop/5.3.22/spring-aop-5.3.22.jar:/home/roman/.m2/repository/org/springframework/spring-expression/5.3.22/spring-expression-5.3.22.jar:/home/roman/.m2/repository/org/springframework/spring-core/5.3.22/spring-core-5.3.22.jar:/home/roman/.m2/repository/org/springframework/spring-jcl/5.3.22/spring-jcl-5.3.22.jar:/home/roman/.m2/repository/org/springframework/spring-beans/5.3.22/spring-beans-5.3.22.jar com.roman.cafe.Administrator
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class org.springframework.beans.factory.BeanCreationException
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:554)
    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:955)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:93)
    at com.roman.cafe.Administrator.main(Administrator.java:8)

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>Cafe</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.22</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.22</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.3.22</version>
        </dependency>
    </dependencies>

</project>

My Configuration Class:

package com.cafe;

import com.cafe.beverages.*;
import com.cafe.clients.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.annotation.Scope;

@Configuration
@PropertySource("properties.properties")
public class MyConfig {
    @Bean
    public Waitress waitressBean(){return new Waitress();}
    @Bean
    @Scope("prototype")
    public Beverage americanoBean(){return new Americano();}
}

My class with main method:

package com.cafe;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.io.FileNotFoundException;

public class Administrator {
    public static void main(String[] args) throws FileNotFoundException {
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(MyConfig.class);
        Waitress waitress = context.getBean("waitressBean",Waitress.class);
        waitress.getOrders();
        context.close();
    }
}

I tried change scope of beans waitressBean and allaBean to prototype and my program was running successfully. So I concluded that the problem is caused by methods waitressBean and allaBean in MyConfig class. I tried to find solution but without result.

I would be grateful for your help.

CodePudding user response:

Thank you to all for your help. I found the solution. The problem was in constructors of beans waitressBean and allaBean because they set initialization of AnnotationConfigApplicationContext field. If move it to separate method but not constructor you can create singletons of these beans so the problem is gone.

CodePudding user response:

Spring create instances of classes by itself, so you shouldn't create objects. Please try change your Waitress bean to follow code:

@Bean
public Waitress waitressBean;
@Bean
public Client allaBean;
  • Related