Can we use @Value with lombok?
I created a class below
@Getter
@Setter
class Hello
{
@Value("${url}")
private String url;
}
Is it possible to reuse the String url value in other class,Using lombok getters and setters?
Can we use @Value with lombok?
I created a class below
@Getter
@Setter
class Hello
{
@Value("${url}")
private String url;
}
Is it possible to reuse the String url value in other class,Using lombok getters and setters?
Of course. Lombok creates the getters and setters, public by default, and therefore they are accessible by any other class using the conventional getter/setter syntax. In this case, you'd just need to invoke the function:
yourHelloObject.getUrl()
Yes, but it still needs to adhere to the rules of autowiring. You need to give Spring's dependency injection framework a chance to get involved.
If you just write
Hello hello = new Hello()
System.out.println(hello.getUrl()); // null
then the result will be null.
Because objects may be left in a half-initialized state, field injection is usually not a good idea.
This is nothing to do with Lombok. The object needs to be created by Spring. One way to do that is to make it a component
@Component
@Getter
@Setter
class Hello
{
@Value("${url}")
private String url;
}
...
@Component
public class AnotherComponent {
public AnotherComponent(Hello hello) { //constructor injection
System.out.println(hello.getUrl()); //not null
}
}