标签:
近期看github上Java项目接触到多种注解,这里对其使用场景做简单的整理
问题:
1. 为什么要用注解?不用注解是否可以实现?
2. 注解的组成?
注解类似一个接口
注解可以定义可指定的属性
3. 如何自定义注解?
4. java spring框架中有哪些已有的注解?
| 注解 | 使用场景 | 备注 |
|---|---|---|
| @Configuration | 类似于xml中<beans>标签 | |
| @Bean | 类似于xml中<bean>标签 | 常与@Configuration配合使用 |
| @ComponentScan | 可指定扫描类的范围 @ComponentScan(basePackages="org.jc" ) |
|
| @Component | ||
| @Resource | 属于javax.annotation.Resource,默认按name注入,可指定按name或type注入。如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常。 | |
| @Autowired | 是spring提供的注解,告知spring容器某个属性需要自动注入,按type注入 可以结合@Qualifier按name注入 | |
| @Injected | 与Autowired类似 | |
| @Import | ||
| @Profile | 指定运行环境,如 @Profile("local") | |
| @Value | 注入静态变量,如 @Value("${my.name}") |
|
| @Repository | 数据访问组件,一个容器类,多是数据源配置 | |
| @Service | 自定义服务,默认自动加载到spring容器 | |
| @Controller | 控制层组件 | |
| @RequestMapping | 用于映射请求,指定处理哪个或哪类url的请求;可作用于类 和 方法,如@RequestMapping(value = "/mylink", method = RequestMethod.GET) |
|
| @RequestBody | ||
| @RequestParam | ||
| @ResponseBody | 可以根据请求方的返回格式的要求,返回json或xml格式,不仅仅返回string | |
| @EnableScheduling | ||
| @Scheduled | 定时任务 | |
| @Transaction | 事务 | |
| @Thrift | ||
| @SpringBootAplication | ||
| @Test | 单元测试,用于修复方法 | |
| @SpringBootTest | 是多个注解的组合 = @Configuration,@EnableAutoConfiguration,@ComponentScan | |
| @RunWith | 用于单元测试,如@RunWith(SpringJUnit4ClassRunner.class) | |
| @ApplicationPath | ||
| @ContextConfiguration | 如@ContextConfiguration(locations = { "org.springframework.amqp.rabbit.log4j.config.server" }, loader = AnnotationConfigContextLoader.class) | |
| @RabbitListner | ||
| @RabbitHandler | ||
标签:
原文地址:http://www.cnblogs.com/douzhou/p/5890781.html