1

I have class that one of it's parameters i want to set from properties file:

import org.springframework.beans.factory.annotation.Value; (..) @Getter @Setter @NoArgsConstructor public class ConvertNonStandardOfferRequestDtoWrapper { private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto; @Value("true") private boolean documentPrintoutsRequired; public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto) { this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto; } } 

What i see inside constructor is that documentPrintoutsRequired is false instead of true. I see that when debuging and setting breakpoint inside constructor. And i have a pom file for this module:

 <modelVersion>4.0.0</modelVersion> <parent> <groupId>x</groupId> <artifactId>policy</artifactId> <version>4.0-SNAPSHOT</version> </parent> <artifactId>policy-api</artifactId> <dependencies> <dependency> <groupId>x</groupId> <artifactId>common-api</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> </dependencies> <build> (...) </build> </project> 

I am wonderning why @value does not work correctly ?

4
  • Ofcourse it is false in the constructor. The @Value will be resolved after the object has been constructed, hence after the constructor. Commented Dec 18, 2018 at 13:25
  • After object returns from my constructor it still has this field setup to false Commented Dec 18, 2018 at 13:28
  • 1
    It will be set after spring processed it. Also HOW are you constructing the objet. I you are constructing it yourself, instead of defining it as a bean in Spring nothing will happen. @Value will only work for Spring managed beans. Commented Dec 18, 2018 at 13:29
  • ok.. thanks - i create it using new keyword. Commented Dec 18, 2018 at 13:40

3 Answers 3

5

I'd advise you use constructor inyection for all attributes, this way you'll see the injected @Value during construction time.

Besides the class must be a Spring bean, so add @Component annotation:

@Component @Getter @Setter @NoArgsConstructor public class ConvertNonStandardOfferRequestDtoWrapper { private ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto; private boolean documentPrintoutsRequired; public ConvertNonStandardOfferRequestDtoWrapper(ConvertNonStandardOfferRequestDto convertNonStandardOfferRequestDto, @Value("${yourproperty}") boolean documentPrintoutsRequired) { this.convertNonStandardOfferRequestDto = convertNonStandardOfferRequestDto; this.documentPrintoutsRequired = documentPrintoutsRequired; } } 
Sign up to request clarification or add additional context in comments.

8 Comments

One time I want to use value from property, other time not - this is the cause of using this constructor. Please tell me why i don't see accurate value in the field?
If you have @Value in the attribute you won't see the injected value during constructor execution. This is so because Spring injects fields AFTER the constructor has finished executing.
After object returns from my constructor it still has this field setup to false
What's the content of your @Value(XXXX)? It should follow this format @Value("${name.of.a.property.from.application.yml}"), e.g: in application.yml: my-prop: "myvalue" -> @Value("${my-prop}")
i tried simple case like value from start true and even this does not work.
|
1

You can read the value from properties file, such as username = Tom.

use @Value in Java, you can set a default value like this:

@Value("${username:Jack}") 

If the username does not exist in properties file, it will be "Jack".

1 Comment

@yami Did you set the <context : property-placeholder> label in the springmvc.xml ?
0

Did you try this:

@Value("${yourPropInPropertiesFile}") private boolean documentPrintoutsRequired; 

1 Comment

Could you post tour properties file please ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.