Spring boot学习笔记(一)
maven环境搭建
首先需要搭建一个父model,所有的版本以父model版本为准
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
如果不继承这个parent,需要使用dependencyManagement,这样不需要再继承,避免了单继承的一些麻烦
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
SpringBoot入口方式
SpringApplication.run(Application.class, args) 返回一个ConfigurableApplicationContext上下文对象,可以通过这个上下文对象来管理bean
在入口上使用 @SpringBootApplication这个注解,将自动扫描所有component,非常方便。
这里是这个注解的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration//当前类是配置类
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes =
TypeExcludeFilter.class))//自动扫描包
public @interface SpringBootApplication {
Class<?>[] exclude() default {};//排除指定的bean
String[] excludeName() default {};//排除指定的bean
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};//从当前包开始扫描
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
除了使用静态方法外,还可以创建一个SpringApplication对象来管理多个源
SpringApplication app = new SpringApplication();
Set<Object> set = new HashSet<>();
set.add(Application1.class);
set.add(Application2.class);
app.setSource(set);
ConfigurableApplicationContext context = app.run(args);
maven环境搭建
首先需要搭建一个父model,所有的版本以父model版本为准
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
如果不继承这个parent,需要使用dependencyManagement,这样不需要再继承,避免了单继承的一些麻烦
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
SpringBoot入口方式
SpringApplication.run(Application.class, args) 返回一个ConfigurableApplicationContext上下文对象,可以通过这个上下文对象来管理bean
在入口上使用 @SpringBootApplication这个注解,将自动扫描所有component,非常方便。
这里是这个注解的源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration//当前类是配置类
@EnableAutoConfiguration
@ComponentScan(excludeFilters = @Filter(type = FilterType.CUSTOM, classes =
TypeExcludeFilter.class))//自动扫描包
public @interface SpringBootApplication {
Class<?>[] exclude() default {};//排除指定的bean
String[] excludeName() default {};//排除指定的bean
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};//从当前包开始扫描
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
除了使用静态方法外,还可以创建一个SpringApplication对象来管理多个源
SpringApplication app = new SpringApplication();
Set<Object> set = new HashSet<>();
set.add(Application1.class);
set.add(Application2.class);
app.setSource(set);
ConfigurableApplicationContext context = app.run(args);