Micronaut不会增强工厂定义的豆

发布于 2025-01-21 20:41:40 字数 1274 浏览 0 评论 0原文

直接在服务类上应用@singleton,其中包含@postConstruct和 @transactional Works,按预期 - @postConstruct被调用,交易方法正常工作。

@Singleton
public class MyService {

    @PostConstruct
    public void init() {
       LOG.info("PostConstruct called")
    }

    @Transactional
    public void doSomethingInTransaction() {

    }

}

将bean定义移至@factory中,如下以下仍然有效,MyService Singleton仍在创建,但并未得到增强 - @postConstruct不调用,交易方法无法正常工作(无法获得当前线程的事务同步会话

public class MyService {

    @PostConstruct
    public void init() {
       LOG.info("PostConstruct called")
    }

    @Transactional
    public void doSomethingInTransaction() {

    }

}

@Factory
public class ApplicationConfig {

    @Singleton
    public MyService myService() {
        return new MyService();
    }

}

)与春季相比,微劳纳的预期行为? 有什么方法可以指示微守年处理外部Java中定义的bean配置与自己类别中定义的bean相同的bean?

注意:我注意到,如果我添加@transactionalAdvice,则Micronaut将应用交易管理(尽管在Java doc中指出不应直接使用它) - 但是@postConstruct仍然不起作用。

@Factory
public class ApplicationConfig {

    @Singleton
    @TransactionalAdvice
    public MyService myService() {
        return new MyService();
    }

}

Applying @Singleton directly on a service class, that contains @PostConstruct and @Transactional works as expected - @PostConstruct is called and the transactional method works just fine.

@Singleton
public class MyService {

    @PostConstruct
    public void init() {
       LOG.info("PostConstruct called")
    }

    @Transactional
    public void doSomethingInTransaction() {

    }

}

Moving the bean definition into a @Factory like below still works, MyService singleton is still created but it's not being enhanced - @PostConstruct is not called and transactional method won't work (Could not obtain transaction-synchronized Session for current thread)

public class MyService {

    @PostConstruct
    public void init() {
       LOG.info("PostConstruct called")
    }

    @Transactional
    public void doSomethingInTransaction() {

    }

}

@Factory
public class ApplicationConfig {

    @Singleton
    public MyService myService() {
        return new MyService();
    }

}

Is this an expected behavior of Micronaut compared with Spring?
Is there any way to instruct Micronaut to handle the bean defined in external Java config the same as the ones defined in their own class?

Note: I've noticed that Micronaut will apply transaction management if I add @TransactionalAdvice like below (although in java doc is stated that it shouldn't be used directly) - but @PostConstruct still doesn't work.

@Factory
public class ApplicationConfig {

    @Singleton
    @TransactionalAdvice
    public MyService myService() {
        return new MyService();
    }

}

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

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

发布评论

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

评论(1

挽手叙旧 2025-01-28 20:41:40

预期行为。

您可以在创建实例之后手动执行初始方法,

我们不分析工厂返回的类,而不是出厂方法上的注释,因为您只能为无法控制的类使用工厂(即在第三方罐子里)。在这种情况下,不应该期望这些注释存在,因为如果类是通过DI注入的,那么您就不会使用工厂来创建它。

The behavior is expected.

You can just execute the init method manually after creating the instance

We don't analyze the classes returned by factories beyond what annotations are on the factory method because you should only be using factories for classes that you aren't in control of (ie are in a third party jar). In that case it should not be expected that those annotations exist because if the class is designed for injection via DI then you wouldn't be using a factory to create it.

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