springboot注解大全
springboot注解:
@Service: 注解在类上,表示这是一个业务层bean
@Controller:注解在类上,表示这是一个控制层bean
@Repository: 注解在类上,表示这是一个数据访问层bean
@Component: 注解在类上,表示通用bean ,value不写默认就是类名首字母小写
@Autowired:按类型注入.默认属性required= true;当不能确定 Spring 容器中一定拥有某个类的Bean 时, 可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false), 这等于告诉Spring:在找不到匹配Bean时也不抛出BeanCreationException 异常。@Autowired 和 @Qualifier 结合使用时,自动注入的策略就从 byType 转变byName 了。@Autowired可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以 Spring 不将 @Autowired 和 @Qualifier 统一成一个注释类。
@Resource: 按名称装配
区别:
@Resource默认按照名称方式进行bean匹配,@Autowired默认按照类型方式进行bean匹配
@Resource(importjavax.annotation.Resource;)是J2EE的注解
@Autowired(importorg.springframework.beans.factory.annotation.Autowired;)是Spring的注解
@Configuration:注解在类上,表示这是一个IOC容器,相当于spring的配置文件,java配置的方式。 IOC容器的配置类一般与 @Bean 注解配合使用,用 @Configuration 注解类等价与 XML 中配置 beans,用@Bean 注解方法等价于 XML 中配置 bean。
@Bean: 注解在方法上,声明当前方法返回一个Bean
@Scope:注解在类上,描述spring容器如何创建Bean实例。
(1)singleton: 表示在spring容器中的单例,通过spring容器获得该bean时总是返回唯一的实例
(2)prototype:表示每次获得bean都会生成一个新的对象
(3)request:表示在一次http请求内有效(只适用于web应用)
(4)session:表示在一个用户会话内有效(只适用于web应用)
(5)globalSession:表示在全局会话内有效(只适用于web应用)
在多数情况,我们只会使用singleton和prototype两种scope,如果未指定scope属性,默认为singleton
@Value:注解在变量上,从配置文件中读取。
例如:@Value(value = “#{message}”)
@ConfigurationProperties 赋值,将注解转换成对象。给对象赋值。车险项目:HttpClientSetting类
@Profile:注解在方法类上在不同情况下选择实例化不同的Bean特定环境下生效!!!!!!!!!!!!!!!!!
@SpringBootApplication:@SpringBootApplication=@ComponentScan+@Configuration+@EnableAutoConfiguration:约定优于配置
@EnableAutoConfiguration启用 Spring 应用程序上下文的自动配置,试图猜测和配置您可能需要的bean。自动配置类通常采用基于你的classpath 和已经定义的 beans 对象进行应用。被 @EnableAutoConfiguration 注解的类所在的包有特定的意义,并且作为默认配置使用。通常推荐将 @EnableAutoConfiguration 配置在 root 包下,这样所有的子包、类都可以被查找到。
Enable auto-configuration of the Spring Application Context, attempting to guess and
configure beans that you are likely to need. Auto-configuration classes are usually
applied based on your classpath and what beans you have defined. For example, if you
have {@code tomcat-embedded.jar} on your classpath you are likely to want a
{@link TomcatServletWebServerFactory} (unless you have defined your own
{@link ServletWebServerFactory} bean). When using {@link SpringBootApplication}, the auto-configuration of the context is
automatically enabled and adding this annotation has therefore no additional effect. Auto-configuration tries to be as intelligent as possible and will back-away as you
define more of your own configuration. You can always manually {@link #exclude()} any
configuration that you never want to apply (use {@link #excludeName()} if you don't
have access to them). You can also exclude them via the
{@code spring.autoconfigure.exclude} property. Auto-configuration is always applied
after user-defined beans have been registered.
可以用 @exclude 排除你不想配的bean,自动配置是在你注册你定义的bean之后使用
The package of the class that is annotated with {@code @EnableAutoConfiguration},
usually via {@code @SpringBootApplication}, has specific significance and is often used
as a 'default'. For example, it will be used when scanning for {@code @Entity} classes.
It is generally recommended that you place {@code @EnableAutoConfiguration} (if you're
not using {@code @SpringBootApplication}) in a root package so that all sub-packages
and classes can be searched.
最好放到根路径,都能扫到
Auto-configuration classes are regular Spring {@link Configuration} beans. They are
located using the {@link SpringFactoriesLoader} mechanism (keyed against this class).
Generally auto-configuration beans are {@link Conditional @Conditional} beans (most
often using {@link ConditionalOnClass @ConditionalOnClass} and
{@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
@ComponentScan:注解在类上,扫描标注了@Controller等注解的类,注册为bean 。@ComponentScan 为 @Configuration注解的类配置组件扫描指令。@ComponentScan 注解会自动扫描指定包下的全部标有 @Component注解的类,并注册成bean,当然包括 @Component下的子注解@Service、@Repository、@Controller。
@RestController @RestController 是一个结合了 @ResponseBody 和 @Controller 的注解
@Responsebody 注解表示该方法的返回的结果直接写入 HTTP 响应正文(ResponseBody)中,一般在异步获取数据时使用,通常是在使用 @RequestMapping 后,返回值通常解析为跳转路径,加上@Responsebody 后返回结果不会被解析为跳转路径,而是直接写入HTTP 响应正文中。
@RequestBody
@PathVariable
@RequestParam
两者的作用都是将request里的参数的值绑定到contorl里的方法参数里的,区别在于,URL写法不同。
当请求参数username不存在时会有异常发生,可以通过设置属性required=false解决,例如:
@RequestParam(value="username",required=false)
使用@RequestParam时,URL是这样的:http://host:port/path?参数名=参数值
使用@PathVariable时,URL是这样的:http://host:port/path/参数值
不写的时候也可以获取到参数值,但是必须名称对应。参数可以省略不写
@RequestMapping 和请求报文是做对应的
a:value,指定请求的地址
b:method 请求方法类型 这个不写的话,自适应:get或者post
c:consumes 请求的提交内容类型
d:produces 指定返回的内容类型 仅当request请求头中的(Accept)类型中包含该指定类型才返回
e: params 指定request中必须包含某些参数值
f:headers 指定request中必须包含指定的header值
g: name 指定映射的名称
@RequestMapping(method = RequestMethod.GET)
@RequestMapping(method = RequestMethod.POST)
@RequestMapping(method = RequestMethod.PUT)
@RequestMapping(method = RequestMethod.DELETE)
当然也可以使用
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping 这与上面的是一样的效果
@EnablCaching @EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了。
@suppresswarnings 抑制警告
@Modifying 如果是增,改,删加上此注解
1:方法的返回值应该是int,表示更新语句所影响的行数。
2:在调用的地方必须加事务,没有事务不能正常执行。@Transactional 事务注解
@Query 自定义查询语句 JPQL
JPA注解
@Entity:
@Table(name=“”):注解在类上表明这是一个实体类。一般用于jpa这两个注解一般一块使用,但是如果表名和实体类名相同的话,@Table可以省略
@Column:通过@Column注解设置,包含的设置如下
name:数据库表字段名
unique:是否唯一
nullable:是否可以为空
Length:长度
inserttable:是否可以插入
updateable:是否可以更新
columnDefinition: 定义建表时创建此列的DDL
secondaryTable: 从表名。如果此列不建在主表上(默认建在主表),该属性定义该列所在从表的名字。
@Column(name = "user_code", nullable = false, length=32)//设置属性userCode对应的字段为user_code,长度为32,非空
private String userCode;
@Column(name = "user_wages", nullable = true, precision=12,scale=2)//设置属性wages对应的字段为user_wages,12位数字可保留两位小数,可以为空
private double wages;
@Id:表示该属性为主键。
@Temporal(TemporalType.DATE)//设置为时间类型
private Date joinDate;
@Transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性。如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Basic。@Basic(fetch=FetchType.LAZY):标记可以指定实体属性的加载方式
@JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。
@JoinColumn(name=”loginId”):一对一:本表中指向另一个表的外键。一对多:另一个表指向本表的外键。
@OneToOne、@OneToMany、@ManyToOne:对应hibernate配置文件中的一对一,一对多,多对一。
@GeneratedValue 用于标注主键的生成策略,通过 strategy 属性指定。默认情况下,JPA 自动选择一个最适合底层数据库的主键生成策略:SqlServer 对应 identity,MySQL 对应 auto increment。 在 javax.persistence.GenerationType 中定义了以下几种可供选择的策略:
IDENTITY:采用数据库 ID自增长的方式来自增主键字段,Oracle 不支持这种方式;
AUTO: JPA自动选择合适的策略,是默认选项;
SEQUENCE:通过序列产生主键,通过 @SequenceGenerator 注解指定序列名,MySql 不支持这种方式
TABLE:通过表产生主键,框架借由表模拟序列产生主键,使用该策略可以使应用更易于数据库移植。
原文:https://blog.csdn.net/yitian_66/article/details/80866571
springboot注解大全的更多相关文章
- springBoot系列-->springBoot注解大全
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- SpringBoot注解大全(转)
原文链接:[springBoot系列]--springBoot注解大全 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Co ...
- SpringBoot注解大全 转
2019年3月17日22:30:10 一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@Enab ...
- SpringBoot注解大全*(转发:http://www.cnblogs.com/ldy-blogs/p/8550406.html)
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- SpringBoot第三节(thymeleaf的配置与SpringBoot注解大全)
Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎.所以这里介绍一下Springboot使用Thymeleaf的实例以及遇到的问题. 1.配置与使用 1.1:在applica ...
- SpringBoot:springBoot注解大全
springboot源码下载 https://github.com/spring-projects/spring-boot/releases 一.注解(annotations)列表 @SpringBo ...
- [springBoot系列]--springBoot注解大全[转]
一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...
- SpringBoot进阶教程(六十四)注解大全
在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签.Java5推出新特性annotation,为spring的更新奠定了基础.从Spring 2.X开始spri ...
随机推荐
- latex之行内公式与行间公式
1.行内公式 我是对行内公式的测试$f(x)=1+x+x^2$ 2.行间公式 单行不编号 \begin{equation} \int_0^1(1+x)dx \end{equation} 结果为: 单行 ...
- SQL 优化经历
一次非常有趣的 SQL 优化经历 阅读本文大概需要 6 分钟. 前言 在网上刷到一篇数据库优化的文章,自己也来研究一波. 场景 数据库版本:5.7.25 ,运行在虚拟机中. 课程表 #课程表 cr ...
- 【SpringBoot】常用Starter介绍和整合模板引擎Freemaker、thymeleaf
========7.SpringBoot常用Starter介绍和整合模板引擎Freemaker.thymeleaf ========================= 1.SpringBoot Sta ...
- Synchronized 有几种用法?
我们都知道 Synchronized 是线程安全同步用的,大部分程序可能只会用到同步方法上面.其实 Synchronized 可以用到更多的场合. 1.同步普通方法(锁实例对象) 这个也是我们用得最多 ...
- Django学习笔记之数据库-数据库与模型
MySQL数据库 在网站开发中,数据库是网站的重要组成部分.只有提供数据库,数据才能够动态的展示,而不是在网页中显示一个静态的页面.数据库有很多,比如有SQL Server.Oracle.Postgr ...
- sequelize的mssql配置
配置文件 development: { username: process.env.LOCAL_USERNAME, password: process.env.LOCAL_PASSWORD, data ...
- SpringBoot Web开发(5) 开发页面国际化+登录拦截
SpringBoot Web开发(5) 开发页面国际化+登录拦截 一.页面国际化 页面国际化目的:根据浏览器语言设置的信息对页面信息进行切换,或者用户点击链接自行对页面语言信息进行切换. **效果演示 ...
- Linux(centos 7)配置tomcat8、JDK1.8、lighttpd、ngnix、mysql
JDK 下载好后使用 rpm -ivh jdk-7u25-linux-x64.rpm 进行安装. 安装好后编辑 /etc/profile 文件,在末尾加上: 1 2 3 export JAVA_HOM ...
- Java异常学习总结二
异常的处理方式 方式一:捕获异常(try-catch-finally) 捕获异常是通过三个关键词来实现的:try-catch-finally.用try来执行一段程序,如果出现异常,系统抛出一个异常,可 ...
- 安卓打开远程调试(免root)
首先用数据线连接adb,在pc端执行: adb tcpip 5555 然后就能拔掉数据线了. pc执行这个: adb connect 172.19.208.2 就能连接上