Lombok Superbuilder Spring Autowire与儿童课程

发布于 2025-02-02 23:18:44 字数 822 浏览 4 评论 0原文

我有一个父班:

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@SuperBuilder
@Service
public class Parent {}

还有一个子类:

@Profile("integration")
@SuperBuilder
@Service
public class Child extends Parent {}

我仅在这样的集成测试中使用此子类:

@SpringBootTest(
        classes = {
            ...
            Child.class,
            ...
        })
@ActiveProfiles({"integration"})
class ITTest {
    @Autowired
    private Child child;
}

当我尝试运行集成测试时,我在加载应用程序上下文时遇到问题:

通过构造函数参数0表示的不满意的依赖性; 嵌套异常是 org.springframework.beans.factory.nosuchbeandefinitionException:否 合格的豆类 '... service.integration.interfaces.Child $育儿者' 可用:预期至少有1个bean有资格作为Autowire 候选人。依赖项注释:{}

父类在Main,子类中,集成测试正在测试包中。

我该如何解决这个问题?

I have a parent class:

@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@SuperBuilder
@Service
public class Parent {}

And a child class:

@Profile("integration")
@SuperBuilder
@Service
public class Child extends Parent {}

I am using this child class only in an integration test like this:

@SpringBootTest(
        classes = {
            ...
            Child.class,
            ...
        })
@ActiveProfiles({"integration"})
class ITTest {
    @Autowired
    private Child child;
}

When I try to run my integration test, I'm having problem on loading application context:

Unsatisfied dependency expressed through constructor parameter 0;
nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type
'...service.integration.interfaces.Child$ChildBuilder'
available: expected at least 1 bean which qualifies as autowire
candidate. Dependency annotations: {}

Parent class is in under main, child class and integration tests are under test package.

How can I solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2025-02-09 23:18:44

我解决了这个问题:

我删除了整个@superbuilder并由我自己实现的构造函数。还使用了默认的应用程序上下文,因此从@springboottest上删除了类:

@RequiredArgsConstructor
@Service
public class Parent {}


@Service
public class Child extends Parent {

      public Child() {
         super();
    }
}

@SpringBootTest
class ITTest {
    @Autowired
    private Child child;
}

它会自动自动自动化正确的bean。

I have solved the issue:

I removed the whole @SuperBuilder and implemented constructor by myself. Also used default application context, so removed classes from @SpringBootTest:

@RequiredArgsConstructor
@Service
public class Parent {}


@Service
public class Child extends Parent {

      public Child() {
         super();
    }
}

@SpringBootTest
class ITTest {
    @Autowired
    private Child child;
}

It automatically autowired the correct bean.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文