为什么这个 Spring Boot Web 应用程序不需要 @Repository?

2024-04-19

我正在学习 Spring Boot 和 JPA、Spring Data Rest、H2 数据库,并且我找到了一个教程。我试图理解它,这是一个简单的例子,但我不明白一些东西。为什么没有必要放@Repository or @Component在 AlienRepo 类中?

repo 对象被注入到外星人控制器类,我从之前的教程中知道你需要使用@Component or @Repository。我认为使用这个注释是强制性的。

控制器:

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

}

POJO:

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

}

您可以在外星人仓库接口没有@Repository or @Component并且代码运行成功。

谢谢你!

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

我想从数据库中获取数据,并添加以下代码。我想问你为什么@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;
}

你注意到了吗,Crud存储库 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html has @NoRepositoryBean https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/NoRepositoryBean.html注释和这个注释使得Crud存储库 https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html一个中间接口。中间接口基本上是添加 派生存储库的一般功能。

如果有任何类注释为@NoRepositoryBeanSpring Data 不会在运行时创建实例。因为它是中间类,创建它是为了为派生类添加功能。

如果你扩展一个接口@NoRepositoryBean并且您的派生接口没有@NoRepositoryBeanSpring 知道您正在尝试在真正的存储库接口中使用该功能,因此添加@存储库 https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/stereotype/Repository.html注释很冗长。


编辑:为什么@RequestParam不是强制性的?

From 官方文档 https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-methods你可以看到“控制器方法参数”表,该表的最后一行显示:

任何其他论点:

如果方法参数与上述任何参数都不匹配,则默认情况下 被解析为@RequestParam https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam如果它是一个简单类型,如 取决于BeanUtils#isSimpleProperty https://docs.spring.io/spring-framework/docs/5.0.8.RELEASE/javadoc-api/org/springframework/beans/BeanUtils.html#isSimpleProperty-java.lang.Class-,或作为 否则@ModelAttribute。

因此,如果您省略“控制器方法参数”的注释并且不编写类似的内容@MatrixVariable https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-matrix-variables, @路径变量 https://docs.spring.io/spring/docs/current/spring-framework-refe or @RequestParam https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestparam。 默认是a@RequestParam.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么这个 Spring Boot Web 应用程序不需要 @Repository? 的相关文章

随机推荐