spring学习(二)——注解

近期看github上Java项目接触到多种注解,这里对其使用场景作简单的整理java

问题:git

1. 为何要用注解?不用注解是否能够实现?github

2. 注解的组成?spring

注解相似一个接口json

注解能够定义可指定的属性app

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
相关文章
相关标签/搜索