Spring Boot通常有一个名为*Applicationd的入口类,入口类里有一个main方法,这个main方法其实就是一个标准的Java应用的入口方法。在main方法中使用SpringApplication.run(class,args),启动Spring Boot应用项目。上一篇,我们建立了一个Spring Boot项目,启动Spring Boot的项目的代码如下:
package com.jack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Springboot1Application {
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
}
}
@SpringBootApplication是Spring Boot的核心注解,它是一个组合注解,源码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "exclude"
)
Class>[] exclude() default {};
@AliasFor(
annotation = EnableAutoConfiguration.class,
attribute = "excludeName"
)
String[] excludeName() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {};
@AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class>[] scanBasePackageClasses() default {};
}
@SpringBootApplication注解主要组合了@EnableAutoConfiguration,@SpringBootConfiguration,@ComponentScan,如不使用@SpringBootApplication注解,则可以在入口类之间使用@EnableAutoConfiguration,@SpringBootConfiguration,@ComponentScan。
其中@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。
例如,添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,那么Spring Boot会对Tomcat和Spring MVC进行自动配置。
如果添加了spring-boot-starter-data-jpa依赖,Spring Boot会自动进行JPA相关的配置。
Spring Boot会自动扫描@SpringBootApplication所在类的同级包以及下级包里的Bean(若为JPA项目还可以扫描标注@Entity的实体类)。建议入口类放置的位置在groupId+arctifactID组合的包名下。
关闭特定的自动配置,通过上面的源码我们可以看出,关闭特定的自动配置应该使用@SpringBootApplication注解的exclude参数,例如:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
1,修改Banner
1)在Spring Boot启动的时候会有一个默认的启动图案,如下:
2)在src/main/resources下新建一个banner.txt
3)通过http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20网站生成字符串,如敲如jack,将网站生成的字符串复制到banner.txt中
4)这时再启动程序,图案将变成如下图所示:
2,关闭banner
1)main里的内容修改为:
package com.jack;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Springboot1Application {
public static void main(String[] args) {
//SpringApplication.run(Springboot1Application.class, args);
//关闭banner
SpringApplication app = new SpringApplication(Springboot1Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
}
2)或者使用fluent API修改为
package com.jack;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Springboot1Application {
public static void main(String[] args) {
//SpringApplication.run(Springboot1Application.class, args);
//关闭banner
/*SpringApplication app = new SpringApplication(Springboot1Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);*/
new SpringApplicationBuilder(Springboot1Application.class).bannerMode(Banner.Mode.OFF).run(args);
}
}
Spring Boot使用一个全局的配置文件application.properties或application.yml,放置在src/main/resources目录或者类路径的/config下,建议放在src/main/resources目录下。
1,简单示例
将tomcat的默认端口8080修改为8090,默认路径"/"修改为springboot1,可以在application.properties中添加:
server.port=8090
server.context-path=/springboot
server:
port: 8090
context-path: /springboot
在Spring Boot中context-path,contextPath或者CONTEXT_PATH新式其实是通用的。并且yaml的配置更简洁清晰。
@ImportResource({"classpath:some-context.xml","classpath:another-context.xml"})