Spring学习之路-注解
Spring的注解总结。
地址:https://docs.spring.io/spring/docs/4.3.12.RELEASE/spring-framework-reference/htmlsingle/
1、标记注解
@Configuration
定义:标记该类作为一个特殊配置类,可以理解为xml文件中的<beans></beans>
Tip:内部@bean注解方法返回的实例为完全体
原文:Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions.
@ComponentScan
定义:与@Configuration配合使用,默认将此类的包作为根,自动扫描目录下所有注解,可以理解为<context:component-scan/>
Tip:<context:component-scan>隐式的声明了<context:annotation-config>,所以只需声明一个
原文:To autodetect these classes and register the corresponding beans, you need to add @ComponentScan to your @Configuration class
@EnableAutoConfiguration
定义:让SpringBoot根据引入的jar包,决定用对应方式来进行初始化操作
原文:This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added.
@Profile
定义:描述性注解,仅当条件满足时激活类
原文:The @Profile annotation allows you to indicate that a component is eligible for registration when one or more specified profiles are active.
2、类注解
@Component
定义:注明该类会被Spring管理,属于一般性注解
原文:@Component is a generic stereotype for any Spring-managed component.
@Service、@Controller、@Repository
定义:与@Component作用相同,区别在于语义化
原文:@Repository, @Service, and @Controller are specializations of @Component for more specific use cases
@Named、@ManagedBean
定义:分别来自于JS-R330、JSR-250,作用与Component类似,可以自定义名字
Tip:JSR-330注解不支持组合注解
@scope
定义:辅助注解,指定类的作用域
Tip:JSR-330的类注解默认scope为'prototype',为了保持一致,Spring将其默认设置为'singleton'
@Qualifier
定义:辅助注解,指定类的id(name)
@ImportResource
定义:引入外部配置文件,配合@Value可以进行配置初始化
3、实例方法注解
@Bean
定义:该注解作用于一个方法,方法必须实例化一个类并交给IOC容器管理,功能类似于xml配置文件中的<bean/>
Tip:若方法所在的类未被注解为@configuration,则返回lite状态的类,此时不允许实例化中引用其他类
原文:The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.
@Description
定义:对@Bean的描述
原文:it is helpful to provide a more detailed textual description of a bean.
@value
定义:用于对类中实例变量进行初始化赋值
4、基于依赖的方法注解
@Resource
定义:来源于JSR-250,默认通过name注入,失败退化为type
@Inject
定义:来源于JSR-330,默认通过type注入,失败退化为name
Tip:可用Java8的optional,等价于required
@Autowired
定义:Spring自定义注解,功能类似于@inject
Tip:可用required表示该依赖是否必须赋值
5、组合注解
@SpringBootApplication
定义:该注解等价于@Configuration、@EnableAutoConfiguration、@ComponentScan三个注解的组合
原文:The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.
@RestController
定义:该注解等价于@ResponseBody、@Controller的组合
Spring学习之路-注解的更多相关文章
- Spring学习之事务注解@Transactional
今天学习spring中的事务注解,在学习Spring注解事务之前需要明白一些事务的基本概念: 事务:并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通 ...
- Spring学习之路-SpringBoot简单入门
简单讲SpringBoot是对spring和springMVC的二次封装和整合,新添加了一些注解和功能,不算是一个新框架. 学习来源是官方文档,虽然很详细,但是太墨迹了… 地址:https://doc ...
- Spring学习之常用注解(转)
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...
- spring学习笔记二 注解及AOP
本节需要导入spring-aop包 注解 使用注解的目的是为了代替配置,在使用注解时,省略键时,则是为value赋值. 扫描某个包下的所有类中的注解 <?xml version="1. ...
- Spring学习笔记5——注解方式AOP
第一步:注解配置业务类 使用@Component("Pservice")注解ProductService 类 package com.spring.service; import ...
- spring学习 十四 注解AOP 通知传递参数
我们在对切点进行增强时,不建议对切点进行任何修改,因此不加以使用@PointCut注解打在切点上,尽量只在Advice上打注解(Before,After等),如果要在通知中接受切点的参数,可以使用Jo ...
- Spring学习之路-从放弃到入门
AOP:方法拦截器 IOC:类管理容器 主要讲讲这一天看Spring视频学到的东西,以下的叫法全是自创的. 1.类实例管理容器 关于Spring,首先是对类的管理,在常规情况,生成一个类需要调用new ...
- Spring学习笔记--使用注解装配
使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...
- Spring学习之-各注解的含义总结
注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...
随机推荐
- Swift可向上滑移出界面的欢迎页简单封装
使用: -(WelcomView*)welcomeView{ if (!_welcomeView) { _welcomeView = [[NSBundle mainBundle] loadNibNam ...
- unigui web app之菜单
unigui web app之菜单 拖放TUnimMenu控件到窗体上. side:=msleft 表示将在左边显示菜单. 菜单项属性 UnimMenu1.Visible := True;显示菜单 U ...
- 迁移桌面程序到MS Store(4)——桌面程序调用Win10 API
上一篇我们讨论了如何在转制的桌面程序中,通过StartupTask来实现转制版本的开机自启动.实际操作中,我们通过编辑Packaging工程中的Package.appxmanifest文件,来添加自启 ...
- 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid
[源码下载] 背水一战 Windows 10 (53) - 控件(集合类): ItemsControl 的布局控件 - ItemsStackPanel, ItemsWrapGrid 作者:webabc ...
- docker容器备份、恢复和迁移volume方案
volume作为数据的载体,在很多情况下需要对其中的数据进行备份.迁移或是恢复.下面一docker容器的volume为例,说一下备份的技巧. 我们先建立一个容器vol_simple,该容器在/date ...
- SO_REUSEADDR SO_REUSEPORT
http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they ...
- linux apache+php+mysql安装及乱码解决办法
1.乱码解决方法 首先确认mysql数据库字符集设置正确,php页面字符设置正确,之后修改apache配制文件http.conf 注释掉以下字符 AddDefaultCharset UTF-8 此为乱 ...
- tomcat 启动 证书异常java.io.IOException: Alias name [cas] does not identify a key entry
在搭建CAS server的过程中,Tomcat开启https,配置秘钥证书,证书是通过keytool生成的 <Connector port=" protocol="org. ...
- 利用koa打造restful API
概述 最近学习利用koa搭建API接口,小有所得,现在记录下来,供以后开发时参考,相信对其他人也有用. 就目前我所知道的而言,API有2种,一种是jsonp这种API,前端通过ajax来进行跨域请求获 ...
- Vue + Bootstrap 制作炫酷个人简历(一)
最近看了别人做的简历,简单炫酷,自己非常喜欢,于是打算自己做一个,尝试一下. 由于写这篇随笔的时候才开始动工,所以目前没有成品给大家看. emmm等我更新完会在最后附上成品. 现在 开始! 首先 配置 ...