关于springboot启动报错和出现无法注入接口失败问题

本文详细阐述了在SpringBoot项目中遇到的启动报错,涉及排除DataSourceAutoConfiguration、接口注入失败及组件扫描等问题的解决方案。通过实例代码和配置详解,助你快速定位并解决这些问题。

关于springboot启动报错的一些问题

  • 启动报错问题

    首次启动要在启动类,加上@SpringBootApplication(exclude{DataSourceAutoConfiguration.class})

@SpringBootApplication(exclude{DataSourceAutoConfiguration.class})
  • 然后出现dao或者mapper里面的接口注入失败Field personProperties in com.example.controller.HelloController required a bean of type ‘com.example.PersonProperties’ that could not be found.
  1. 在dao层或则mapper里面给接口加上@Mapper和@Component
package com.example.demo.Dao;

import com.example.demo.bean.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component
public interface accoutDao {
    List<Account> findAll();
}

然后引入的包要引入对应

  1. 在启动类加上@ComponentScan(basePackages = “com.example”)扫描example下全部的包,并且去掉

@SpringBootApplication(exclude{DataSourceAutoConfiguration.class})

后面的exclude{DataSourceAutoConfiguration.class}自动注入数据。代码如下:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication()
@ComponentScan(basePackages = "com.example")
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

下面附上我的目录和各层的代码
在这里插入图片描述

实体类Account.java

package com.example.demo.bean;

public class Account {
    private Integer id;
    private String user;
    private String psw;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPsw() {
        return psw;
    }

    public void setPsw(String psw) {
        this.psw = psw;
    }
}

DAO层accoutDao.java

package com.example.demo.Dao;

import com.example.demo.bean.Account;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;

import java.util.List;
@Mapper
@Component
public interface accoutDao {
    List<Account> findAll();
}

Mapper中的accountMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.demo.Dao.accoutDao">

    <select id="findAll" resultType="com.example.demo.bean.Account">
        SELECT * FROM account
    </select>

</mapper>

Service层accountService.java

package com.example.demo.Service;

import com.example.demo.bean.Account;

import java.util.List;

public interface accountService {
    List<Account> findAll();
}

实现类accountServiceImp.java

package com.example.demo.serviceImp;

import com.example.demo.Dao.accoutDao;
import com.example.demo.Service.accountService;
import com.example.demo.bean.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class accountServiceImp implements accountService {

    @Autowired
    private accoutDao ado;

    @Override
    public List<Account> findAll() {
        return ado.findAll();
    }
}

控制层helloController.java,这里使用的是@RestController注解,直接输入内容到游览器

package com.example.demo.Controller;

import com.example.demo.Dao.accoutDao;
import com.example.demo.Service.accountService;
import com.example.demo.bean.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class helloController {

    @Autowired
    private accountService aService;

    @RequestMapping("/hello")
    public List hello(){
        return aService.findAll() ;
    }
}

运行结果
在这里插入图片描述
配置文件application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/testspringboot
    username: root
    password: haroot
    driver-class-name: com.mysql.jdbc.Driver
server:
  port: 8083
mybatis:
  mapper-locations: classpath:Mapper/*.xml
  type-aliases-package: com.example.demo.bean

以上就是我在学习springboot过程中遇到的全部问题,如有问题可以在评论区中留言,大家一起讨论交流。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值