1

I'm using @Value annotation to inject properties and now the properties have increased and the constructor is getting really big .Is there a way to handle this problem

@Component
public class Job {

    private String someProperty

    @Autowired
    public Job(@Value("${some.property}") String someProperty,.............){
        this.someProperty = someProperty
    }

2 Answers 2

3

Just annotate the fields directly.

@Value("${some.property}") 
private String someProperty

You can do any additional processing in a @PostConstruct method.

Sign up to request clarification or add additional context in comments.

1 Comment

writing a unit test is easy If I use constructor . I can do Job job = new Job("fakeSomeProperty");..
1

Why dont you just do this:

@Component public class Job {

@Value("${some.property1}") private String someProperty1
@Value("${some.property2}") private String someProperty2
//...

@Autowired public Job( ){ 


// your someProperty1 is already set
}

2 Comments

ok so someProperty1 gets initialized when an instance of class is created?
yes, those values will be initialized by Spring at startup of the application. I assume that you will get an instance of this class using @Autowired .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.