admin管理员组文章数量:1026989
他奶奶滴。反反复复检查反反复复检查,一下午我就硬是没看出来。
一开始是生命了一个这个接口
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指定优先调用具体的实现类
他奶奶滴。反反复复检查反反复复检查,一下午我就硬是没看出来。
一开始是生命了一个这个接口
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
版权声明:本文标题:Springboot踩坑:Consider defining a bean of type ‘xxx‘ in your configurat 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/jiaocheng/1738334622a1562455.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论