英文:
lombok @RequiredArgsConstructor how to inject value to the constructor spring boot
问题
I have a class with lombok @RequiredArgsConstructor:
@RequiredArgsConstructor
@Service
public class Test{
private final String str;
private String str5;
// more code
}
In non-spring boot we provide in xml as:
<bean id="Test" class="com.abc.Test">
<constructor-arg index="0" value="${xyz}"/>
</bean>
如何在Spring Boot中实现相同的功能,可能通过application.properties
,但如何进行注入?
英文:
I have a class with lombok @RequiredArgsConstructor:
@RequiredArgsConstructor
@Service
public class Test{
private final String str;
private String str5;
// more code
}
In non-spring boot we provide in xml as:
<bean id="Test" class="com.abc.Test">
<constructor-arg index="0" value="${xyz}"/>
</bean>
how to achieve same from spring boot may be via application.properties but how to inject
答案1
得分: 1
在这种情况下,我认为最好的做法要么是注释字段:
@Service
public class Test{
@Value("${xyz}")
private String str;
private String str5;
// 更多的代码
}
或者显式地定义带有注释参数的构造函数:
@Service
public class Test{
private final String str;
private String str5;
public Test(@Value("${xyz}") String str) {
this.str = str;
}
// 更多的代码
}
如果您有其他 final 字段,您可以结合使用 Lombok 构造函数生成和字段注释,就像在 https://stackoverflow.com/questions/52321988 中所提到的那样:
@RequiredArgsConstructor
@Service
public class Test{
@Value("${xyz}")
private String str;
private final String str5;
// 更多的代码
}
英文:
In this case I think you are better off either annotating the field :
@Service
public class Test{
@Value("${xyz}")
private String str;
private String str5;
// more code
}
or defining explicitly the constructor with an annotated parameter:
@Service
public class Test{
private final String str;
private String str5;
public Test(@Value("${xyz}") String str) {
this.str = str;
}
// more code
}
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
@RequiredArgsConstructor
@Service
public class Test{
@Value("${xyz}")
private String str;
private final String str5;
// more code
}
答案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.
//Test.java
package com.abc;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@RequiredArgsConstructor
@ToString
public class Test {
private final String str;
private String str5;
}
//DemoApplication.java
package com.abc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
Test buildTest(@Value("${xyz}") String value) {
return new Test(value);
}
}
note: @SpringBootApplication
implies @Configuration
//DemoApplicationTests.java
package com.abc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
com.abc.Test test;
@Test
void contextLoads() {
System.out.println(test);
}
}
#application.properties
xyz=print me
Result:
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.
//Test.java
package com.abc;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@RequiredArgsConstructor
@ToString
public class Test {
private final String str;
private String str5;
}
//DemoApplication.java
package com.abc;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Bean
Test buildTest(@Value("${xyz}") String value) {
return new Test(value);
}
}
note: @SpringBootApplication
implies @Configuration
//DemoApplicationTests.java
package com.abc;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Autowired
com.abc.Test test;
@Test
void contextLoads() {
System.out.println(test);
}
}
#application.properties
xyz=print me
Result:
Test(str=print me, str5=null)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论