记一次Spring boot搭建过程

遇到的问题如下:

1.Spring Boot正常启动后,访问Controller报404
问题描述:

spring boot正常启动,通过 http://localhost:8000/hello/first 访问,一直报404

原因:

在搭建完项目之后,Application类是放在com.example.hello的包下面,而Controller类是放置在com.example.controller的包下面,导致spring boot无法扫描controller包下的内容(默认扫Application类对应的包下的内容)
解决措施:

方法1:将controller包下的类移动到hello包下

方法2:在启动上方添加@ComponentScan注解,此注解为指定扫描路径,例如:@ComponentScan(basePackages = {"com.example.controller"})

package com.example.hello;

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

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.controller"})
public class HelloApplication {

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

}

2.无法注入继承JpaRepository的接口
问题描述:

如下代码该接口在继承JpaRepository后,在controller类中通过@Autowired注入时,工程一直无法启动,并报
Parameter 0 of constructor in com.example.controller.ReadingListController required a bean of type 'com.example.model.ReadingListRepository' that could not be found.

记一次Spring boot搭建过程

package com.example.model;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ReadingListRepository extends JpaRepository<Book, Long> {

    List<Book> findByReader(String reader);
}

package com.example.controller;

import com.example.model.Book;
import com.example.model.ReadingListRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/readingList")
public class ReadingListController {

    ReadingListRepository readingListRepository;

    @Autowired
    public ReadingListController(ReadingListRepository readingListRepository) {
        this.readingListRepository = readingListRepository;
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.GET)
    public String readersBooks(@PathVariable("reader") String reader, Model model) {
        List<Book> readingList = readingListRepository.findByReader(reader);
        if (readingList != null) {
            model.addAttribute("books", readingList);
        }
        return "readingList";
    }

    @RequestMapping(value = "/{reader}", method = RequestMethod.POST)
    public String addToReadingList(@PathVariable("reader") String reader, Book book) {
        book.setReader(reader);
        readingListRepository.save(book);
        return "redirect:/readingList/{reader}";
    }
}

原因:

Springboot未能正常将其扫描并没注入到容器中。而且一般在使用Springboot的初始框架中,启动类位置于所有Service,Entity,Controller或者其它类的最上层的话,这个问题很少会出现。

解决措施:

方案一、把 @SpringBootApplication 注解的 SpringBoot 入口类移到上层 root 包中,使 JpaRepository 子接口位于 root 包及其子包中。

方案二、在 SpringBoot 入口类上添加

    (1) @ComponentScan(basePackages = "xxx.xxx.xxx"):扫描 @Controller、@Service 注解;
    (2) @EnableJpaRepositories(basePackages = "xxx.xxx.xxx"):扫描 @Repository 注解;
    (3) @EntityScan(basePackages = "xxx.xxx.xxx"):扫描 @Entity 注解;

猜你喜欢

转载自blog.51cto.com/14402006/2409528