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的组合
最新文章
- NoSql basic knowledge
- 更改ubuntu多系统启动顺序
- gcc编译参数-fPIC的一些问题
- wordpress 添加自定义菜单到管理面板(wp-admin)
- 各种drawable
- 【图形学】我理解的伽马校正(Gamma Correction)
- Open vSwitch简述
- Sencha Touch2 时间轴ListPanel
- Codeforces 626A Robot Sequence
- Android学习系列(20)--App数据格式之解析Json
- C++中的头文件和源文件
- 项目FAQ
- 学号:201521123116 《java程序设计》第三周学习总结
- [Luogu 2642] 双子序列最大和
- Vlan ---虚拟局域网
- 关于Input内容改变的触发事件
- django xadmin查找当前用户所在组
- JSP报错:The superclass ";javax.servlet.http.HttpServlet"; was not found on the Java Build Path
- IDEA多线程调试设置
- dojo小代码
热门文章
- Oracle EBS主界面的Top Ten List
- FastReport的使用方法
- delphi 过滤开头 结尾 全部 空格的函数
- PCA in MLLib
- 设计模式:visitor
- Python2.7更新pip:UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xb7 in position 7: ordinal not in range(128)
- FunDA(17)- 示范:异常处理与事后处理 - Exceptions handling and Finalizers
- GoLang学习控制语句之if/else
- 【dpdk】使用libpcap-PMD驱动收发包
- Openstack单元测试工具简单说明