0

This seems silly because it looks like such an easy thing but I am trying to initialize my spring boot project with dummy data using the ApplicationRunner. Can anyone explain why this code is not working? I am expecting it to print to the console the following:

Note(id=1, title=Note 1, text=null, user=user)
Note(id=2, title=Note 2, text=null, user=user)
Note(id=3, title=Note 3, text=null, user=user)

but instead it doesn't print anything at all.

Here is the implementation:

import com.bhanna.bugtracker.notes.Note
import com.bhanna.bugtracker.notes.NotesRepository
import org.springframework.boot.ApplicationArguments
import org.springframework.boot.ApplicationRunner
import org.springframework.stereotype.Component

@Component
class DataInitializer(val repository: NotesRepository) : ApplicationRunner {

    @Throws(Exception::class)
    override fun run(args: ApplicationArguments) {
        println("This should be printing to the console but it is not")

        listOf("Note 1", "Note 2", "Note 3").forEach {
            repository.save(Note(title = it, user = "user"))
        }
        repository.findAll().forEach { println(it) }
    }
}

Where Note is:

@Entity
data class Note(@Id @GeneratedValue var id: Long? = null,
                var title: String? = null,
                var text: String? = null,
                @JsonIgnore var user: String? = null) {
}

@Component
@RepositoryEventHandler(Note::class)
class AddUserToNote {

    @HandleBeforeCreate
    fun handleCreate(note: Note) {
        val username: String =  SecurityContextHolder.getContext().getAuthentication().name
        println("Creating note: $note with user: $username")
        note.user = username
    }
}

and NoteRepository is:

@RepositoryRestResource
interface NotesRepository : JpaRepository<Note, Long> {
    fun findAllByUser(name: String): List<Note>
}

and finally the main file:

@SpringBootApplication
class BugTrackerApplication

fun main(args: Array<String>) {
    runApplication<BugTrackerApplication>(*args)
}

I thought the @Component annotation meant spring will scan it and execute the underlying run() function since it is implementing ApplicationRunner. Can anyone explain whats going on here?

4
  • 1
    any specific error you are facing? Commented Jun 15, 2020 at 19:42
  • @SushilBehera no error, just not printing to the console (i updated the question). The function run() is not being executed Commented Jun 15, 2020 at 19:57
  • 1
    How does your main() look like? What is your package structure? Commented Jun 15, 2020 at 21:20
  • @FLUXparticle I just added it above. (sorry i tried to keep the code to a minimum but at this point all my cards are on the table lol). Any ideas? Commented Jun 16, 2020 at 0:59

1 Answer 1

2

I tried your program and got this error message:

Configuration problem: @Configuration class 'BugTrackerApplication' may not be final. Remove the final modifier to continue.

So when I changed BugTrackerApplication to:

@SpringBootApplication
open class BugTrackerApplication

it worked as expected.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.