springboot笔记01

 每个SpringBoot程序都有一个主入口,也就是main方法,main里面调用SpringApplication.run()启动整个spring-boot程序,该方法所在类需要使用@SpringBootApplication注解,以及@ImportResource注解(if need),@SpringBootApplication包括三个注解,功能如下:@EnableAutoConfiguration:SpringBoot根据应用所声明的依赖来对Spring框架进行自动配置

@SpringBootConfiguration(内部为@Configuration):被标注的类等于在spring的XML配置文件中(applicationContext.xml),装配所有bean事务,提供了一个spring的上下文环境

@ComponentScan:组件扫描,可自动发现和装配Bean,默认扫描SpringApplication的run方法里的Booter.class所在的包路径下文件,所以最好将该启动类放到根包路径下

 

2.实际上@Configuration可以理解为xml中的beans标签,而@Bean可以理解为bean标签

例如:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-lazy-init="true">
<!--bean定义-->
</beans>
/////这两种方式的配置是一种效果
@Configuration
public class MockConfiguration{
//bean定义 

}

3 @ComponentScan

组件扫描,可自动发现和装配Bean,默认扫描SpringApplication的run方法里的Booter.class所在的包路径下文件,所以最好将该启动类放到根包路径下

这个注解在Spring中很重要,他赌赢了XML配置的元素。@ComponentScan的功能其实就是自动扫描并加载到符合条件的组件中(比如@Component和@Repository)或者Bean的定义,最终将这些bean加载IOC容器中。我们可以通过BasePackge等属性来细粒度定制@ComponentScan自动扫描的范围,如果不指定,则任务spring框架会从声明出开始扫描。

 

posted @ 2019-06-05 20:02  wjj1013  阅读(117)  评论(0编辑  收藏  举报