Spring boot入门四 注解说明
2017-08-30 17:07
375 查看
常用注解(annotations)列表 :@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。@Configuration 相当于传统的xml配置文件,如果有些第三方库需要用到xml文件,建议仍然通过@Configuration类作为项目的配置主类——可以使用@ImportResource注解加载xml配置文件
@EnableAutoConfiguration Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。
@ComponentScan 表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类
@Component 可配合CommandLineRunner使用,在程序启动后执行一些基础任务。@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。@Autowired 自动导入。@PathVariable 获取参数。@JsonBackReference 解决嵌套外链问题。@RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用。
@Import:用来导入其他配置类。@ImportResource:用来加载xml配置文件。@Autowired:自动导入依赖的bean@Service:一般用于修饰service层的组件@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。@Bean:用@Bean标注方法等价于XML中配置的bean。@Value:注入Spring boot application.properties配置的属性的值。示例代码:
@EnableAutoConfiguration Spring Boot自动配置(auto-configuration):尝试根据你添加的jar依赖自动配置你的Spring应用。例如,如果你的classpath下存在HSQLDB,并且你没有手动配置任何数据库连接beans,那么我们将自动配置一个内存型(in-memory)数据库”。你可以将@EnableAutoConfiguration或者@SpringBootApplication注解添加到一个@Configuration类上来选择自动配置。如果发现应用了你不想要的特定自动配置类,你可以使用@EnableAutoConfiguration注解的排除属性来禁用它们。
@ComponentScan 表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类
@Component 可配合CommandLineRunner使用,在程序启动后执行一些基础任务。@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。@Autowired 自动导入。@PathVariable 获取参数。@JsonBackReference 解决嵌套外链问题。@RepositoryRestResourcepublic 配合spring-boot-starter-data-rest使用。
@Import:用来导入其他配置类。@ImportResource:用来加载xml配置文件。@Autowired:自动导入依赖的bean@Service:一般用于修饰service层的组件@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。@Bean:用@Bean标注方法等价于XML中配置的bean。@Value:注入Spring boot application.properties配置的属性的值。示例代码:
相关文章推荐
- SpringBoot入门-20(springboot集成mybatis注解形式properties配置,利用@Provider实现动态SQL)
- spring boot 自定义注解入门
- springboot 入门教程(3)-运行原理、关键注解和配置
- Spring Boot快速入门(五):使用MyBatis(注解形式)进行数据库操作
- spring boot JPA 使用(二)常见注解说明
- SpringBoot入门-12-1(springboot配置servlet,利用注解实现)
- Springboot Enable开头注解解释说明
- SpringBoot入门-17(springboot集成mybatis注解形式增删查改properties配置)
- 微服务开发的入门级框架Spring Boot详解:注解
- SpringBoot之一些注解说明汇总
- 零基础入门学习——Spring Boot注解
- springboot入门---入口类注解@SpringBootApplication解析
- SpringBoot入门-21(springboot集成mybatis注解形式增删查改properties配置,利用@Provider实现动态SQL)
- spring boot之Spring Mvc开发和常用注解说明
- Spring入门——基于Java的容器注解说明之@bean
- Spring Boot基础入门之基于注解的Mybatis
- SpringBoot入门-17(springboot集成mybatis注解形式实现ID自动增长)
- SpringBoot入门-16(springboot集成mybatis注解形式)
- SpringBoot注解说明
- Spring boot入门三 应用子项列表说明