来自Yml文件的Spring Boot和属性数据

时间:2018-05-10 14:26:07

标签: java spring-boot

我的Spring启动应用程序无法使用application.yml中的属性对象创建bean。

这是我做的:

;WITH Boundaries AS
(
    SELECT
        T.FieldID,
        T.ChangeField,
        T.OldValue,
        T.NewValue,
        OldestRanking = ROW_NUMBER() OVER (
            PARTITION BY
                T.FieldID,
                T.ChangeField
            ORDER BY
                T.ChangeDate ASC),
        NewestRanking = ROW_NUMBER() OVER (
            PARTITION BY
                T.FieldID,
                T.ChangeField
            ORDER BY
                T.ChangeDate DESC)
    FROM
        YourTable AS T
)
SELECT
    T.FieldID,
    T.ChangeField,
    T.OldValue,
    OldValueDate = T.ChangeDate,
    N.NewValue,
    NewValueDate = N.ChangeDate,
FROM
    Boundaries AS T
    INNER JOIN Boundaries AS N ON 
        T.FieldID = N.FieldID AND
        T.ChangeField  = N.ChangeField
WHERE
    T.OldestRanking = 1 AND
    N.NewestRanking = 1

我创建了一个application.yml: statsd: host: 10.0.5.23 port: 8125 类来保存上述数据:

StatsdProperties @Component

我创建了另一个应该使用上述属性的@Component @ConfigurationProperties(prefix="statsd") public class StatsdProperties { private String host; private int port; .... getters & setters } 类:

@Component

@Component public class MyClass{ @Autowired private StatsdProperties statsdProperties; public MyClass(){ statsdProperties.getPort() <--- statsdProperties is null here }

statsdProperties为空

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要在MyClass

中启用配置
 @Component
 @EnableConfigurationProperties(StatsdProperties.class)
 public class MyClass