如何向Java9模块提供spring@bean(提供与……)

mlmc2os5  于 2021-07-12  发布在  Java
关注(0)|答案(0)|浏览(182)

有没有(干净的)方法在Java9模块中提供一个初始化的Springbean作为服务实现?
我现在想到的最佳解决方案是:
module-info.java模块

module my.module {
   provides Service with ServiceWrapper;
}

服务.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);
    }

}

服务 Package 器.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;
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题