java - Spring Boot没有拾取属性
问题描述
我有一个 Spring Boot 应用程序,我试图从 application.properties 传递一个属性作为 Entity 类中的参数注释,如下所示:
@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。我的配置类之前确实有以下注释,但它仍然不起作用:
@Configuration
@EnableConfigurationProperties
@PropertySources({
@PropertySource("classpath:application.properties")
})
我错过了什么吗?
解决方案
@SequenceGenerator 不是 Spring 注释。因此,在实体创建的处理过程中,spring 不会对此进行评估。
您不能在此处使用弹簧表达式语言 (SpEl)。
如果要配置这些,则必须为此使用 JPA 或 Hibernate 配置。
例如。编写或扩展当前的 SequenceGenerator。有关详细信息,请参见此处:
https://thorben-janssen.com/custom-sequence-based-idgenerator/
推荐阅读
- python - 如何在 Django 项目中对我的用户隐藏外部 url?
- c# - 如何将自定义消息从 C++ MFC 发送到 WPF C# 应用程序?
- javascript - 我们可以使用 CSS 代替 jQuery 中的 .fadeIn() 吗?
- c++ - std::cin.fail() 的问题
- python - 将 text() 与 python-escpos 一起使用时,我得到 [Errno None] 和 key error = 1 (windows 10)
- sql-server - 在 MSSQL 服务器中使用行锁进行更新
- c++ - 如何在不知道其大小的情况下将输入输入到向量内的向量中?
- laravel - DataTable 在 Laravel 和 Vuejs 中被破坏
- postgresql - SERIAL 与 NULL 一起使用,始终作为 IDENTITY 生成而不是
- javascript - 单一 SPA 的替代方案