简体   繁体   English

Spring 无法创建 UserService bean

[英]Spring cannot create UserService bean

Dao tier.道层。 I have abstract jpa dao interface, extended interface, and I added abstract implementation, from which I extend other real used implementations.我有抽象 jpa dao 接口,扩展接口,我添加了抽象实现,从中我扩展了其他实际使用的实现。 These classes as follows:这些类如下:

public interface AbstractDao<E> {

    E findById(Long id);

    List<E> findAll();

    void save(E entity);

    void update(E entity);

    void delete(E entity);

    void deleteById(Long entityId);

}
public interface UserDao extends AbstractDao<User> {
}
public abstract class AbstractDaoImpl<E> implements AbstractDao<E> {

   private final Class<E> clazz;

   @PersistenceContext
   protected EntityManager entityManager;

   public AbstractDaoImpl(Class<E> clazz) {
      this.clazz = clazz;
   }

   @Override
   public E findById(Long id) {
      return entityManager.find(clazz, id);
   }

   @SuppressWarnings("unchecked")
   @Override
   public List<E> findAll() {
      return entityManager
              .createQuery("from " + clazz.getName())
              .getResultList();
   }

   @Override
   public void save(E entity){
      entityManager.persist(entity);
   }

   @Override
   public void update(E entity){
      entityManager.merge(entity);
   }

   @Override
   public void delete(E entity) {
      entityManager.remove(entity);
   }

   @Override
   public void deleteById(Long entityId){
      E entity = findById(entityId);
      delete(entity);
   }
}
@Repository
public class UserDaoImpl extends AbstractDaoImpl<User> implements UserDao {
    public UserDaoImpl() {
        super(User.class);
    }
}

Service tier.服务层。 Here I also have abstract service interface, one extended interface (UserService) and its abstract and real implementations:这里我也有抽象服务接口,一个扩展接口(UserService)及其抽象和真实的实现:

public interface AbstractService<E, DTO> {
    E findById(Long id);

    List<E> findAll();

    void save(E entity);

    void update(E entity);

    void delete(E entity);

    void deleteById(Long entityId);

    DTO convertToDTO(E entity);
}
public interface UserService extends AbstractService<User, UserDTO> {
}
@Getter @Setter @AllArgsConstructor
public abstract class AbstractServiceImpl<E, D extends AbstractDao<E>, DTO> implements AbstractService<E, DTO> {

    private D dao;
    private ModelMapper mapper;

    @Override
    public E findById(Long id) {
        return dao.findById(id);
    }

    @Override
    public List<E> findAll() {
        return dao.findAll();
    }

    @Override
    public void save(E entity) {
        dao.save(entity);
    }

    @Override
    public void update(E entity) {
        dao.update(entity);
    }

    @Override
    public void delete(E entity) {
        dao.delete(entity);
    }

    @Override
    public void deleteById(Long entityId) {
        dao.deleteById(entityId);
    }


}
@Service
public class UserServiceImpl extends AbstractServiceImpl<User, UserDao, UserDTO> implements UserService {

    @Autowired
    public UserServiceImpl(UserDao dao, ModelMapper mapper) {
        super(dao, mapper);
    }

    @Override
    public UserDTO convertToDTO(User entity) {
        return getMapper().map(entity, UserDTO.class);
    }
}

In my real project I got many extended interfaces from AbstractDao and AbstractServie.在我的实际项目中,我从 AbstractDao 和 AbstractServie 获得了许多扩展接口。 You can see the actual hierarchy:您可以看到实际的层次结构: 在此处输入图像描述

I can't understand why spring can't create @Service annotated beans and autowire those in my Controllers.我不明白为什么 spring 不能在我的控制器中创建 @Service 注释 bean 并自动装配它们。 Any help would be appreciated.任何帮助,将不胜感激。

I took the liberty to look into your project in github https://github.com/tuanalexeu/JavaSchoolFinalTask我冒昧地在 github https://github.com/tuanalexeu/JavaSchoolFinalTask 中查看您的项目

The problem is how you initialize your spring contexts, The AppConfig context is not read at all.问题是您如何初始化 spring 上下文,根本不读取 AppConfig 上下文。 This context has all your configurations.此上下文具有您的所有配置。

By modifying your initializer to include your AppConfig as root, all beans should be present in the same context.通过修改您的初始化程序以将您的 AppConfig 包含为 root,所有 bean 都应该存在于相同的上下文中。 (You can also choose to have parent -> child contexts as well, but that too should be done in the initializer). (您也可以选择拥有父 -> 子上下文,但这也应该在初始化程序中完成)。 Hope it helps.希望能帮助到你。 Good luck.祝你好运。

public class MainWebAppInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(final ServletContext sc) throws ServletException {

        AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
        root.register(AppConfig.class);
        
        Dynamic servlet = sc.addServlet("dispatcher", new DispatcherServlet(root));
        servlet.setLoadOnStartup(1);
        servlet.addMapping("/");

        sc.addListener(new ContextLoaderListener(root));
        sc.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
                .addMappingForUrlPatterns(null, false, "/*");

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring无法创建bean - Spring cannot create bean 创建无法自动连线的Spring bean - Create a Spring bean that cannot be autowired 名为“userService”的 Bean 应该是“demo.spring.orm.userService.UserService”类型,但实际上是“com.sun.proxy.$Proxy20”类型 - Bean named 'userService' is expected to be of type 'demo.spring.orm.userService.UserService' but was actually of type 'com.sun.proxy.$Proxy20' 在Jhipster中将UserService注入SecurityConfiguration会导致Spring bean循环依赖 - Injecting UserService into SecurityConfiguration in jhipster leads to spring bean circular dependency Spring Security 4无法在Tomcat 7上创建bean FilterChain - Spring Security 4 Cannot create bean FilterChain on Tomcat 7 Spring 使用组件扫描配置 bean - 服务中的字段 userRepository.UserService 需要类型为“repository”的 bean - Spring configuring bean with component scan - Field userRepository in service.UserService required a bean of type 'repository Spring-data:创建名为“mainController”的 bean 时出错:通过字段“userService”表达的不满足的依赖关系 - Spring-data : Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'userService' UserService NullPointerException春季/休眠 - UserService NullPointerException Spring/Hibernate Spring找不到bean - Spring cannot find bean Hibernate无法使用Spring Boot在已编译的jar中创建EntityManagerFactory bean - Hibernate cannot create entityManagerFactory bean in compiled jar with Spring Boot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM