Micronaut Bean Events

2023-02-27 17:30 更新

您可以使用以下接口之一创建 bean 钩子:

  • BeanInitializedEventListener - 允许在设置属性之后但在 @PostConstruct 事件挂钩之前修改或替换 bean。
  • BeanCreatedEventListener - 允许在 bean 完全初始化并调用所有 @PostConstruct 挂钩后修改或替换 bean。

BeanInitializedEventListener 接口通常与工厂 bean 结合使用。考虑以下示例:

 Java Groovy  Kotlin 
public class V8Engine implements Engine {
    private final int cylinders = 8;
    private double rodLength; // (1)

    public V8Engine(double rodLength) {
        this.rodLength = rodLength;
    }

    @Override
    public String start() {
        return "Starting V" + getCylinders() + " [rodLength=" + getRodLength() + ']';
    }

    @Override
    public final int getCylinders() {
        return cylinders;
    }

    public double getRodLength() {
        return rodLength;
    }

    public void setRodLength(double rodLength) {
        this.rodLength = rodLength;
    }
}

@Factory
public class EngineFactory {

    private V8Engine engine;
    private double rodLength = 5.7;

    @PostConstruct
    public void initialize() {
        engine = new V8Engine(rodLength); // (2)
    }

    @Singleton
    public Engine v8Engine() {
        return engine;// (3)
    }

    public void setRodLength(double rodLength) {
        this.rodLength = rodLength;
    }
}

@Singleton
public class EngineInitializer implements BeanInitializedEventListener<EngineFactory> { // (4)
    @Override
    public EngineFactory onInitialized(BeanInitializingEvent<EngineFactory> event) {
        EngineFactory engineFactory = event.getBean();
        engineFactory.setRodLength(6.6);// (5)
        return engineFactory;
    }
}
class V8Engine implements Engine {
    final int cylinders = 8
    double rodLength // (1)

    @Override
    String start() {
        return "Starting V$cylinders [rodLength=$rodLength]"
    }
}

@Factory
class EngineFactory {
    private V8Engine engine
    double rodLength = 5.7

    @PostConstruct
    void initialize() {
        engine = new V8Engine(rodLength: rodLength) // (2)
    }

    @Singleton
    Engine v8Engine() {
        return engine // (3)
    }
}

@Singleton
class EngineInitializer implements BeanInitializedEventListener<EngineFactory> { // (4)
    @Override
    EngineFactory onInitialized(BeanInitializingEvent<EngineFactory> event) {
        EngineFactory engineFactory = event.bean
        engineFactory.rodLength = 6.6 // (5)
        return engineFactory
    }
}
class V8Engine(var rodLength: Double) : Engine {  // (1)

    override val cylinders = 8

    override fun start(): String {
        return "Starting V$cylinders [rodLength=$rodLength]"
    }
}

@Factory
class EngineFactory {

    private var engine: V8Engine? = null
    private var rodLength = 5.7

    @PostConstruct
    fun initialize() {
        engine = V8Engine(rodLength) // (2)
    }

    @Singleton
    fun v8Engine(): Engine? {
        return engine// (3)
    }

    fun setRodLength(rodLength: Double) {
        this.rodLength = rodLength
    }
}

@Singleton
class EngineInitializer : BeanInitializedEventListener<EngineFactory> { // (4)
    override fun onInitialized(event: BeanInitializingEvent<EngineFactory>): EngineFactory {
        val engineFactory = event.bean
        engineFactory.setRodLength(6.6) // (5)
        return engineFactory
    }
}
  1. V8Engine 类定义了一个 rodLength 属性

  2. EngineFactory 初始化 rodLength 的值并创建实例

  3. 创建的实例作为 Bean 返回

  4. 实现BeanInitializedEventListener接口监听工厂的初始化

  5. 在 onInitialized 方法中,rodLength 在工厂 bean 创建引擎之前被覆盖。

BeanCreatedEventListener 接口通常用于修饰或增强完全初始化的 bean,例如通过创建代理。

Bean 事件侦听器在类型转换器之前初始化。如果您的事件侦听器通过配置属性 bean 或任何其他机制依赖类型转换,您可能会看到与类型转换相关的错误。


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号