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

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)}


遇到这个问题,首先提示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();
    }
}

里面引用了,@Autowired  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 athenaBaseShelfList = athenaBaseShelfMapper.selectBaseShelf();
        Map athenaBaseShelfMap = athenaBaseShelfList.stream().collect(Collectors.toMap(AthenaBaseShelf::getShelfCode, e -> e));
        List baseShelfList = baseShelfMapper.selectWmsBaseShelf();
        List updatedBaseShelfList = updateBaseShelf(athenaBaseShelfMap, baseShelfList);
        for (BaseShelf baseShelf : updatedBaseShelfList) {
            System.out.println(baseShelf);
        }
    }

    private List updateBaseShelf(Map athenaBaseShelfMap, List 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;
    }
}

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

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

你可能感兴趣的:(java)