先把问题贴出来:
@org.springframework.beans.factory.annotation.Autowired(required=true)
报这个错是因为:
@Autowired(required=true):当使用@Autowired注解的时候,其实默认就是@Autowired(required=true),表示注入的时候,该bean必须存在,否则就会注入失败。
Mapper层
package com.yyyy.eamon.dao;
import tk.mybatis.mapper.common.Mapper;
import com.yzym.eamon.domain.Community;
//import org.springframework.stereotype.Repository;
//import com.yzym.eamon.service.impl.CommunityServiceImpl;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//
//@Configuration //自动注入,程序一开始就注册实例
public interface CommunityMapper extends Mapper<Community> {
}
**文章来源:https://www.toymoban.com/news/detail-641953.html
Service层
**
package com.yzym.eamon.service.impl;
import com.yzym.eamon.dao.CommunityMapper;
import com.yzym.eamon.domain.Community;
import com.yzym.eamon.service.CommunityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//
//@Configuration //自动注入,程序一开始就注册实例
@Service
public class CommunityServiceImpl implements CommunityService {
@Autowired
private CommunityMapper communityMapper;
@Override
public List<Community> findAll(){
List<Community> communities = communityMapper.selectAll();
return communities;
}
}
启动类
package com.yzym.eamon;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
//@SpringBootApplication
//MapperScan要导入Tk包
//@MapperScan(basePackages = {"com.yzym.eamon.dao "})
@MapperScan(basePackages = "#####") //这里填自己的地址参考Mapper的package包的位置
@SpringBootApplication
public class EamonApplication {
public static void main(String[] args) {
SpringApplication.run(EamonApplication.class, args);
}
}
解决办法:文章来源地址https://www.toymoban.com/news/detail-641953.html
1、先看sevice层,你有没有加上@Service注解。
2、再看mapper层有没有加上@Mapper注解,以及在启动类上有没有加上@MapperScan来扫描mapepr
到了这里,关于解决报错:@org.springframework.beans.factory.annotation.Autowired(required=true)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!