Yogendra
Yogendra

Reputation: 231

Using Spring @Value annotation for multiple properties

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
    }

Upvotes: 1

Views: 3630

Answers (2)

storm_buster
storm_buster

Reputation: 7568

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
}

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279880

Just annotate the fields directly.

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

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

Upvotes: 3

Related Questions