In my Spring Boot application, I'm using CommandLineRunner to create a new schema and them import some test data after that.
@Profile("create-schema")
@Component
public class CreateSchema {
// creating schema inside. This works because I can see from the database
}
@Profile("import-data")
@Component
public class DataImporter {
}
and this is the sequence in application.properties
spring.profiles.active=${SPRING_PROFILE}, create-schema, import-data
And using this in application.properties
spring.jpa.properties.hibernate.default_schema=simba
Schema creation starts after the application has started; and after schema is created, import-data starts.
when import-data is running, I get an error that
relation
schema_name.table_name
does not exist
However, once the schema is created and I run the application again - it works. So, when I have to deploy my app where every time I have to create a schema to run some integration tests - it would fail there.
am I running something in wrong order?
Profiles are completely irrelevant here. You can ensure the schema is created before the data import by doing something like the following:
@Component("schemaCreator")
public class SchemaCreator {
@PostConstruct
public void initSchema(){
}
}
Data Importer can be made dependent on the schema being initialised via the @DependsOn
annotation.
Beans on which the current bean depends. Any beans specified are guaranteed to be created by the container before this bean. Used infrequently in cases where a bean does not explicitly depend on another through properties or constructor arguments, but rather depends on the side effects of another bean's initialization.
@DependsOn("schemaCreator")
@Component
public class DataImporter {
@PostConstruct
public void initData(){
}
}
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.