如何为Java 9模块提供Spring @Bean通过...提供...

问题描述

在Java 9模块中是否有(干净)的方法来提供初始化的Spring Bean作为服务实现?

我现在想到的最好的解决方案是:

module-info.java

module my.module {
   provides Service with ServiceWrapper;
}

Service.java

public interface Service {
    void doSomething();
}

ServiceImpl.java

@Component
public class ServiceImpl implements Service,InitializingBean {

    @Autowired SomeDependency dep;

    public void doSomething() {
        ...
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        ServiceWrapper.INSTANCE.set(() -> this);
    }

}

ServiceWrapper.java

public class ServiceWrapper implements Service {

    static final ServiceWrapper INSTANCE = new ServiceWrapper();

    private supplier<Service> supplier;

    void set(supplier<Service> supplier) {
        this.supplier = supplier;
    }

    public void doSomething() {
        this.supplier.get().doSomething();
    }

    public static Service provider() {
        return ServiceWrapper.INSTANCE;
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)