简体   繁体   中英

Spring Boot not picking up properties

I have a Spring Boot application and I'm trying to pass a property from application.properties as a parameter annotation within Entity class like that:

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

And my application.properties have:

start-seq=100000

But it doesn't compile saying "incompatible types: java.lang.String cannot be converted to int" not recognizing expression. Also I tried to pass this property with @Value annotation to a variable like that:

@Value("${start-seq}")
private int startSeq;

But the variable is always 0. I do have following annotations before my config class, but it still doesn't work:

@Configuration
@EnableConfigurationProperties
@PropertySources({
        @PropertySource("classpath:application.properties")
})

Am I missing something?

@SequenceGenerator is not an Spring annotation. Thus spring does not evaluate this during the processing of the entity creation.

You can not use spring expression language (SpEl) here.

If you want to configure these, you have to use JPA oder Hibernate configuration for this.

Eg. write or extend the current SequenceGenerator. See here for details:

https://thorben-janssen.com/custom-sequence-based-idgenerator/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM