解决报错:@org.springframework.beans.factory.annotation.Autowired(required=true)

先把问题贴出来:

@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> {

}

**

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);
    }

}

解决办法:

1、先看sevice层,你有没有加上@Service注解。
2、再看mapper层有没有加上@Mapper注解,以及在启动类上有没有加上@MapperScan来扫描mapepr

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值