代码之家  ›  专栏  ›  技术社区  ›  elvis

为什么这个Spring Boot web应用不需要@Repository?

  •  4
  • elvis  · 技术社区  · 6 年前

    我正在学习Spring Boot和JPA,Spring Data Rest,H2数据库,我找到了一个教程。我试着去理解它,这是一个简单的例子,但我不明白什么。为什么没有必要 @Repository @Component 在AlienRepo班?

    回购对象被注入 自动控制器 @组成部分 @存储库

    控制器:

    package com.dgs.springbootjpa.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.dgs.springbootjpa.dao.AlienRepo;
    import com.dgs.springbootjpa.model.Alien;
    
    @Controller
    public class AlienController {
    
        @Autowired
        AlienRepo repo;
    
        @RequestMapping("/")
        public String home() {
    
            return "home.jsp";
        }
    
        @RequestMapping("/addAlien")
        public String addAlien(Alien alien) {
    
            repo.save(alien);
            return "home.jsp";
        }
    
    }
    

    波乔:

    package com.dgs.springbootjpa.model;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    
    @Entity
    public class Alien {
    
        @Id
        private int aid;
    
        private String aname;
    
        public int getAid() {
            return aid;
        }
        public void setAid(int aid) {
            this.aid = aid;
        }
        public String getAname() {
            return aname;
        }
        public void setAname(String aname) {
            this.aname = aname;
        }
    
        @Override
        public String toString() {
    
            return "Alien [aid=" + aid + ", aname=" + aname + "]";
        }
    }
    

    dao接口:

    package com.dgs.springbootjpa.dao;
    
    import org.springframework.data.repository.CrudRepository;
    
    import com.dgs.springbootjpa.model.Alien;
    
    public interface AlienRepo extends CrudRepository<Alien, Integer> {
    
    }
    

    你可以在 阿利恩雷波 接口没有 @存储库 代码运行成功。

    更新---------------------------------------------

    我想从数据库中获取数据并添加以下代码。我想问你为什么@RequestParam不是强制性的?如果我删除@RequestParam,应用程序将成功运行。

    @RequestMapping("/getAlien")
    public ModelAndView addAlien(@RequestParam int aid) {
    
        ModelAndView mv = new ModelAndView("showAlien");
        Alien alien = repo.findById(aid).orElse(new Alien()); 
        mv.addObject(alien); 
        return mv;
    }
    
    4 回复  |  直到 6 年前
        1
  •  5
  •   Muhammed Ozdogan    5 年前

    你注意到了吗, CrudRepository @NoRepositoryBean 注释和此注释使 粗糙的 派生存储库的常规功能。

    如果有用注释的类 @NoRepositoryBean Spring数据不会在运行时创建实例。因为它是中间类,创建它是为了为派生类添加功能。

    如果扩展的接口 @无柄菜豆 你的派生接口没有 @无柄菜豆 @Repository


    编辑:为什么@RequestParam不是必需的?

    the official documentation 您可以看到“Controller method argument”表,表的最后一行显示:

    任何其他论点:

    如果方法参数与上述任何参数都不匹配,则默认情况下 作为 @RequestParam 决定者 BeanUtils#isSimpleProperty ,或作为 @否则为ModelAttribute。

    因此,如果省略“Controller Method Argument”的注释,并且没有编写 @MatrixVariable @PathVariable @请求参数 这是默认的 @RequestParam .

        2
  •  2
  •   dunni    6 年前

    @Component @Repository 可以在类上用作SpringBean。然而, AlienRepo 不是类,而是扩展Spring数据接口的接口。Spring数据不使用注释,它通过扫描类路径和查看接口层次结构来检测接口,以查看是否扩展了Spring数据接口。如果是这样,它会在运行时为每个接口创建一个实现,然后作为Spring Bean添加到应用程序上下文中。这就是为什么在Spring数据接口上不需要任何注释的原因。

        3
  •  2
  •   Mạnh Quyết Nguyễn    6 年前

    你的 @SpringBootApplication 产生 @EnableAutoConfiguration 注释:

    @EnableAutoConfiguration
    public @interface SpringBootApplication
    

    这个注释将猜测并自动配置bean。

    Spring数据JPA存储库的自动配置。 当上下文中配置了DataSource类型的bean、Spring Data JPA JpaRepository类型在类路径上且没有配置其他现有JpaRepository时激活。

    这个配置类将在Hibernate自动配置之后激活。

    public class JpaRepositoriesAutoConfiguration {
    
    }
    

    添加数据jpa依赖项时, JpaRepositoriesAutoConfiguration 触发并带来 @EnableJpaRepositories

    Your repository then auto-configured:

    Spring数据存储库通常从存储库或原始接口扩展。如果使用自动配置,则会从包含主配置类(用@EnableAutoConfiguration或@springbootsapplication注释的配置类)的包中搜索存储库。

        4
  •  1
  •   Andrew    6 年前

    AlienRepo 类扩展 CrudRepository ,因此Spring将能够浏览配置中指定的包并查找存储库。

    @Autowired 如果只有一个构造函数 AlienController(AlienRepo) ,Spring也能找到答案。