英文:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论