我为我的模型创建了一个 CustomSequenceGenerator,一切正常。
现在我正在尝试从 CustomSequenceGenerator 中的 application.properties 中读取值,但失败了。
我尝试了stackoverflow建议的许多方法,但仍然没有运气。
1. Using @Value
2. Using Spring Environment env > env.getProperty()
3. Using @ConfigurationProperties
4. Using @PropertySource
这是我的代码:
模型
@Id
@GenericGenerator(name = "user_id_gen", strategy="com.my.model.common.CustomSequenceGenerator ",
parameters = {
@org.hibernate.annotations.Parameter(name = "sequence_name", value = "USER_SEQ")}
)
@GeneratedValue(generator = "user_id_gen")
@Column(name = "UserId", unique = true, nullable = false, length = 6)
private String userId;
自定义序列生成器
public class CustomSequenceGenerator implements IdentifierGenerator, Configurable {
@Value("${seq.prefix}")
private String sequencePrefix;
.......
}
我在我的 CustomSequenceGenerator 上放了一个断点,我注意到它在服务器启动期间跳转到断点,所以我猜 Spring 在启动/初始化期间无法读取 application.properties。
应用程序属性
位于Resources/conf/application.properties
,我已经指定了使用的位置-Dspring.config.location
,其他控制器在访问属性文件时没有问题,只是 CustomSequenceGenerator 有问题。
....
spring.jpa.show-sql=true
seq.prefix = MOCKDB.MOCK_SCHEMA.
....
那么在这种情况下我如何读取属性值呢?
谢谢你。