码迷,mamicode.com
首页 > 编程语言 > 详细

Spring框架注解

时间:2017-11-22 17:39:45      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:ant   clu   any   context   英文   code   instant   new   name   

  学习Spring的过程发现注解实在是太多太多了,有些来自于JSR,有些是Spring,有些只是语义上不同,有些是组合注解。

  这里做一个总结,懵逼的时候回来看。

 

 

1、标记注解

@Configuration

定义:标记该类作为一个特殊配置类,可以理解为xml文件中的<beans></beans>。与@Bean配合使用,可以让同一个类的内部,实例化方法实现互相依赖。

英文原版:@Configuration classes allow inter-bean dependencies to be defined by simply calling other @Bean methods in the same class.

@ComponentScan

定义:与@Configuration配合使用,默认将此类的包作为根,自动扫描目录下所有注解,可以理解为<context:component-scan/>

Tip:The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config>. There is usually no need to include the<context:annotation-config> element when using <context:component-scan>.

英文原版: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. 

 

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

 

3、方法注解

@Bean

定义:该注解作用于一个方法,方法必须实例化一个类并交给IOC容器管理,功能类似于xml配置文件中的<bean/>

英文原版: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. 

@value

定义:用于对类中实例变量进行初始化赋值

 

4、依赖注解

@Resource

定义:来源于JSR250,默认通过name注入,失败退化为type

@Inject

定义:来源于JSR330,默认通过type注入,失败退化为name

@Autowired

定义:Spring自定义注解,功能类似于@inject

 

5、组合注解

@SpringBootApplication

定义:该注解等价于@Configuration、@EnableAutoConfiguration、@ComponentScan三个注解的组合

英文原版:The @SpringBootApplication annotation is equivalent to using @Configuration@EnableAutoConfiguration and @ComponentScan with their default attributes.

@RestController

定义:该注解等价于@ResponseBody、@Controller的组合

Spring框架注解

标签:ant   clu   any   context   英文   code   instant   new   name   

原文地址:http://www.cnblogs.com/QH-Jimmy/p/7879716.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!