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 configuratio 2 问题分析 2 1 Spring 1 已经使用 Service 注解定义了 UserService 的实现类 但是 Spring 没有扫描到

大家好,我是讯享网,很高兴认识大家。

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(缓存数据库)添加、查询、更新、删除数据操作

小讯
上一篇 2025-01-14 12:36
下一篇 2025-03-09 10:29

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/19505.html