美文网首页springboot
SpringBoot注解详解

SpringBoot注解详解

作者: 上进的小二狗 | 来源:发表于2018-08-10 14:30 被阅读72次
一、常用注解

1、@RestController = @ResponseBody + @Controller
表示修饰该Controller所有的方法返回JSON格式数据而不是视图,直接可以编写
Restful接口
2、@ComponentScan等价于 <context:component-scan basepackage=""/>表示程序启动是,自动扫描当前包及子包下所有类
3、@Configuration等价于applicationContext.xml 表示将该类作用springboot配置文件类
4、@Import 等价于applicationContext.xml中的<import>标签
5、@Bean 等价于applicationContext.xml中的<bean id/name>标签
6、@Scope 等价于applicationContext.xml中的<bean>标签中的scope属性

二、启动类相关配置

@SpringBootAppliaction=@Configuration+@ComponentScan+@EnableAutoConfiguration
@SpringBootAppliaction在类上加入,实际上就是注解的整合,能够作为启动类,包扫描(只限于当前同级包下)

三、加载类相关配置

1、@EnableAutoConfiguration 表示程序启动时,自动加载springboot默认的配置
2、@EnableConfigurationProperties(ThymeleafProperties.class) 加载properties 对应的类
3、@ConditionalOnClass(SpringTemplateEngine.class) 这是代表了条件的注解,该主键中传递的参数是用来判断的
4、@AutoConfigureAfter(WebMvcAutoConfiguration.class) 在参数中声明的类加载完了之后,才会去自动配置

四、测试相关配置

@RunWith(SpringRunner.class) SpringRunner 继承了SpringJunit4Runner
@SpringBootTest 这里可以换成@ContextConfiguration

相关文章

网友评论

    本文标题:SpringBoot注解详解

    本文链接:https://www.haomeiwen.com/subject/bztcbftx.html