我的主類(ReservationprojectApplication)中有這個 CommandLineRunner
@Bean
CommandLineRunner run(UserService userService) {
return args -> {
userService.saveRole(new Role(null, "ROLE_ADMIN"));
userService.saveRole(new Role(null, "ROLE_STUDENT"));
userService.saveUser(new User("administrator", "[email protected]", "t", new ArrayList<>(), Instant.now(), true));
userService.saveUser(new User("elias", "[email protected]", "t", new ArrayList<>(), Instant.now(), true));
userService.addRoleToUser("administrator", "ROLE_ADMIN");
userService.addRoleToUser("elias", "ROLE_STUDENT");
};
}
有沒有辦法只在將 ddl-auto 設定為創建時才運行此代碼?每次我的 ddl-auto 設定為更新時,我都不想評論此代碼。
spring:
# Database properties
datasource:
password: blabla
url: jdbc:postgresql://localhost:5432/cegeka_reservation
username: postgres
jpa:
hibernate:
**ddl-auto: update**
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
format_sql: true
show_sql: true
提前致謝!
uj5u.com熱心網友回復:
您應該可以訪問該屬性,以便檢查它很容易
@Value("${spring.jpa.hibernate.ddl-auto}")
private String ddl;
@Bean
CommandLineRunner run(UserService userService) {
if ("create".equals(ddl) {
// Rest of your code here
}
}
uj5u.com熱心網友回復:
最簡單的解決方案是使用@ConditionalOnProperty如下所示的注釋,它檢查指定的屬性是否具有特定值,有關 Spring Boot 條件 bean 的更多詳細資訊,請查看博客文章。
@Bean
@ConditionalOnProperty(
name = {"spring.jpa.hibernate.dll-auto"},
havingValue = "create")
CommandLineRunner run(UserService userService) {
//...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/527346.html
上一篇:KotlinJetpack,如何使用字串陣列加載可繪制物件
下一篇:串列內的Javax驗證
