Spring 相关注解
spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。
在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。
虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。
所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。
@Service 用于标注业务层组件
@Controller 用于标注控制层组件(如struts中的action)
@Repository 用于标注数据访问组件,即DAO组件
@Component 泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。@RestController
为了方便Rest开发,通过新的 @RestController 指定在控制器上,就不需要在每个 @RequestMapping 方法上加 @ResponseBody 了。
可以这样理解:
- 单独使用 @Controller,页面进行跳转。
- 使用 @Controller + @ResponseBody,将 return 的值写入Response对象的body数据区,用来返回 json、xml 等格式的数据。
- @RestController = @Controller + @ResponseBody。
@RequestMapping
此注解提供路由信息。@ResponseBody
作用:
该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。
使用时机:
返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
- 这样配置:
@RequestMapping("[url]")
@ResponseBody
将会在访问的页面上输出字符串,而不会跳转到指定的页面。
只需删除注解:@ResponseBody 便可以返回页面,且不会在页面上输出字符串。
@Autowired 自动装配
Spring 2.5 引入了 @Autowired 注解,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。@EnableAutoConfiguration
此注解的作用:让 Spring Boot 根据应用所声明的依赖来对 Spring 框架进行自动配置,这就减少了开发人员的工作量。
如果配置了 spring-boot-starter-web,会添加 Tomcat 和 Spring MVC,所以auto-configuration将假定你正在开发一个web应用,并对Spring进行相应地设置。@SpringBootApplication
注解等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan@Import
用来导入其他配置类。@ImportResource
用来加载xml配置文件。@Bean
用@Bean标注方法等价于XML中配置的bean。@Value
注入Spring boot application.properties配置的属性的值。@Qualifier
@Qualifier限定描述符除了能根据名字进行注入,还能进行更细粒度的控制如何选择候选者,具体使用方式如下:
@Autowired
@Qualifier(value = "demoInfoService")
private DemoInfoService demoInfoService;
- @Inject
等价于默认的@Autowired,只是没有required属性。
Spring 相关注解的更多相关文章
- spring相关注解
spring相关注解: 使用之前需要<context:annotation-config/>在配置文件中启用 @Required 应用于类属性的set方法,并且要求该属性必须在xml容器里 ...
- spring 相关注解详情(一)
1.@controller 控制器(注入服务) 用于标注控制层,相当于struts中的action层2.@service 服务(注入dao) 用于标注服务层,主要用来进行业务的逻辑处理3.@repos ...
- spring 相关注解详情(二)
@Configuration用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法 /* 数据源的属性类 */ public class DataSourcePro ...
- Spring框架系列(七)--Spring常用注解
Spring部分: 1.声明bean的注解: @Component:组件,没有明确的角色 @Service:在业务逻辑层使用(service层) @Repository:在数据访问层使用(dao层) ...
- Spring - Spring 常用注解
概述 简单整理一些 Spring 的注解 这个算是一个 水一波 类型的整理 内容不全 分类可能有的地方不会太符合逻辑 而且时间也不太充裕 先把自己想写的写下来, 然后随缘整理吧 约定 版本 Sprin ...
- Spring bean依赖注入、bean的装配及相关注解
依赖注入 Spring主要提供以下两种方法用于依赖注入 基于属性Setter方法注入 基于构造方法注入 Setter方法注入 例子: public class Communication { priv ...
- SpringMVC中与Spring相关的@注解
一.Spring的常用组件类注解 @Component 被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Repository.在 ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- Spring中与Spring相关的注解
# 一.Spring的常用组件类注解 ## @Component 被该注解所修饰的类是一个普通的spring bean类,该注解可以替代@Controller.@Service.@Reposi ...
随机推荐
- HDOJ1009
#include "iostream" #include "algorithm" #include "cstdio" using names ...
- transition多个属性同时渐变(left/top)
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- forget words out1
forget word 1● information 2● infomation 3● mation 4● pavilion 5● river 6● mouth 7● fish 8● lick 9● ...
- 014PHP文件处理——文件指针控制fseek rewind ftell feof fpassthru
<?php /** * 文件指针控制fseek rewind ftell feof fpassthru */ //feof()判断文件读取是否超出文件长度 /*$file = fopen('a. ...
- SQL Server 调优系列基础篇 - 索引运算总结
前言 上几篇文章我们介绍了如何查看查询计划.常用运算符的介绍.并行运算的方式,有兴趣的可以点击查看. 本篇将分析在SQL Server中,如何利用先有索引项进行查询性能优化,通过了解这些索引项的应用方 ...
- python函数def
#函数就是实现一个特定功能的程序块 #下面这个函数实现了打印helloworld的功能 def print_hello(): print('hello world!') #这是调用方法 print_h ...
- js根据选中的复选框,隐藏那一行
如图,选择复选框,点击“隐藏”按钮,隐藏选中行 1.JavaScript代码: function getCheckedIds() { var checkedSubject = $('#showSbgl ...
- html头标签meta实现refresh重定向
<html> <head> <meta http-equiv="content-type" content="text/html; char ...
- 了解SAGA
萨迦(尤指古代挪威或冰岛讲述冒险经历和英雄业绩的长篇故事)
- anu - reactIE
import { options } from "./util"; import { Children } from "./Children"; import ...