Я пытаюсь внедрить EntityManager в хранилище. Компиляция прошла успешно, но когда я запустил приложение и отправил почтовый запрос, я получил эту ошибку:
Unexpected error occurred: Failed to inject value for parameter [entityManager] of class: aututor.apps.DAO.Repository.RepositoryImp.UserRepository
Message: No bean of type [javax.persistence.EntityManager] exists. Make sure the bean is not disabled by bean requirements (enable trace logging for 'io.micronaut.context.condition' to check) and if the bean is enabled then ensure the class is declared a bean and annotation processing is enabled (for Java and Kotlin the 'micronaut-inject-java' dependency should be configured as an annotation processor).
Path Taken: new UserController([UserService service]) --> new UserServiceImpl([IUserRepository userRepository]) --> new UserRepository([EntityManager entityManager])
io.micronaut.context.exceptions.DependencyInjectionException: Failed to inject value for parameter [entityManager] of class: aututor.apps.DAO.Repository.RepositoryImp.UserRepository
это хранилище:
package aututor.apps.DAO.Repository.IRepository;
import aututor.apps.DAO.Model.*;
public interface IUserRepository {
public User save(User User);
public User findByEmail(String Email);
}
package aututor.apps.DAO.Repository.RepositoryImp;
import aututor.apps.DAO.Model.User;
import aututor.apps.DAO.Repository.IRepository.IUserRepository;
import io.micronaut.configuration.hibernate.jpa.scope.CurrentSession;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Singleton
public class UserRepository implements IUserRepository {
@PersistenceContext
protected EntityManager entityManager;
public UserRepository(@CurrentSession EntityManager entityManager) {
this.entityManager = entityManager;
}
@Override
public User save(User User) {
return entityManager.merge(User);
}
@Override
public User findByEmail(String Email) {
return entityManager.createQuery("SELECT user FROM User user WHERE user.Email = :email", User.class)
.setParameter("email", Email)
.getSingleResult();
}
}
это служба:
package aututor.apps.Services.ServicesImpl;
import aututor.apps.DAO.Model.User;
import aututor.apps.DAO.Repository.IRepository.IUserRepository;
import aututor.apps.DTO.Mapper.UserMapper;
import aututor.apps.DTO.UserDTO;
import aututor.apps.Services.IServices.UserService;
import aututor.apps.Util.Response;
import javax.inject.Singleton;
@Singleton
public class UserServiceImpl implements UserService {
protected final IUserRepository userRepository;
protected final UserMapper userMapper= new UserMapper();
public UserServiceImpl(IUserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public Response CreateUser(UserDTO User) {
if(userRepository.findByEmail(User.getEmail())==null) {
User u = userRepository.save(userMapper.ToUser(User));
return new Response("request has succeeded",userMapper.ToUserDTO(u),200);
}
else {
return new Response("Not Acceptable : Adress email already exist",null,406);
}
}
@Override
public Response UpdateUser(UserDTO User) {
return null;
}
@Override
public Response DeleteUser(UserDTO User) {
return null;
}
@Override
public Response FindUserByID(Long Id) {
return null;
}
}
Контроллер:
package aututor.apps.Controllers;
import aututor.apps.DTO.UserDTO;
import aututor.apps.Services.IServices.UserService;
import aututor.apps.Util.Response;
import io.micronaut.http.MediaType;
import io.micronaut.http.annotation.*;
@Controller("/rest/User")
public class UserController {
protected final UserService service ;
public UserController(UserService service) {
this.service = service;
}
@Post("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response CreateUser(@Body UserDTO user) {
return service.CreateUser(user);
}
}
build.gradle:
dependencies {
annotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
annotationProcessor "io.micronaut:micronaut-inject-java"
annotationProcessor "io.micronaut:micronaut-validation"
annotationProcessor "io.micronaut.data:micronaut-data-processor:1.0.0"
implementation platform("io.micronaut:micronaut-bom:$micronautVersion")
implementation "io.micronaut:micronaut-security-jwt"
implementation "io.micronaut:micronaut-http-client"
implementation "io.micronaut:micronaut-validation"
implementation "io.micronaut:micronaut-http-server-netty"
implementation "io.micronaut.configuration:micronaut-hibernate-jpa"
implementation("io.micronaut:micronaut-spring")
compile "io.micronaut.configuration:micronaut-jdbc-tomcat"
compile "io.micronaut:micronaut-runtime"
compile "io.micronaut:micronaut-inject"
compile("io.micronaut.data:micronaut-data-hibernate-jpa")
compile("io.micronaut.data:micronaut-data-jdbc")
compile("org.postgresql:postgresql:42.2.1")
runtimeOnly "ch.qos.logback:logback-classic:1.2.3"
testAnnotationProcessor platform("io.micronaut:micronaut-bom:$micronautVersion")
testAnnotationProcessor "io.micronaut:micronaut-inject-java"
testImplementation platform("io.micronaut:micronaut-bom:$micronautVersion")
testImplementation "org.junit.jupiter:junit-jupiter-api"
testImplementation "io.micronaut.test:micronaut-test-junit5"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine"
}
application.yml:
---
micronaut:
application:
name: apps
server:
port: 8089
---
datasources:
default:
url: jdbc:postgresql://localhost:5432/db
username: root
password: ''
schema-generate: CREATE_DROP
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: update
dialect: org.hibernate.dialect.PostgreSQL95Dialect
entity-scan:
packages: 'apps.model'
Я пытался работать с CrudRepository и там тоже была проблема. Все выглядит правильно, я не знаю, пропустил ли я что-то.