简体   繁体   English

如何在 PostConstruct 时自动装配 spring 中的这种依赖关系

[英]How to autowire this dependency in spring at the time of PostConstruct

I have the below code inside the @PostConstruct block in a spring managed code.我在 spring 托管代码的@PostConstruct块中有以下代码。


class A {

    private BoogleFeature boogle;

    @PostConstruct
        public void createBoggleClient() {
                SDKPersona.SDKPersonaBuilder sdkBuilder =
                        new AppIdentifier.SDKPersonaBuilder()
                                .setRegistryId(config.getRegistryId())
                                .setRegistrySecret(config.getRegistrySecret())
        
                boggle = new BoggleFeature(sdkBuilder.build());
        }
    }
}

Now i want to not do a boggle = new BoggleFeature(sdkBuilder.build());现在我不想做一个boggle = new BoggleFeature(sdkBuilder.build()); and make it as bean and inject it as a deopendency.并将其作为 bean 并将其作为 deopendency 注入。 How can i achieve that.我怎样才能做到这一点。

did you try below, you can put below code in come configuration class你在下面尝试了吗,你可以把下面的代码放在配置 class 中

    @Bean(name="boggleFeature")
    public BoggleFeature createBoggleClient() {
        SDKPersona.SDKPersonaBuilder sdkBuilder =
            new AppIdentifier.SDKPersonaBuilder()
                .setRegistryId(config.getRegistryId())
                .setRegistrySecret(config.getRegistrySecret())
    
        return new BoggleFeature(sdkBuilder.build());
    }
       
    

Then you can use with autowired any where然后你可以在任何地方使用 autowired

 @Autowired
    private BoogleFeature boogle

You can use GenericApplicationContext to register the bean dynamically during @PostConstruct this way:您可以使用GenericApplicationContext@PostConstruct期间以这种方式动态注册 bean:

@Component
public class BoogleFactory {

  @Autowired
  private GenericApplicationContext context;

  @PostConstruct
  public void createBoggleClient() {
    //build the sdk
    String sdk = "Boogle SDK";

    //then register the bean in spring context with constructor args
    context.registerBean(BoogleFeature.class,sdk);
  }

}

The bean to register dynamically:动态注册的bean:

public class BoogleFeature {

  private String sdk;

  public BoogleFeature(String sdk) {
    this.sdk = sdk;
  }

  public String doBoogle() {
    return "Boogling with " + sdk;
  }
}

You can then use, Anywhere in your app:然后,您可以在应用程序中的任何位置使用:

@Component
class AClassUsingBoogleFeature {
   @Autowired
   BoogleFeature boogleFeature; //<-- this will have the sdk instantiated already
}

The following test proves that, after spring context initialization,下面的测试证明,spring上下文初始化后,
(which will initialize BooglerFactory then call the @Postcontruct method BTW), (这将初始化BooglerFactory然后调用@Postcontruct方法顺便说一句),
you can use Boogler with @Autowired anywhere.您可以在任何地方使用带有@AutowiredBoogler

@SpringBootTest
public class BooglerTest {

  @Autowired
  BoogleFeature boogleFeature;

  @Test
  void boogleFeature_ShouldBeInstantiated() {
    assert "Boogling with Boogle SDK".equals(boogleFeature.doBoogle());
  }

}

registerBean has more powerful options, check it out here . registerBean有更强大的选项, 在这里查看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM