Springboot踩坑:Consider defining a bean of type ‘xxx‘ in your configurat

他奶奶滴。反反复复检查反反复复检查,一下午我就硬是没看出来。

一开始是生命了一个这个接口

package myblog.myblog.service;

import myblog.myblog.po.Type;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public interface TypeService {


    Type saveType(Type type);
    Type getType(Long id);
    Page<Type> listType(Pageable pageable);
    Type updateType(Long id,Type type) throws Exception;
    void deleteType(Long id);

}

然后写了它的实现类

package myblog.myblog.service;

import myblog.myblog.dao.TypeRepository;
import myblog.myblog.po.Type;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

//@Service
public class TypeServiceIMPL implements TypeService{

    @Autowired
    private TypeRepository typeRepository;


    @Override
    public Type saveType(Type type) {
        return typeRepository.save(type);
    }
    @Transactional
    @Override
    public Type getType(Long id) {
        return typeRepository.getOne(id);
    }

    @Transactional
    @Override
    public Page<Type> listType(Pageable pageable) {
        return typeRepository.findAll(pageable);
    }


    @Transactional
    @Override
    public Type updateType(Long id, Type type) throws Exception {
        Type temp=typeRepository.getOne(id);
        if(temp==null)
        {
            throw new Exception("没找到");
        }
        BeanUtils.copyProperties(type,temp);
        return typeRepository.save(temp);
    }
    @Transactional
    @Override
    public void deleteType(Long id) {
        typeRepository.deleteById(id);

    }
}

去运行springboot一直报错,我当时寻思接口上我不是加了@Service吗,然后调用的时候也@Autowired注入了啊,就一直很懵逼的状态。然后想起来,这接口上声明Bean,有个几把用。赶紧把实现类的@Service加上,然后跑通了。

有时候真的能被自己蠢到!发个博客记录一下,焯,!

顺便加一下这个问题 ,spring接口多个实现 会执行哪个?

这里也可以看出Spring在注入的时候是注入的子类和接口的实现类!

多个实现类的时候就要用:@Primary指定优先调用具体的实现类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值