I cannot seem to make dependency injection work #4599
-
I am currently learning micronaut and I wanted to create a simple CRUD application that will take me through all the modules (web, data, etc.). The project link can be found here: https://github.com/nomemory/micronaut-crud-backend . I have an entity called @Entity
@Introspected
public class Book {
@Id
@GeneratedValue
private Long id;
private String title;
private int pages;
/// A repository for books: @Singleton
@Repository
public interface BookRepository extends CrudRepository<Book, Long> {
@Executable
Book find(String title);
} And a controller for books: @Controller("/book")
public class BookController {
@Inject
BookRepository bookRepository;
@Post
@Produces(TEXT_PLAIN)
public Long createBook(@Body CreateBook createBook) {
Book book = new Book(createBook.getTitle(), createBook.getPages());
return bookRepository.save(book).getId();
}
} When I try to access the endpoint
My investigation lead to the conclusion that the My gradle dependencies are looking this:
Any ideas & suggestions or another sample repo where I can get inspired from ? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Micronaut Launch is a great reference for how to get setup properly and get your application correctly configured.
$ mn create-app jpa-example --features data-jpa
$ cd jpa-example or $ curl https://launch.micronaut.io/jpa-example.zip?features=data-jpa -o jpa-example.zip
$ unzip jpa-example.zip -d jpa-example
$ cd jpa-example
Modify content to: package jpa.example;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
class Person {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package jpa.example;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import javax.inject.Inject;
@MicronautTest
public class JpaExampleTest {
@Inject
PersonRepository repository;
@Test
void testItWorks() {
Assertions.assertEquals(0, repository.count());
}
}
|
Beta Was this translation helpful? Give feedback.
-
@graemerocher thanks for the quick reply. Can I use the I've created a project as you described above, and I saw that the dependencies generated similar to the dependencies from the project I am referring to: https://github.com/nomemory/micronaut-crud-backend . Do you have any idea what I am doing wrong ? |
Beta Was this translation helpful? Give feedback.
Micronaut Launch is a great reference for how to get setup properly and get your application correctly configured.
$ mn create-app jpa-example --features data-jpa $ cd jpa-example
or
Modify content to: