您的位置:首页 > 编程语言 > Java开发

Spring Boot核心-基本配置

2018-04-10 15:25 519 查看
[源]:https://blog.csdn.net/xiaolyuh123/article/details/70698659


1 入口类和@SpringBootApplication

Spring Boot通常有一个名为*Application的入口类,入口类里有一个main方,这main方法就是一个J标准的ava应用入口方法。在main方法中使用SpringApplication.run(SpringBootStudentApplication.class, args),启动Spring Boot应用。

@SpringBootApplication是Spring Boot的核心注解,他是一个组合注解,源代码如下:

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.context.TypeExcludeFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
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 {

/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};

/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "excludeName")
String[] excludeName() default {};

/**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};

/**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};

}


@SpringBootApplication注解主要组合了@Configuration(@SpringBootConfiguration)、@EnableAutoConfiguration、@ComponentScan注解;如果不使用@SpringBootApplication注解可以直接在类上使用@Configuration、@EnableAutoConfiguration、@ComponentScan注解。

其中,@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。

列如,添加了spring-boot-starter-web依赖,会自动添加Tomcate和Spring MVC依赖,那么Spring Boot会对Tomcate和Spring Boot进行自动配置。

Spring Boot会自动扫描@SpringBootApplication所在类的同级包(列如:com.chenfeng.xiaolyuh)及下级包里的Bean。建议入口类放置在groupId+artifactId组合的包名下。


2 关闭特定的自动配置

通过@SpringBootApplication的源码可以看出,关闭特定的自动配置应该使用@SpringBootApplication注解的exclude参数,列如:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})


3 定制Banner


3.1 修改Banner

在Spring启动的时候会有一个默认的图案,如图:


我们在src/main/resources下新建一个banner.txt文件。

通过http://patorjk.com/software/taag网站生成字符串,如敲击“WISELY”将生成的内容复制到banner.txt文件中。

再启动程序图案为:


参考网站


3.2 关闭banner

main函数里的内容修改为:
public static void main(String[] args) {
// SpringApplication.run(SpringBootStudentApplication.class, args);
SpringApplication app = new SpringApplication(SpringBootStudentApplication.class);
/*
* Mode.OFF:关闭;
* Mode.CONSOLE:控制台输出,默认方式;
* Mode.LOG:日志输出方式;
*/
app.setBannerMode(Mode.OFF);
app.run(args);
}



4 Spring
Boot 的配置文件

Spring Boot使用一个全局配置文件application.properties或application.yml放置在src/main/resources目录或类路径的/config下。

Spring Boot不仅支持常用的properties配置文件,还支持yaml语言的配置文件。yaml是以数据为中心的语言,在配置数据的时候具有面向对象的特征。

Spring Boot全局配置文件的作用是对一些默认配置进行修改。

简单示例 将Tomcate默认端口8080改成9090,并将默认的访问路径“/”改成“helloboot”。

可以在application.properties中添加:

server.port=9090
server.context-path=/helloboot

或者application.yml中添加:

server:
port: 9090
context-path: /helloboot

application.yml和application.properties都是Spring Boot全局配置,只需要写一个,如果同是存在使用application.properties。


5 starter pom

Spring Boot为我们提供了简化企业级开发绝大多数场景的starter pom,只要使用了应用场景的starter pom,相关的技术配置将会消除,就可以得到Spring Boot为我们提供的自动配置的Bean。

官方的starter pom






第三方的 starter pom



6 使用XML配置

Spring Boot提倡0配置,但实际开发中可能需要XML配置,我们可以通过@ImportResource注解来加载XML配置,如:

@ImportResource({"classpath:some-context.xml", "classpath:another-context.xml"})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: