Spring 引导不拾取属性
Spring Boot not picking up properties
我有一个 Spring 引导应用程序,我正在尝试从 application.properties 传递 属性 作为实体 class 中的参数注释,如下所示:
@Entity
public class Entity{
@Id
@SequenceGenerator(name = "entity_id_seq", sequenceName = "entity_id_seq", initialValue = "${start-seq}")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dish_id_seq")
private int id;
我的 application.properties 有:
start-seq=100000
但它不会编译说“不兼容的类型:java.lang.String 无法转换为 int”无法识别表达式。我还尝试将带有 @Value 注释的 属性 传递给这样的变量:
@Value("${start-seq}")
private int startSeq;
但变量始终为 0。我在配置前确实有以下注释 class,但它仍然不起作用:
@Configuration
@EnableConfigurationProperties
@PropertySources({
@PropertySource("classpath:application.properties")
})
我是不是漏掉了什么?
@SequenceGenerator 不是 Spring 注释。因此 spring 在实体创建过程中不会对此进行评估。
您不能在此处使用 spring 表达式语言 (SpEl)。
如果你想配置这些,你必须为此使用 JPA 或 Hibernate 配置。
例如。编写或扩展当前的 SequenceGenerator。详情请看这里:
https://thorben-janssen.com/custom-sequence-based-idgenerator/
我有一个 Spring 引导应用程序,我正在尝试从 application.properties 传递 属性 作为实体 class 中的参数注释,如下所示:
@Entity
public class Entity{
@Id
@SequenceGenerator(name = "entity_id_seq", sequenceName = "entity_id_seq", initialValue = "${start-seq}")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "dish_id_seq")
private int id;
我的 application.properties 有:
start-seq=100000
但它不会编译说“不兼容的类型:java.lang.String 无法转换为 int”无法识别表达式。我还尝试将带有 @Value 注释的 属性 传递给这样的变量:
@Value("${start-seq}")
private int startSeq;
但变量始终为 0。我在配置前确实有以下注释 class,但它仍然不起作用:
@Configuration
@EnableConfigurationProperties
@PropertySources({
@PropertySource("classpath:application.properties")
})
我是不是漏掉了什么?
@SequenceGenerator 不是 Spring 注释。因此 spring 在实体创建过程中不会对此进行评估。
您不能在此处使用 spring 表达式语言 (SpEl)。
如果你想配置这些,你必须为此使用 JPA 或 Hibernate 配置。
例如。编写或扩展当前的 SequenceGenerator。详情请看这里:
https://thorben-janssen.com/custom-sequence-based-idgenerator/