简体   繁体   English

@Value 字段、Lombok 和构造函数注入的最佳实践?

[英]Best practice for @Value fields, Lombok, and Constructor Injection?

I'm developing a Java Spring application.我正在开发一个 Java Spring 应用程序。 I have some fields in my application which are configured using a .yml config file.我的应用程序中有一些字段是使用 .yml 配置文件配置的。 I would like to import those values using an @Value annotation on the fields in question.我想在相关字段上使用 @Value 注释导入这些值。 I would also like to use the best-practice of constructor injection rather than field injection, but I would like to write my constructor using Lombok rather than manually.我还想使用构造函数注入的最佳实践而不是字段注入,但我想使用 Lombok 而不是手动编写我的构造函数。 Is there any way to do all these things at once?有没有办法一次完成所有这些事情? As an example, this doesn't work but is similar to what I want to do:例如,这不起作用,但与我想做的类似:

@AllArgsConstructor
public class my service {
    @Value("${my.config.value}")
    private String myField;

    private Object myDependency;

    ...
}

In this case, what I want is Lombok to generate a constructor which sets only myDependency, and for myField to be read from my config file.在这种情况下,我想要的是 Lombok 生成一个仅设置 myDependency 的构造函数,并从我的配置文件中读取 myField。

Thanks!谢谢!

You need @RequiredArgsConstructor and mark myDependency as final.您需要@RequiredArgsConstructor并将myDependency标记为 final。 In this case, Lombok will generate a constructor based on 'required' final filed as argument, for example:在这种情况下,Lombok 将根据作为参数的“required”final 生成一个构造函数,例如:

@RequiredArgsConstructor
@Service
public class MyService {

    @Value("${my.config.value}")
    private String myField;

    private final MyComponent myComponent;

    //...
}

That is equal the following:这等于以下内容:

@Service
public class MyService {

    @Value("${my.config.value}")
    private String myField;

    private final MyComponent myComponent;

    public MyService(MyComponent myComponent) { // <= implicit injection
        this.myComponent = myComponent;
    } 

    //...
}

Since here is only one constructor, Spring inject MyComponent without the explicit use of the @Autowired annotation .由于这里只有一个构造函数,Spring 注入MyComponent 而不显式使用@Autowired注解

Male sure you are using at least version 1.18.4 of Lombok.男性确保您至少使用 Lombok 的1.18.4版本。 And that you have your desired annotation added to the lombok.config file.并且您已将所需的注释添加到lombok.config文件中。

lombok.copyableAnnotations += org.springframework.beans.factory.annotation.Value

Here is your class:这是你的课:

@AllArgsConstructor(onConstructor = @__(@Autowired))
public class MyService{

    @Value("${my.config.value}")
    private String myField;

    private Object myDependency;
}

And here is the lombok generated class:这是 lombok 生成的类:

public class MyService {

@Value("${my.config.value}")
private String myField;

private Object myDependency;

@Autowired
@Generated
public MyService(@Value("${my.config.value}") final String myField, final Object myDependency) {
    this.myField = myField;
    this.myDependency = myDependency;
}

PS: Make sure you have the lombok.config file under /src/main/java folder. PS:确保您在 /src/main/java 文件夹下有lombok.config文件。 I tried adding it to /src/main/resources and it did not work.我尝试将其添加到 /src/main/resources 中,但没有奏效。

Response taken from here .这里采取的回应。

我在对OP的评论中使用了Chrylis的建议并且它起作用了。

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

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