Reputation: 2104
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 ?
Upvotes: 1
Views: 4795
Reputation: 63
Did you try this:
@Value("${yourPropInPropertiesFile}")
private boolean documentPrintoutsRequired;
Upvotes: 0
Reputation: 24482
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;
}
}
Upvotes: 5
Reputation: 150
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".
Upvotes: 1