1

我们如何将 Spring 应用程序增量迁移到 Micronaut ?我正在寻找将 Spring Beans 迁移到 Micronaut 的方法。我尝试按照文档从 Spring 应用程序中使用 Micronaut bean,但它不是 wokring

我的示例测试: https ://github.com/luistrigueiros/micronaut-expose-spring-beans/blob/master/src/test/groovy/example/ConsumeBeanSpec.groovy

消费BeanSpec.groovy

import groovy.util.logging.Slf4j
import org.springframework.test.context.ContextConfiguration
import spock.lang.Specification

import javax.inject.Inject

@Slf4j
@ContextConfiguration(classes = [Config])
class ConsumeBeanSpec extends Specification {
    @Inject
    ConsumerBean consumerBean

    def "should be able to do something"(){
        expect:
            consumerBean.doSomething()
    }

}

当前的堆栈跟踪错误:...省略了 36 个公共帧 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“example.ExposedBean”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖关系注释:{} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1509) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104) at org. springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1065) 在 org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) 在 org.springframework。

java.lang.IllegalStateException:无法加载 ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
at org.spockframework.spring.SpringTestContextManager.prepareTestInstance(SpringTestContextManager.java:49)
at org.spockframework.spring.SpringInterceptor.interceptSetupMethod(SpringInterceptor.java:42)
at org.spockframework.runtime.extension.AbstractMethodInterceptor.intercept(AbstractMethodInterceptor.java:28)
at org.spockframework.runtime.extension.MethodInvocation.proceed(MethodInvocation.java:87)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

引起:org.springframework.beans.factory.UnsatisfiedDependencyException:在文件 [C:\tools\micronaut.io\study\micronaut-expose-spring-beans\out\production\classes\ example\ConsumerBean.class]:通过构造函数参数0表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“example.ExposedBean”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖关系注释:{} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) at org. springframework.beans.factory.support。

4

1 回答 1

2

我已经向https://github.com/luistrigueiros/micronaut-expose-spring-beans/pull/1发送了一个 PR,它允许您的测试通过。

作为https://docs.micronaut.io/1.0.0.M1/api/io/micronaut/spring/beans/MicronautBeanProcessor.html的 javadocs ,MicronautBeanProcessor只处理单例 bean。

这是在https://github.com/micronaut-projects/micronaut-core/blob/0108642b11d0c6d7f11bcffc85b357e823c98d42/spring/src/main/java/io/micronaut/spring/beans/MicronautBeanProcessor.java#L109实施的。

javadoc对此很清楚。我们将在用户指南中对此进行一些澄清。

于 2018-06-29T04:48:39.353 回答