-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this is for demo usage of Profile & ConditionalOnProperty features
- Loading branch information
yingtingxu
committed
Jun 5, 2024
1 parent
7e7b10a
commit 01544c8
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
mall-cloud-native/src/main/java/org/mall/catalogservice/demo/BookDataLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.mall.catalogservice.demo; | ||
|
||
import org.mall.catalogservice.domain.Book; | ||
import org.mall.catalogservice.domain.BookRepository; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
//@Profile("testdata") | ||
@ConditionalOnProperty(name="mall.testdata.enabled", havingValue = "true") | ||
public class BookDataLoader { | ||
private final BookRepository bookRepository; | ||
|
||
public BookDataLoader(BookRepository bookRepository) { | ||
this.bookRepository = bookRepository; | ||
} | ||
|
||
@EventListener(ApplicationReadyEvent.class) | ||
public void loadBookTestData() { | ||
var book1 = new Book("1234567891", "Northern Lights", "Lyra Silverstar", 9.90); | ||
var book2 = new Book("1234567892", "Polar Journey", "Iorek Polarson", 12.90); | ||
bookRepository.save(book1); | ||
bookRepository.save(book2); | ||
} | ||
} |