目录

@Component  组件

@Repository 持久化层

@Service 业务层

@Controller 控制层

重定向 与 服务端跳转


@Component  组件

1、@Controller、@Service、@Repository、@Component 注解的类会纳入 Spring 容器中进行管理,在需要使用的时候,只需要注入即可。

2、@Controller 用于标注控制层组件;@Service 用于标注业务层组件;@Repository 用于标注数据持久化层组件;@Component 泛指组件,用于标注不好归类的组件。

3、默认情况 bean 的名称为类名(首字母小写),可以通过 value 属性自定义 bean 的名称。默认情况下 bean 是单例的,可以使用 @Scope 注解指定。

4、代码举例如下:

@Repository 持久化层

import org.springframework.stereotype.Repository;//默认 bean 的名称为类名首字母小写 BookServiceImpl
@Repository
public class BookRepository {public void deleteById(long id) {System.out.println("根据主键删除 id=" + id);}
}

@Service 业务层

public interface BookService {void deleteById(long id);
}import com.wmx.web_app.repository.BookRepository;
import com.wmx.web_app.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;//使用 value 指定 bean 的名称,默认为类名首字母小写 BookServiceImpl
@Service(value = "bookServiceImpl")
public class BookServiceImpl implements BookService {@Autowired  //根据类型注入private BookRepository bookRepository;@Overridepublic void deleteById(long id) {bookRepository.deleteById(id);}
}

@Controller 控制层

import com.wmx.web_app.service.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;import javax.annotation.Resource;//@RestController 等同于在整个类上加了 @ResponseBody,表示类中所有的方法返回值都直接返回给页面
//对于前后端分离的项目,@RestController 更加方便
@Controller
public class BookController {//根据名称注入@Resource(name = "bookServiceImpl")private BookService bookService;/*** http://192.168.2.77:8080/deleteById?id=99890** @param id* @return* @ResponseBody 注解表示将返回的内容直接输出到页面*/@GetMapping("deleteById")@ResponseBodypublic String deleteById(@RequestParam long id) {bookService.deleteById(id);return "删除成功:" + id;}
}

重定向 与 服务端跳转

forward(服务端跳转) 与 redirect(重定向) 区别

对比项 对比结果
地址栏

1)forword 是服务器内部的转发,服务器直接访问目标地址,客户端并不知道,客户端浏览器的网址不会发生变化。

2)redirect 是服务器根据逻辑,发送一个状态码,告诉浏览器重新去请求新的地址,地址栏显示的是新的地址。

数据共享

1)由于在整个转发过程中用的是同一个 request,因此 forward 会将 request 的信息带到被转发的目标中使用,可以共享数据

2)redirect 重定向无法共享数据,必须重新赋值。

应用情况 1)forword  一般用于用户可以重复访问的时候,比如查询。
2)redirect 一般用于用户不可以重复访问的时候,比如新增后需要重定向到列表页面、注销登录后需要重定向到登陆页面等等。
请求次数

1、forword 请求一次;而 redirect 请求两次。

2、forword 效率高,而 redirect 效率低。

    /*** http://127.0.0.1:8080/redirect* 1、redirect 重定向时相当于浏览器重新发起了新的请求,所以使用 {@link Model} 是无法传递参数的。* 2、重定向可以使用 {@link javax.servlet.http.HttpSession} 或者 {@link RedirectAttributes} 传递参数* 3、也可以拼在 url 地址上,如 ?a=x&b=y** @return*/@GetMapping("/redirect")public String redirect(RedirectAttributes attributes) {System.out.println("重定向到主页");attributes.addAttribute("attr", "厉害");attributes.addAttribute("param", "不得了");return "redirect:/home?code=7845P87";}/*** 跳转到前端页面* 1、跳转到页面,不需要加‘forward’或者‘redirect’。* 2、可以使用 {@link Model} 往前端传递参数。* 3、或者直接使用 Map 往前端传参也行,默认Map的内容会放到请求域中,页面可以直接取值。** @param model* @param code* @param attr* @return*/@GetMapping("/home")public String home(Model model, String code, String attr, String param) {//进入主页面:code=7845P87,attr=厉害System.out.println("进入主页面:code=" + code + ",attr=" + attr + ",param=" + param);model.addAttribute("orderList", Arrays.asList(1, 2, 3, 4, 5, 6));return "/index.html";}/*** http://127.0.0.1:8080/forward?attr=厉害* 1、服务器端跳转可以使用 {@link javax.servlet.http.HttpSession} 传值,也可以拼接在 url 上* 2、前端给本方法传递的参数,在服务器跳转到的目标方法中同样可以获取的值。** @param session* @return*/@GetMapping("/forward")public String forward(HttpSession session, String attr, RedirectAttributes attributes) {session.setAttribute("param", "不得了");attributes.addAttribute("param", "不得了");System.out.println("服务器端跳转到主页");return "forward:/home?code=7845P87";}

src/main/java/com/wmx/reddoor/controller/JumpController.java · 汪少棠/red-door - Gitee.com

Spring 注解 @Controller,@Service,@Repository,@Component,重定向 与 服务端跳转相关推荐

  1. @Controller,@Service,@Repository,@Component详解

    转载自 @Controller,@Service,@Repository,@Component详解 @Controller 用来表示一个web控制层bean,如SpringMvc中的控制器. @Ser ...

  2. spring自动扫描的注解@Component @Controller @Service @Repository

    @Component @Controller @Service @Repository的作用 1.@controller 控制器(注入服务) 2.@service 服务(注入dao) 3.@repos ...

  3. 解释@Component @Controller @Service @Repository

    2019独角兽企业重金招聘Python工程师标准>>> Spring 配置管理 Bean XML .net 对Spring的注解标签刚刚接触,所以就找了几个常用的,记录下,感觉注解用 ...

  4. 基于Spring Security + OAuth2 的SSO单点登录(服务端)

    相关技术 spring security: 用于安全控制的权限框架 OAuth2: 用于第三方登录认证授权的协议 JWT:客户端和服务端通信的数据载体 传统登录 登录web系统后将用户信息保存在ses ...

  5. 破甲两千六 Spring Cloud 教程(三):添加Spring Cloud 的 Netflix Eureka 插件,实现服务端、客户端的发现与注册

    写在前面: Spring Cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等. 5大常用组件: 服务发现 ...

  6. Spring注解之Service详解

    目录 @[TOC](目录) Service注解 Service用法及示例 传统方式是怎么做的呢? @Service注解是怎么体现业务逻辑复用的? 总结 Service注解 @Service 注解是 S ...

  7. spring注解controller示例

    依赖库 spring 3.0 配置web.xml文件如下: <?xml version="1.0" encoding="UTF-8"?> <w ...

  8. java spring框架 注解_史上最全的java spring注解

    史上最全的java spring注解,没有之一 注解是个好东西,但好东西我们也是看见过,整理过,理解过,用过才知道好.不求我们每个都记住,但求保有印象,在需要的时候能提取出来再查找相关资料,平时工作就 ...

  9. 【Spring注解系列07】Spring注入Bean有哪些方式总结

    给Spring容器中注册组件方式: 1).包扫描+组件标注注解(@Controller/@Service/@Repository/@Component) 2).@Bean 导入的第三方包里面的组件 3 ...

  10. spring注解开发:容器中注册组件方式

    1.包扫描+组件标注注解 使用到的注解如下,主要针对自己写的类 @Controller @Service @Repository @Component @ComponentScan 参考 spring ...

最新文章

  1. django创建项目案例1详细介绍方法01
  2. 你以为没有CAD的前辈们画不出复杂的图纸?
  3. RabbitMQ基本概念
  4. 子屏幕selection-screen
  5. 聊聊高并发(二十九)解析java.util.concurrent各个组件(十一) 再看看ReentrantReadWriteLock可重入读-写锁
  6. LeetCode——7. Reverse Integer
  7. 带你掌握二进制SCA检测工具的短板及应对措施
  8. 《硝烟中的Scrum和XP》学习手札
  9. 并发安全的mysql序列实现
  10. [AHK]双击Ctrl+C调用谷歌翻译!
  11. 寻找矩阵行最大列最小元素
  12. Setup Time 、Hold Time、Setup check、Hold check,同步异步 及违例修复
  13. X86 CPU 漏洞 Meltdown 原理及google攻击代码
  14. 电子护照阅读器|酒店机场高铁自助机录入系统
  15. 价值7000万的商业模式,羊毛出在狗身上,猪来买单
  16. “死亡之星”——“阿波非斯”
  17. js监听苹果手机自带按钮返回事件
  18. 状态控件ios 中滑块、开关、分段控件、操作表和警告的常用函数
  19. [ROC-RK3568-PC] 手把手教你编译Linux_SDK并打包Ubuntu系统固件
  20. 九校联考-长沙市一中NOIP模拟Day2T2 走格子(cell)

热门文章

  1. 从程序员到项目经理:原来一切问题都是可以解决的
  2. 推荐12个漂亮的CSS3按钮实现方案
  3. 气泡shader_仿蚂蚁森林气泡
  4. 自己动手写cpu pdf_教你自己动手组装电脑(第一篇:CPU)
  5. 拓端tecdat|数据感知游客的森林公园游憩需求
  6. jQuery学习笔记02
  7. vs2017安装qt
  8. python只想调用函数不想执行.py
  9. 关于FCN代码实现(实践篇)
  10. python求解next数组实现KMP算法