Spring boot扫描包@ComponentScan 、@EnableJpaRepositories、@EntityScan

package com.reptile.springboot.start; /**

  • Alipay.com Inc. Copyright © 2004-2019 All Rights Reserved.
    */

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
*

  • @author Administrator

  • @version $Id: TestApplication.java, v 0.1 2019年07月01日 20:14 Administrator Exp $
    /
    @SpringBootApplication
    @RestController
    @ComponentScan(basePackages = "com.reptile.springboot.
    ")
    @EnableJpaRepositories(basePackages = “com.reptile.springboot.dao”)
    @EntityScan(basePackages = “com.reptile.springboot.entity”)
    public class Application {

    //@RequestMapping("/")
    //public String helloSpring(){
    // return “hello spring boot”;
    //}

    public static void main(String [] agrs){

     SpringApplication.run(Application.class,agrs);
    

    }
    }
    @ComponentScan(basePackages = “com.reptile.springboot.*”)
    是扫描@Controller @Service 等注解的
    @EnableJpaRepositories(basePackages = “com.reptile.springboot.dao”)
    是扫描@Repository注解的 就是Dao 接口继承 JpaRepository
    @EntityScan(basePackages = “com.reptile.springboot.entity”)
    是扫描@Entity 的注解, 扫描实体类

如果不配置这个三个注解 会报找不到相应的bean

你可能感兴趣的:(spring)