Spring @value没有设置属性

时间:2018-12-18 13:09:11

标签: java spring spring-boot

我有一个类,它是我想从属性文件中设置的参数之一:

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;
    }
}

我在构造函数中看到的是documentPrintoutsRequiredfalse而不是true。我在构造函数中调试和设置断点时看到了这一点。我为此模块有一个pom文件:

    <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>

我想知道为什么@value无法正常工作?

3 个答案:

答案 0 :(得分:4)

我建议您对所有属性都使用构造函数inection,这样您就可以在构造期间看到注入的@Value。

除了该类必须是Spring bean ,因此添加@Component批注:

@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;
    }
}

答案 1 :(得分:0)

您尝试过此吗:

@Value(“ $ {yourPropInPropertiesFile}”)  私有布尔文件printoutsRequired;

答案 2 :(得分:0)

您可以从属性文件中读取值,例如 用户名=汤姆。

在Java中使用@Value,您可以设置如下默认值:

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

如果用户名在属性文件中不存在,则为“杰克”。