Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject value from properties into interface field

Is there an option to insert property on interface field? I tried something like this, but it didn't work.

public interface ServicePathsConfig {
    @Value("${default-connection-timeout}")
    int DEFAULT_CONNECT_TIMEOUT = 1000;
}

I tried to make default setter with @PostConstruct, same result. Any ideas how can I inject property to interface field?

like image 380
NOXYEES Avatar asked May 06 '26 18:05

NOXYEES


1 Answers

As a workaround, in some cases you can turn your interface into an abstract class, and then it is possible:

public abstract class ServicePathsConfig {
    @Value("${default-connection-timeout:1000}")
    protected int defaultConnectTimeout;

    public abstract void someMethod();
}

Fun fact about C++: It is what some people from the C++ world call the "Interface Design Pattern", not having interface as a language feature, but having multiple inheritance.

like image 81
Honza Zidek Avatar answered May 09 '26 11:05

Honza Zidek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!