lombok @RequiredArgsConstructor how to inject value to the constructor spring boot

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

lombok @RequiredArgsConstructor how to inject value to the constructor spring boot

问题

I have a class with lombok @RequiredArgsConstructor:

  1. @RequiredArgsConstructor
  2. @Service
  3. public class Test{
  4. private final String str;
  5. private String str5;
  6. // more code
  7. }

In non-spring boot we provide in xml as:

  1. <bean id="Test" class="com.abc.Test">
  2. <constructor-arg index="0" value="${xyz}"/>
  3. </bean>

如何在Spring Boot中实现相同的功能,可能通过application.properties,但如何进行注入?

英文:

I have a class with lombok @RequiredArgsConstructor:

  1. @RequiredArgsConstructor
  2. @Service
  3. public class Test{
  4. private final String str;
  5. private String str5;
  6. // more code
  7. }

In non-spring boot we provide in xml as:

  1. &lt;bean id=&quot;Test&quot; class=&quot;com.abc.Test&quot;&gt;
  2. &lt;constructor-arg index=&quot;0&quot; value=&quot;${xyz}&quot;/&gt;
  3. &lt;/bean&gt;

how to achieve same from spring boot may be via application.properties but how to inject

答案1

得分: 1

在这种情况下,我认为最好的做法要么是注释字段:

  1. @Service
  2. public class Test{
  3. @Value("${xyz}")
  4. private String str;
  5. private String str5;
  6. // 更多的代码
  7. }

或者显式地定义带有注释参数的构造函数:

  1. @Service
  2. public class Test{
  3. private final String str;
  4. private String str5;
  5. public Test(@Value("${xyz}") String str) {
  6. this.str = str;
  7. }
  8. // 更多的代码
  9. }

如果您有其他 final 字段,您可以结合使用 Lombok 构造函数生成和字段注释,就像在 https://stackoverflow.com/questions/52321988 中所提到的那样:

  1. @RequiredArgsConstructor
  2. @Service
  3. public class Test{
  4. @Value("${xyz}")
  5. private String str;
  6. private final String str5;
  7. // 更多的代码
  8. }
英文:

In this case I think you are better off either annotating the field :

  1. @Service
  2. public class Test{
  3. @Value(&quot;${xyz}&quot;)
  4. private String str;
  5. private String str5;
  6. // more code
  7. }

or defining explicitly the constructor with an annotated parameter:

  1. @Service
  2. public class Test{
  3. private final String str;
  4. private String str5;
  5. public Test(@Value(&quot;${xyz}&quot;) String str) {
  6. this.str = str;
  7. }
  8. // more code
  9. }

And if you have other final fields you can combine lombok constructor generation with field annotation like this as noted in https://stackoverflow.com/questions/52321988

  1. @RequiredArgsConstructor
  2. @Service
  3. public class Test{
  4. @Value(&quot;${xyz}&quot;)
  5. private String str;
  6. private final String str5;
  7. // more code
  8. }

答案2

得分: 0

The @Service annotation needs to be removed and the bean must be created in a @Configuration class with a @Bean annotated method returning that class type.

  1. //Test.java
  2. package com.abc;
  3. import lombok.RequiredArgsConstructor;
  4. import lombok.ToString;
  5. @RequiredArgsConstructor
  6. @ToString
  7. public class Test {
  8. private final String str;
  9. private String str5;
  10. }
  1. //DemoApplication.java
  2. package com.abc;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.Bean;
  7. @SpringBootApplication
  8. public class DemoApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. @Bean
  13. Test buildTest(@Value("${xyz}") String value) {
  14. return new Test(value);
  15. }
  16. }

note: @SpringBootApplication implies @Configuration

  1. //DemoApplicationTests.java
  2. package com.abc;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. class DemoApplicationTests {
  8. @Autowired
  9. com.abc.Test test;
  10. @Test
  11. void contextLoads() {
  12. System.out.println(test);
  13. }
  14. }
  1. #application.properties
  2. xyz=print me

Result:

  1. Test(str=print me, str5=null)
英文:

The @Service annotation needs to be removed and the bean must be created in a @Configuration class with a @Bean annotated method returning that class type.

  1. //Test.java
  2. package com.abc;
  3. import lombok.RequiredArgsConstructor;
  4. import lombok.ToString;
  5. @RequiredArgsConstructor
  6. @ToString
  7. public class Test {
  8. private final String str;
  9. private String str5;
  10. }
  1. //DemoApplication.java
  2. package com.abc;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.annotation.Bean;
  7. @SpringBootApplication
  8. public class DemoApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. @Bean
  13. Test buildTest(@Value(&quot;${xyz}&quot;) String value) {
  14. return new Test(value);
  15. }
  16. }

note: @SpringBootApplication implies @Configuration

  1. //DemoApplicationTests.java
  2. package com.abc;
  3. import org.junit.jupiter.api.Test;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.test.context.SpringBootTest;
  6. @SpringBootTest
  7. class DemoApplicationTests {
  8. @Autowired
  9. com.abc.Test test;
  10. @Test
  11. void contextLoads() {
  12. System.out.println(test);
  13. }
  14. }
  1. #application.properties
  2. xyz=print me

Result:

  1. Test(str=print me, str5=null)

huangapple
  • 本文由 发表于 2020年8月13日 00:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63380477.html
匿名

发表评论

匿名网友

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

确定