当类从 MicronautRequestHandler 扩展时出现 BeanInstantiationException

问题描述 投票:0回答:1

当要初始化从

MicronautRequestHandler
扩展的 bean 时,我会递归地遇到以下错误。但其他普通豆子却并非如此。

  • Java版本:17
  • Micronaut:3.10.0(我尝试了4.1.0并降级到较低版本进行检查)

我的 Handler 类的简化版本如下: (为了简化,我还删除了所有注入的服务)

package org.example;

import com.amazonaws.services.lambda.runtime.events.S3Event;
import io.micronaut.function.aws.MicronautRequestHandler;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

@Singleton
public class Handler extends MicronautRequestHandler<S3Event, Void> {

    @Inject
    public Handler() {
        // NOP
    }

    @Override
    public Void execute(S3Event input) {
        System.out.println("Received S3 event: " + input);
        return null;
    }
}

为了在没有 Lambda 的情况下在本地重新创建它,我创建了这个 main 方法。 这是主 LocalRunner 类的最小版本,它仍然会导致问题。

   package org.example;

import io.micronaut.context.ApplicationContext;

public class LocalRunner {
    public static void main(String[] args) {
        // Start the Micronaut application context
        try (ApplicationContext context = ApplicationContext.run()) {
            // Instantiate your handler class
            Handler handler = context.getBean(Handler.class);
        }
    }

}

当部署到 Lambda 并使用

S3Event
触发时,我遇到了同样的错误。

我的构建gradle文件:

plugins {
    id("application")
}

version = "0.1"
group = "org.example"

repositories {
    mavenCentral()
    maven { url "https://s01.oss.sonatype.org/content/repositories/releases/" }
}

dependencies {
    annotationProcessor("io.micronaut:micronaut-http-validation:3.10.0")
    annotationProcessor("io.micronaut:micronaut-inject-java:3.10.0")
    implementation('io.micronaut.aws:micronaut-function-aws:3.18.0')
    implementation("io.micronaut:micronaut-inject:3.10.0")
    implementation("io.micronaut:micronaut-http-server-netty:3.10.0")
    implementation("io.micronaut:micronaut-json-core:3.10.0")
    implementation("io.micronaut:micronaut-jackson-databind:3.10.0")
    implementation("jakarta.inject:jakarta.inject-api:2.0.1")
    implementation("com.amazonaws:aws-lambda-java-events:3.11.0")
    implementation("software.amazon.awssdk:s3:2.29.0")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.16.0")
    implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.16.0")

    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    implementation 'org.slf4j:slf4j-api:1.2.13'
    //https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl
    runtimeOnly("org.apache.logging.log4j:log4j-to-slf4j:2.18.0") 

    //JDBC
    implementation 'com.amazon.redshift:redshift-jdbc42:2.1.0.9'
    implementation 'commons-dbcp:commons-dbcp:1.4'

}
configurations {
    all*.exclude group: 'ch.qos.logback'
    all*.exclude group: 'org.apache.logging.log4j', module: 'log4j-to-slf4j'
}
java {
    sourceCompatibility = JavaVersion.toVersion("17")
    targetCompatibility = JavaVersion.toVersion("17")
}

错误日志的一部分(重复多次):

Path Taken: new Handler()
    ... 1024 more
Caused by: io.micronaut.context.exceptions.BeanInstantiationException: Bean 
definition [org.example.Handler] could not be loaded:

//...

    at org.example.Handler.<init>(Handler.java:12)
    at org.example.$Handler$Definition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
    at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
    at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
    at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
    at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2746)
    at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
    at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
    at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
    at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
    at io.micronaut.function.executor.AbstractExecutor.startEnvironment(AbstractExecutor.java:124)
    at io.micronaut.function.aws.MicronautRequestHandler.buildApplicationContext(MicronautRequestHandler.java:222)
    at io.micronaut.function.aws.MicronautRequestHandler.<init>(MicronautRequestHandler.java:108)
    at org.example.Handler.<init>(Handler.java:12)
    at org.example.$Handler$Definition.build(Unknown Source)
    at io.micronaut.context.DefaultBeanContext.resolveByBeanFactory(DefaultBeanContext.java:2354)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2305)
    at io.micronaut.context.DefaultBeanContext.doCreateBean(DefaultBeanContext.java:2251)
    at io.micronaut.context.DefaultBeanContext.createRegistration(DefaultBeanContext.java:3016)
    at io.micronaut.context.SingletonScope.getOrCreate(SingletonScope.java:80)
    at io.micronaut.context.DefaultBeanContext.findOrCreateSingletonBeanRegistration(DefaultBeanContext.java:2918)
    at io.micronaut.context.DefaultBeanContext.loadContextScopeBean(DefaultBeanContext.java:2746)
    at io.micronaut.context.DefaultBeanContext.initializeContext(DefaultBeanContext.java:1915)
    at io.micronaut.context.DefaultApplicationContext.initializeContext(DefaultApplicationContext.java:249)
    at io.micronaut.context.DefaultBeanContext.readAllBeanDefinitionClasses(DefaultBeanContext.java:3326)
    at io.micronaut.context.DefaultBeanContext.finalizeConfiguration(DefaultBeanContext.java:3684)
    at io.micronaut.context.DefaultBeanContext.start(DefaultBeanContext.java:341)
    at io.micronaut.context.DefaultApplicationContext.start(DefaultApplicationContext.java:194)
    at io.micronaut.function.executor.AbstractExecutor.startEnvironment(AbstractExecutor.java:124)
    at io.micronaut.function.aws.MicronautRequestHandler.buildApplicationContext(MicronautRequestHandler.java:222)
    at io.micronaut.function.aws.MicronautRequestHandler.<init>(MicronautRequestHandler.java:108)
    at org.example.Handler.<init>(Handler.java:12)

resolveByBeanFactory()
再次尝试初始化构造函数,因此递归地循环下去。

更新

我能够通过 3 个简单的步骤使用新的 IntelliJ-Micronaut 默认项目(Java 17、Micronaut 4.4.2)重新创建该问题。

  1. 将这 2 个依赖项附加到您的 build.gradle
implementation('io.micronaut.aws:micronaut-function-aws')
implementation("com.amazonaws:aws-lambda-java-events:3.11.0")
  1. 添加上面简单的Handler类
  2. 将这两行添加到 Micronaut.run 之后的
    Application.java
public class Application {

   public static void main(String[] args) {
       Micronaut.run(Application.class, args);
       try (ApplicationContext context = ApplicationContext.run()) {
           Handler handler = context.getBean(Handler.class);
       }
   }
}
java micronaut micronaut-aws
1个回答
0
投票

我终于发现问题了。一个小小的错误导致了这一切:-(

切勿在 Lambda Handler 类上使用

@Singleton

原因如下:

在 Micronaut 中,@Singleton 将类注册为应用程序上下文中的 bean,这对于服务和其他组件非常有效。但是,Lambda Handler 类(此处扩展了 MicronautRequestHandler)不应该是单例,因为它的处理方式与其他 bean 不同。在处理程序上使用 @Singleton 可能会导致初始化问题并导致依赖项注入失败。

解决方案:

只需从处理程序类中删除 @Singleton 注释,然后让 Micronaut 将其作为 Lambda 函数而不是应用程序 bean 来处理。

© www.soinside.com 2019 - 2024. All rights reserved.