Home > Net >  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Opensea
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'Opensea

Time:11-03

I am using Java and spring boot. I have 2 files, an @configuration file and a @Service file.

Configuration file:


@Configuration
    public class OpenSearchRestClientConfiguration {
        @Value("${aws.opensearch.domain.endpoint}")
        private String endpoint;
        @Value("${aws.region}")
        private String region;
        @Value("${aws.serviceName}")
        private String myServiceName;
    
    
        @Bean  //I get an error when I add @Bean, but when I remove @Bean, I get no errors.
        public OpenSearchClient OpensearchClient(){
            SdkHttpClient myHttpClient = ApacheHttpClient.builder().build();
    
            AWS4Signer mySigner = new AWS4Signer();
            mySigner.setServiceName(myServiceName);
            mySigner.setRegionName(region);
            return new OpenSearchClient(
                    new AwsSdk2Transport(myHttpClient, endpoint, Region.of(region), AwsSdk2TransportOptions.builder().build()));
    
    
        }
    }  

Service file:

@Service
public class PostsSearchService {

    
    private final OpenSearchRestClientConfiguration myOpenSearchClient;

    public PostsSearchService(OpenSearchRestClientConfiguration openSearchClient){

        this.myOpenSearchClient = openSearchClient;
    }
} 

I get an error when I add @Bean at the top of the OpensearchClient method in OpenSearchRestClientConfiguration class, but when I remove @Bean, I get no errors.

Here is the error I'm getting:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'OpensearchClient' defined in class path resource [com/search_engine_microservice_group/search_engine_microservice/Configuration/OpenSearchRestClientConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.opensearch.client.opensearch.OpenSearchClient]: Factory method 'OpensearchClient' threw exception; nested exception is java.lang.NoClassDefFoundError: jakarta/json/spi/JsonProvider
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:486) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.23.jar:5.3.23]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-2.7.5.jar:2.7.5]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-2.7.5.jar:2.7.5]
    at com.search_engine_microservice_group.search_engine_microservice.SearchEngineMicroserviceApplication.main(SearchEngineMicroserviceApplication.java:10) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.7.5.jar:2.7.5]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.opensearch.client.opensearch.OpenSearchClient]: Factory method 'OpensearchClient' threw exception; nested exception is java.lang.NoClassDefFoundError: jakarta/json/spi/JsonProvider
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.3.23.jar:5.3.23]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ~[spring-beans-5.3.23.jar:5.3.23]
    ... 24 common frames omitted
Caused by: java.lang.NoClassDefFoundError: jakarta/json/spi/JsonProvider
    at java.base/java.lang.ClassLoader.defineClass1(Native Method) ~[na:na]
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1017) ~[na:na]
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:800) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:698) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:621) ~[na:na]
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:579) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    at org.opensearch.client.json.jackson.JacksonJsonpMapper.<init>(JacksonJsonpMapper.java:61) ~[opensearch-java-2.1.0.jar:na]
    at org.opensearch.client.json.jackson.JacksonJsonpMapper.<init>(JacksonJsonpMapper.java:57) ~[opensearch-java-2.1.0.jar:na]
    at org.opensearch.client.json.jackson.JacksonJsonpMapper.<init>(JacksonJsonpMapper.java:68) ~[opensearch-java-2.1.0.jar:na]
    at org.opensearch.client.transport.aws.AwsSdk2Transport.<init>(AwsSdk2Transport.java:156) ~[opensearch-java-2.1.0.jar:na]
    at org.opensearch.client.transport.aws.AwsSdk2Transport.<init>(AwsSdk2Transport.java:99) ~[opensearch-java-2.1.0.jar:na]
    at com.search_engine_microservice_group.search_engine_microservice.Configuration.OpenSearchRestClientConfiguration.OpensearchClient(OpenSearchRestClientConfiguration.java:38) ~[classes/:na]
    at com.search_engine_microservice_group.search_engine_microservice.Configuration.OpenSearchRestClientConfiguration$$EnhancerBySpringCGLIB$$606bb8d1.CGLIB$OpensearchClient$0(<generated>) ~[classes/:na]
    at com.search_engine_microservice_group.search_engine_microservice.Configuration.OpenSearchRestClientConfiguration$$EnhancerBySpringCGLIB$$606bb8d1$$FastClassBySpringCGLIB$$549e6b4b.invoke(<generated>) ~[classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.3.23.jar:5.3.23]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331) ~[spring-context-5.3.23.jar:5.3.23]
    at com.search_engine_microservice_group.search_engine_microservice.Configuration.OpenSearchRestClientConfiguration$$EnhancerBySpringCGLIB$$606bb8d1.OpensearchClient(<generated>) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:566) ~[na:na]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.3.23.jar:5.3.23]
    ... 25 common frames omitted
Caused by: java.lang.ClassNotFoundException: jakarta.json.spi.JsonProvider
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
    ... 50 common frames omitted

I get an error when I add @Bean at the top of the OpensearchClient *method *in **OpenSearchRestClientConfiguration **class, but when I remove the @Bean annotation, I get no errors.

What is the reason behind such error ?

CodePudding user response:

When you add @Bean, it tells Spring to add bean by bean configuration, as you can see in the docbean-configuration.When you remove @Bean, the method OpensearchClient() will not be executed. And the reason you get the error ClassNotFoundException, is maybe that you forget to import package jakarta-json or the version of jakarta-json might be wrong.

CodePudding user response:

 //private final OpenSearchRestClientConfiguration myOpenSearchClient;     

 private final OpenSearchClient myOpenSearchClient;
  • Related