admin管理员组

文章数量:1794759

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

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

本文标签: defining踩坑SpringBootbeanconfigurat