admin管理员组

文章数量:1794759

Spring Boot Consider defining a bean of type `xxx` in your configuration 错误6种情况解决(Spring、Dubbo、JPA等)

Spring Boot Consider defining a bean of type `xxx` in your configuration 错误6种情况解决(Spring、Dubbo、JPA等)

1 问题描述

Consider defining a bean of type 'com.service.UserService' in your configuration.

2 问题分析 2.1 Spring

1、已经使用@Service注解定义了UserService的实现类,但是Spring没有扫描到。

解决方法:

(1)将当前模块的Dao类、Service类、Entity类、Controller类放在和XxxApplication启动类同一目录下或者子目录下。

(2)在xxxApplication启动类加上 @ComponentScan("com") 注解。

2、UserService启动装配时变量名默认为实现类的名字userServiceImpl,实现类是UserServiceImpl类,而我们使用@Autowired注解时变量名为userService。

解决办法:

将实现类的@Service改为@Service("userService")。

2.2 OpenFeign

1、使用了OpenFeign来调用其他微服务的接口,但是OpenFeign相关的jar包没有完成导入。

解决办法请参考以下博客。

Feign 出现Failed to introspect Class [org.springframework.cloud.openfeign.FeignClientFactoryBean问题解决

2、使用OpenFeign来调用其他微服务的接口时,XxxApplication启动类未加@EnableFeignClinets注解。

SpringBoot 配置OpenFeign的详细步骤请参考以下博客。

Spring Cloud openfeign使用

2.3 JPA

1、使用JPA框架创建Dao类时,未继承JpaRepository和JpaSpecificationExecutor类。

解决办法:继承JpaRepository和JpaSpecificationExecutor类。

package com.dao; import com.MsgTemplate; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Repository; @Repository public interface MsgTemplateDao extends JpaRepository<MsgTemplate, String>, JpaSpecificationExecutor<MsgTemplate>{ }

 3、由于要自定义Dao类,这个自定义Dao类跟其他Dao类在类的定义上会不一样,如果自定义Dao类跟其他Dao类在类的定义上一样,会导致报错。

package com.dao; import org.springframework.stereotype.Repository; @Repository public interface CommonRepository { } package com.dao.impl; import com.dao.CommonRepository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; public class CommonRepositoryImpl implements CommonRepository { @PersistenceContext private EntityManager entityManager; }

解决办法:

        将两个Dao类合并成一个Dao类即可。

package com.dao; import org.springframework.stereotype.Repository; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; @Repository public class CommonRepository { @PersistenceContext private EntityManager entityManager; } 2.4 Spring Security 

1、使用Spring Security做权限管理的时候,无法直接注入authenticationManager 。

解决办法:在WebSecurityConfigurerAdapter的实现类当中,重写authenticationManagerBean方法,就可以注入authenticationManager 了。

/** * 解决 无法直接注入 AuthenticationManager * * @return * @throws Exception */ @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } 2.5 Dubbo 

1、使用了Dubbo的@Service注解,导致自动装配失败。

解决办法:添加以下注解,代表当前类是Spring容器确保自动装配能够成功。

import org.springframework.stereotype.Component; @Component @org.springframework.stereotype.Service 2.6 Elasticsearch

1、实例化的时候用到了elasticsearchTemplate,但是elasticsearchTemplate没有实例化。

解决办法:创建Elasticsearch配置类,在配置类中实例化ElasticsearchTemplate。具体做法请查看以下博客。

Spring Boot配置Elasticsearch6(缓存数据库)添加、查询、更新、删除数据操作

本文标签: 错误情况definingbeanboot