在Spring中通过AsyncConfigurerSupport创建Bean,遵循最佳实践,使用构造函数。

huangapple go评论67阅读模式
英文:

create Bean in Spring through AsyncConfigurerSupport, respecting best practices with constructor

问题

我知道在Spring中创建@Service的最佳实践是将所有协作者作为final字段,并在构造函数中使用@Autowired。我已经创建了一个这样的服务,现在我需要通过AsyncConfigurerSupport实现将其实例化为一个Bean。

在将所有MyAsyncService的协作者变为final字段之前,这是我的AsyncConfigurerSupport实现:

package com.my.spring.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class SpringConfig extends AsyncConfigurerSupport {
  @Bean
  public MyAsyncService createMyAsyncService() {
    return new MyAsyncServiceImpl();
  }
}

在将所有MyAsyncService的协作者变为final字段后,如何将它们传递给构造函数呢?
谢谢。

英文:

I know that the best practice for creating a @Service in Spring to have all collaborators as final fields and to have the @Autowired in the constructor.
I have created a service like that and I need to init an instance of it as a Bean through an AsyncConfigurerSupport implementation.

Here is my AsyncConfigurerSupport implementation before turning all MyAsyncService collaborators into final fields.

package com.my.spring.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class SpringConfig extends AsyncConfigurerSupport {
  @Bean
  public MyAsyncService createMyAsyncService() {
    return new MyAsyncServiceImpl();
  }
}

How to pass the collaborators to the constructor after turning all MyAsyncService them into final fields?
Thanks

答案1

得分: 1

这可以通过ApplicationContext 这种方式来实现。

package com.my.spring.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class SpringConfig extends AsyncConfigurerSupport {
  private final ApplicationContext context;

  @Autowired
  public SpringConfig(ApplicationContext context) {
    this.context = context;
  }

  @Bean
  public AsyncSendingService asyncSendingService() {
    return context.getBean(AsyncSendingServiceImpl.class);
  }
}
英文:

It can be done through ApplicationContext in this way.

package com.my.spring.app.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import org.springframework.scheduling.annotation.EnableAsync;

@Configuration
@EnableAsync
public class SpringConfig extends AsyncConfigurerSupport {
  private final ApplicationContext context;

  @Autowired
  public SpringConfig(ApplicationContext context) {
    this.context = context;
  }

  @Bean
  public AsyncSendingService asyncSendingService() {
    return context.getBean(AsyncSendingServiceImpl.class);
  }
}

huangapple
  • 本文由 发表于 2023年3月9日 21:51:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定