springboot的UnsatisfiedDependencyException异常问题

文章最前: 我是Octopus,这个名字来源于我的中文名--章鱼;我热爱编程、热爱算法、热爱开源。所有源码在我的个人github ;这博客是记录我学习的点点滴滴,如果您对 Python、Java、AI、算法有兴趣,可以关注我的动态,一起学习,共同进步。

相关文章:

  1. DuplicateKeyException异常处理:java向数据库插入数据异
  2. springboot的UnsatisfiedDependencyException异常问题
  3. org.springframework.dao.DataIntegrityViolationException
  4. com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'current_state'
  5. java中出现这种错误: "error": "Internal Server Error",
  6. The Tomcat connector configured to listen on port 8888 failed to start
  7. java.nio.charset.MalformedInputException错误解决
  8. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.geekplus.dao

文章目录:

1.问题报错

2.问题描述:

3.解决问题:


1.问题报错


org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.geekplus.hephaestus.wms.shelfscore.ShelfScoreTests2': Unsatisfied dependency expressed through field 'shelfScoreSyn'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.geekplus.hephaestus.wms.api.shelfscoring.ShelfScoreSyn' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}


2.问题描述:


遇到这个问题,首先提示Dependency annotations

package com.geekplus.hephaestus.wms.shelfscore;

import com.geekplus.hephaestus.library.sys.AlgoSysConfigUtils;
import com.geekplus.hephaestus.wms.HephaestusWmsApiApplication;
import com.geekplus.hephaestus.wms.api.shelfscoring.ShelfScoreFacade;
import com.geekplus.hephaestus.wms.api.shelfscoring.ShelfScoreSyn;
import com.geekplus.hephaestus.wms.api.shelfscoring.entity.ShelfScoreResult;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author: zhangyu
 */

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {HephaestusWmsApiApplication.class})  // 指定启动类
@ComponentScan(basePackages = {"com.geekplus"})
public class ShelfScoreTests2 {

    @Before
    public void prepare() {
        AlgoSysConfigUtils.setValue("hephaestus.debug.enabled", "true");
    }


    @Autowired
    private ShelfScoreSyn shelfScoreSyn;

    @Test
    public void testUpdateShelfScore() {
        shelfScoreSyn.execute();
    }
}

里面引用了, private ShelfScoreSyn shelfScoreSyn;但是在另外一个类中:

package com.geekplus.hephaestus.wms.api.impl.shelfscoring;

import com.geekplus.hephaestus.dataplatform.common.dao.rms.AthenaBaseShelfMapper;
import com.geekplus.hephaestus.dataplatform.common.dao.wms.BaseShelfMapper;
import com.geekplus.hephaestus.dataplatform.common.entity.rms.AthenaBaseShelf;
import com.geekplus.hephaestus.dataplatform.common.entity.wms.BaseShelf;
import com.geekplus.hephaestus.library.sys.AlgoSysConfigUtils;
import com.geekplus.hephaestus.wms.api.shelfscoring.ShelfScoreSyn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 将rms的货架数据同步到wms货架数据上
 *
 */
// @Service
public class ShelfScoreSynImpl implements ShelfScoreSyn {
    // 查询rms的货架数据同步到wms货架数据上
    @Autowired
    private AthenaBaseShelfMapper athenaBaseShelfMapper;

    @Autowired
    private BaseShelfMapper baseShelfMapper;

    @Override
    public void execute() {
        /*
           1.从数据库查询货架位置信息
           2.把数据插入到wms
           3.返回插入的值
         */
        List<AthenaBaseShelf> athenaBaseShelfList = athenaBaseShelfMapper.selectBaseShelf();
        Map<String, AthenaBaseShelf> athenaBaseShelfMap = athenaBaseShelfList.stream().collect(Collectors.toMap(AthenaBaseShelf::getShelfCode, e -> e));
        List<BaseShelf> baseShelfList = baseShelfMapper.selectWmsBaseShelf();
        List<BaseShelf> updatedBaseShelfList = updateBaseShelf(athenaBaseShelfMap, baseShelfList);
        for (BaseShelf baseShelf : updatedBaseShelfList) {
            System.out.println(baseShelf);
        }
    }

    private List<BaseShelf> updateBaseShelf(Map<String, AthenaBaseShelf> athenaBaseShelfMap, List<BaseShelf> baseShelfList) {
        int shiftParameter = AlgoSysConfigUtils.getIntegerValue("hephaestus.shelfscoring.shiftParameter", 1000);
        for (BaseShelf baseShelf : baseShelfList) {
            if (athenaBaseShelfMap.keySet().contains(baseShelf.getShelfCode())) {
                AthenaBaseShelf athenaBaseShelf = athenaBaseShelfMap.get(baseShelf.getShelfCode());
                if (athenaBaseShelf.getLocationX() != null || athenaBaseShelf.getLocationY() != null || athenaBaseShelf.getLocationX() > 0 || athenaBaseShelf.getLocationY() > 0) {
                    int shiftLocationX = athenaBaseShelf.getLocationX() * shiftParameter;
                    int shiftLocationY = athenaBaseShelf.getLocationY() * shiftParameter;
                    if (!(baseShelf.getLocax() == shiftLocationX && baseShelf.getLocay() == shiftLocationY)) {
                        baseShelf.setLocax(shiftLocationX);
                        baseShelf.setLocay(shiftLocationY);
                        baseShelfMapper.updateWmsBaseShelf(baseShelf);
                    }
                }
            }
        }
        return baseShelfList;
    }
}

3.解决问题:


它没有交给spring进行管理:ShelfScoreSynImpl ,在上面加个注释就好了,把它交给spring管理。

当加了@service之后,就没有这种异常了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
引用\[1\]中提到了四个异常,分别是UnsatisfiedDependencyException、BeanCreationException、PropertyBatchUpdateException和MethodInvocationException。其中,UnsatisfiedDependencyException是BeanCreationException的子类。UnsatisfiedDependencyException表示在创建Bean时,存在依赖关系无法满足的情况。BeanCreationException表示在创建Bean时发生了异常。PropertyBatchUpdateException表示在更新属性时发生了异常。MethodInvocationException表示在调用方法时发生了异常。 引用\[2\]中提到了解决UnsatisfiedDependencyException的方式。可以在SpringBoot启动类上加上@MapperScan注解,并指定mapper接口层的包路径。这样可以解决依赖关系无法满足的问题。 引用\[3\]中提到了一些内容可以用来判断是否是连接池错误。可以检查applicationContext.xml文件中的dataSource配置,确保配置正确。比如,可以检查driverClass、jdbcUrl、user和password等属性的配置是否正确。 所以,public class UnsatisfiedDependencyException extends BeanCreationException是表示在创建Bean时存在依赖关系无法满足的异常。可以通过在SpringBoot启动类上加上@MapperScan注解来解决这个异常,并且还可以检查连接池的配置是否正确。 #### 引用[.reference_title] - *1* *3* [spring整合mybatis异常 UnsatisfiedDependencyException BeanCreationException ...](https://blog.csdn.net/weixin_45574920/article/details/113357684)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [项目启动时报UnsatisfiedDependencyException异常,没有bean可用或bean注入失败](https://blog.csdn.net/qq_42567801/article/details/100559343)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值