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

编程知识 更新时间:2023-05-02 05:23:47

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

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

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指定优先调用具体的实现类

更多推荐

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

本文发布于:2023-04-26 05:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/2e325a34751dd5f3efe25a0806f442b5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:defining   踩坑   Springboot   bean   configurat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 104358文章数
  • 26212阅读数
  • 0评论数