您现在的位置是:首页 >技术杂谈 >记录一次工作遇到的问题网站首页技术杂谈

记录一次工作遇到的问题

Java-请多指教 2023-05-24 16:00:03
简介记录一次工作遇到的问题

一个A微服务引入另一个B微服务的依赖后(即maven中引入另一个B服务)

A服务启动报错:

1.报错druid没有配置数据源

Failed to bind properties under 'spring.datasource.type' to java.lang.Class<javax.sql.DataSource>:

    Property: spring.datasource.type
    Value: org.apache.tomcat.jdbc.pool.DataSource
    Origin: class path resource [application.properties]:5:24
    Reason: No converter found capable of converting from type [java.lang.String] to type [java.lang.Class<javax.sql.DataSource>]

Action:

Update your application's configuration

解决:加入数据源依赖

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>

2.继续启动,又报错没有mysql驱动

<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<!--阿里巴巴连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>

3.继续启动,又报错注入的bean找不到

A服务中注入了B服务的Service

 @Autowired
    TokenService tokenService;

提示无法装配Bean

报错:Field xxxService in com.xxx required a bean of type ‘com.xxx‘ that could not be found

解决:ComponentScan指定扫描包

A服务的主类中添加扫描包,扫描B服务的接口路径

@ComponentScan(basePackages = {"com.webber.cm.token.service"}

4.继续启动,启动不报错了,但是前台调A服务的接口时报404

原因:@ComponentScan(basePackages = {"com.webber.cm.token.service"}

这样做是有问题的,我发现启动后服务不能正常访问。查找资料后发现是因为@ComponentScan 和@SpringBootApplication注解的包扫描有冲突,@ComponentScan注解包扫描会覆盖掉@SpringBootApplication的包扫描

解决办法就是在@ComponentScan(basePackages={“com.whu.commom.redis”})的基础上加上@SpringBootApplication扫描的包

@ComponentScan(basePackages={“A服务扫描路径”,“B服务扫描路径”})

终于所有的问题都解决了!!!!!

风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。