Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
How to set @Value default from class field/member ?
I have a Spring @Bean class with a @Value annotated field and I need that the default value for that field will come from a class member/field value.
In my example I need the maxNumOfThreads field get its default value from the DEFAULT_MAX_NUM_OF_THREADS.
@Configuration
public class AppConfig {
@Value("${jobs.max-num-of-threads:....??..}") // <<== howto ?
private Integer maxNumOfThreads;
public final Integer DEFAULT_MAX_NUM_OF_THREADS = 10;
How do I do this ?
Class field default value can be references using the spel '@' bean reference syntax: @beanName.fieldName
e.g.: when the bean name is 'AppConfig' and the field we need to reference is
DEFAULT_MAX_NUM_OF_THREADS
use @appConfig
as the bean name (class name AppConfig starts with lowercase) dot '.' the field name.
bymeans: @appConfig.DEFAULT_MAX_NUM_OF_THREADS
@Configuration
public class AppConfig {
@Value("#{'${jobs.max-num-of-threads:}' ?: @appConfig.DEFAULT_MAX_NUM_OF_THREADS}")
private Integer maxNumOfThreads;
public final Integer DEFAULT_MAX_NUM_OF_THREADS = 10;
@PostConstruct
public void init(){
System.out.println("@PostConstruct init() - in.");
by yl.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.