简体   繁体   English

如何将参数传递给无法在 Spring 引导中自动装配的 @Component class

[英]How to pass parameter to a @Component class which can't be autowired in Spring Boot

I am building a desktop application using JavaFx and Spring Boot and I am stuck at some problem我正在使用 JavaFx 和 Spring Boot 构建桌面应用程序,但遇到了一些问题

I have @Component class Translator which extends Task<String> and makes an api call to google translation api and I want to pass an autowired bean class and some string parameters such as source and target language to the Translator constructor.我有@Component class Translator ,它扩展了Task<String>并对谷歌翻译 api 进行了 api 调用,我想将自动装配的 bean class 和一些字符串参数(例如源语言和目标语言)传递给Translator构造函数。

@Component
class Translator extends Task<String>{

   private TextPreprocessor textPreprocessor;
   private Stirng strUrl;
   private String from;
   private String to;
   private String text;

   public Translator(@Value("${translator.api.url}" strUrl,TextPreprocessor textPreprocessor)){
      this.strUrl=strUrl;
      this.textPreprocessor=textPreprocessor;
   }

   protected String call() throws Exception{
    //some code 
   }


}
@Component
class TextPreprocessor{
 //some code
}

So my problem is I need to pass parameters from , to and text to Translator constructor but I can't do that here because these varibles can't be autowired.所以我的问题是我需要将参数fromtotext传递给 Translator 构造函数,但我不能在这里这样做,因为这些变量不能自动装配。 How can I fix this?我怎样才能解决这个问题?

According to your requirements the class Translator should not belong to Spring-context and it should not be a spring-bean .根据您的要求, class Translator不应该属于Spring-context ,也不应该是spring-bean Instead it seems that this class should be instantiated manually by you in the code each time you need it so that you can pass dynamically the fields from , to , text .相反,似乎这个 class 应该在您每次需要它时由您在代码中手动实例化,以便您可以动态传递字段fromtotext

In the class where you will manually instantiate Translator instances, you could have autowired strUrl and textPreprocessor as fields, so that they are always available for you to pass in the new instance of Translator that you will create.在您将手动实例化Translator实例的 class 中,您可以将strUrltextPreprocessor自动装配为字段,以便您始终可以使用它们来传递您将创建的Translator的新实例。

There is 1 case where I would see it as a spring-bean and this would be if you moved from , to , text from been fields of this class into been parameters of method call .在 1 种情况下,我会把它看作一个spring-bean ,如果你from这个 class 的 been 字段移动to text到 been parameters of method call This way you could have Translator as a singleton retrieved from Spring-context and you could use dynamic values for from , to , text any time you would like to invoke call method.通过这种方式,您可以将Translator作为 singleton 从Spring-context检索,并且可以在任何时候调用 call 方法时为fromtotext使用动态值。

The whole picture of your question seems like a design problem.你的问题的全貌似乎是一个设计问题。

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

相关问题 Spring Boot @Autowired无法初始化类 - Spring boot @Autowired can't initialise class 如何在 Spring Boot 中实现通用 JPA 存储库 - 它可以自动装配到任何实体/类类型的 spring 服务中 - How to implement Generic JPA Repository in Spring Boot - Which can be autowired into spring services for any entity/class type Spring Boot找不到我的自动装配类(自动装配的成员必须在有效的Spring bean中定义) - Spring Boot can't find my autowired class (Autowired members must be defined in valid Spring bean) Java Spring测试类无法从@Autowired组件访问变量 - Java Spring Test Class Can't access variable from @Autowired component 可以在 spring 引导中的服务 class 中自动连接没有实现的接口 - Can interface without implementation be autowired in service class in spring boot Maven 测试无法解决自动装配的 spring 启动依赖项 - Maven tests can't resolve autowired spring boot dependencies Spring Boot发送邮件无法自动装配Bean - Spring Boot Send Mail can't Autowired Bean 创建一个Spring Boot应用程序并且无法使@autowired工作 - Creating a spring boot app and can't get @autowired to work 我如何在构造函数中为未经过Spring修饰的对象使用autowired参数? - How can I use autowired parameter in a constructor for an object that isn't Spring-ified? Singleton bean如何在不同的地方自动布线 - How Singleton bean can be Autowired in different places spring boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM