一、启动注解 @springbootapplication@target(elementtype.type)@retention(retentionpolicy.runtime)@documented@inherited@springbootconfiguration@enableautoconfiguration@componentscan(excludefilters = { @filter(type = filtertype.custom, classes = typeexcludefilter.class), @filter(type = filtertype.custom, classes = autoconfigurationexcludefilter.class) })public @interface springbootapplication { // ... 此处省略源码}查看源码可发现,@springbootapplication是一个复合注解,包含了@springbootconfiguration,@enableautoconfiguration,@componentscan这三个注解
@springbootconfiguration 注解,继承@configuration注解,主要用于加载配置文件@springbootconfiguration继承自@configuration,二者功能也一致,标注当前类是配置类, 并会将当前类内声明的一个或多个以@bean注解标记的方法的实例纳入到spring容器中,并且实例名就是方法名。
@enableautoconfiguration 注解,开启自动配置功能@enableautoconfiguration可以帮助springboot应用将所有符合条件的@configuration配置都加载到当前springboot创建并使用的ioc容器。借助于spring框架原有的一个工具类:springfactoriesloader的支持,@enableautoconfiguration可以智能的自动配置功效才得以大功告成
@componentscan 注解,主要用于组件扫描和自动装配@componentscan的功能其实就是自动扫描并加载符合条件的组件或bean定义,最终将这些bean定义加载到容器中。我们可以通过basepackages等属性指定@componentscan自动扫描的范围,如果不指定,则默认spring框架实现从声明@componentscan所在类的package进行扫描,默认情况下是不指定的,所以springboot的启动类最好放在root package下。
二、controller 相关注解@controller控制器,处理http请求。
@restcontroller 复合注解查看@restcontroller源码
@target(elementtype.type)@retention(retentionpolicy.runtime)@documented@controller@responsebodypublic @interface restcontroller { /** * the value may indicate a suggestion for a logical component name, * to be turned into a spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty string otherwise) * @since 4.0.1 */ @aliasfor(annotation = controller.class) string value() default ;}从源码我们知道,@restcontroller注解相当于@responsebody+@controller合在一起的作用,restcontroller使用的效果是将方法返回的对象直接在浏览器上展示成json格式.
@requestbody通过httpmessageconverter读取request body并反序列化为object(泛指)对象
@requestmapping@requestmapping 是 spring web 应用程序中最常被用到的注解之一。
这个注解会将 http 请求映射到 mvc 和 rest 控制器的处理方法上
@getmapping用于将http get请求映射到特定处理程序的方法注解注解简写:@requestmapping(value = /say,method = requestmethod.get)等价于:@getmapping(value = /say)
getmapping源码
@target(elementtype.method)@retention(retentionpolicy.runtime)@documented@requestmapping(method = requestmethod.get)public @interface getmapping {//...}是@requestmapping(method = requestmethod.get)的缩写
@postmapping用于将http post请求映射到特定处理程序的方法注解@target(elementtype.method)@retention(retentionpolicy.runtime)@documented@requestmapping(method = requestmethod.post)public @interface postmapping { //...}是@requestmapping(method = requestmethod.post)的缩写
三、取请求参数值@pathvariable:获取url中的数据@controller@requestmapping(/user)public class helloworldcontroller { @requestmapping(/getuser/{uid}) public string getuser(@pathvariable(uid)integer id, model model) { system.out.println(id:+id); return user; }}请求示例:http://localhost:8080/user/getuser/123
@requestparam:获取请求参数的值@controller@requestmapping(/user)public class helloworldcontroller { @requestmapping(/getuser) public string getuser(@requestparam(uid)integer id, model model) { system.out.println(id:+id); return user; }}请求示例:http://localhost:8080/user/getuser?uid=123
@requestheader 把request请求header部分的值绑定到方法的参数上
@cookievalue 把request header中关于cookie的值绑定到方法的参数上
四、注入bean相关@repositorydao层注解,dao层中接口继承jparepository,需要在build.gradle中引入相关jpa的一个jar自动加载。
repository注解源码
@target({elementtype.type})@retention(retentionpolicy.runtime)@documented@componentpublic @interface repository { /** * the value may indicate a suggestion for a logical component name, * to be turned into a spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty string otherwise) */ @aliasfor(annotation = component.class) string value() default ;}@service@target({elementtype.type})@retention(retentionpolicy.runtime)@documented@componentpublic @interface service { /** * the value may indicate a suggestion for a logical component name, * to be turned into a spring bean in case of an autodetected component. * @return the suggested component name, if any (or empty string otherwise) */ @aliasfor(annotation = component.class) string value() default ;}@service是@component注解的一个特例,作用在类上@service注解作用域默认为单例使用注解配置和类路径扫描时,被@service注解标注的类会被spring扫描并注册为bean@service用于标注服务层组件,表示定义一个bean@service使用时没有传参数,bean名称默认为当前类的类名,首字母小写@service(“servicebeanid”)或@service(value=”servicebeanid”)使用时传参数,使用value作为bean名字@scope作用域注解@scope作用在类上和方法上,用来配置 spring bean 的作用域,它标识 bean 的作用域
@scope源码
@target({elementtype.type, elementtype.method})@retention(retentionpolicy.runtime)@documentedpublic @interface scope { /** * alias for {@link #scopename}. * @see #scopename */ @aliasfor(scopename) string value() default ; @aliasfor(value) string scopename() default ; scopedproxymode proxymode() default scopedproxymode.default;}属性介绍
value singleton 表示该bean是单例的。(默认) prototype 表示该bean是多例的,即每次使用该bean时都会新建一个对象。 request 在一次http请求中,一个bean对应一个实例。 session 在一个httpsession中,一个bean对应一个实例。proxymode default 不使用代理。(默认) no 不使用代理,等价于default。 interfaces 使用基于接口的代理(jdk dynamic proxy)。 target_class 使用基于类的代理(cglib)。@entity实体类注解@table(name =数据库表名),这个注解也注释在实体类上,对应数据库中相应的表。
@id、@column注解用于标注实体类中的字段,pk字段标注为@id,其余@column。
@bean产生一个bean的方法@bean明确地指示了一种方法,产生一个bean的方法,并且交给spring容器管理。支持别名@bean(xx-name)
@autowired 自动导入@autowired注解作用在构造函数、方法、方法参数、类字段以及注解上@autowired注解可以实现bean的自动注入@component把普通pojo实例化到spring容器中,相当于配置文件中的
虽然有了@autowired,但是我们还是要写一堆bean的配置文件,相当麻烦,而@component就是告诉spring,我是pojo类,把我注册到容器中吧,spring会自动提取相关信息。那么我们就不用写麻烦的xml配置文件了
五、导入配置文件@propertysource注解引入单个properties文件:
@propertysource(value = {classpath : xxxx/xxx.properties})引入多个properties文件:
@propertysource(value = {classpath : xxxx/xxx.properties,classpath : xxxx.properties})@importresource导入xml配置文件可以额外分为两种模式 相对路径classpath,绝对路径(真实路径)file
注意:单文件可以不写value或locations,value和locations都可用
相对路径(classpath)
引入单个xml配置文件:@importsource(classpath : xxx/xxxx.xml)引入多个xml配置文件:@importsource(locations={classpath : xxxx.xml , classpath : yyyy.xml})绝对路径(file)
引入单个xml配置文件:@importsource(locations= {file : d:/hellxz/dubbo.xml})引入多个xml配置文件:@importsource(locations= {file : d:/hellxz/application.xml , file : d:/hellxz/dubbo.xml})
取值:使用@value注解取配置文件中的值@value(${properties中的键})private string xxx;@import 导入额外的配置信息功能类似xml配置的,用来导入配置类,可以导入带有@configuration注解的配置类或实现了importselector/importbeandefinitionregistrar。
使用示例
@springbootapplication@import({smsconfig.class})public class demoapplication { public static void main(string[] args) { springapplication.run(demoapplication.class, args); }}六、事务注解 @transactional在spring中,事务有两种实现方式,分别是编程式事务管理和声明式事务管理两种方式
编程式事务管理:编程式事务管理使用transactiontemplate或者直接使用底层的platformtransactionmanager。对于编程式事务管理,spring推荐使用transactiontemplate。
声明式事务管理:建立在aop之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务,通过@transactional就可以进行事务操作,更快捷而且简单。推荐使用
七、全局异常处理@controlleradvice 统一处理异常@controlleradvice 注解定义全局异常处理类
@controlleradvicepublic class globalexceptionhandler {}@exceptionhandler 注解声明异常处理方法@controlleradvicepublic class globalexceptionhandler { @exceptionhandler(exception.class) @responsebody string handleexception(){ return exception deal!; }}
基美电子扩展ESD等级陶瓷电容器产品组合
USB-IF主席谈Type-C:以USB史上最快速度投放市场
5G技术最热话题有哪些
未来的人工智能是否能够丢弃过时的、影响最终分析的信息?
女朋友生气了吗?算法比直男更懂她
SpringBoot常用注解及原理
电装EPS-MCU技术在机器人中的应用
连接器电镀常识科普
安全代币和STO市场将如何推动数字货币行业的发展
电池管理系统(BMS)原理
买3G手机 让我们把世界杯握在手心
工控机在无人银行中有着怎样的应用
DeepMind如何用神经网络自动构建启发式算法
小米5G蓄势待发,骁龙865+1.08亿像素+环绕屏
创维OLED电视技术突破_推出未来形态的OLED电视
电位器知识
设计通用串行总线协议接口时的六个关键问题
在未来机器人的市场会发生什么样的变化
超乎你的想象!混合现实用于医院进行远程医疗
宝宝不开心肿么办 VR有助于缓解青少年社交焦虑