初识SpringBoot

基本配置:

1.入口类和@SpringBootApplication

    springboot 通常有一个名为*Application 的入口类,入口类里有一个 main 方法,这个 main 方法其实就是一个标准 Java 应用的入口方法。在 main 方法中使用    SpringApplication.run(Application.classargs); ,启动 springboot 应用程序。

    @SpringBootApplication 是 springboot 的核心注解,它是一个组合注解,源码如下:

  1. * Copyright 2012- 2017 the original author or authors.
  2. package org.springframework.boot.autoconfigure;
  3. import java.lang.annotation.Documented;
  4. import java.lang.annotation.ElementType;
  5. import java.lang.annotation.Inherited;
  6. import java.lang.annotation.Retention;
  7. import java.lang.annotation.RetentionPolicy;
  8. import java.lang.annotation.Target;
  9. import org.springframework.boot.SpringBootConfiguration;
  10. import org.springframework.boot.context.TypeExcludeFilter;
  11. import org.springframework.context.annotation.Bean;
  12. import org.springframework.context.annotation.ComponentScan;
  13. import org.springframework.context.annotation.ComponentScan.Filter;
  14. import org.springframework.context.annotation.Configuration;
  15. import org.springframework.context.annotation.FilterType;
  16. import org.springframework.core.annotation.AliasFor;
  17. /**
  18. * Indicates a {@link Configuration configuration} class that declares one or more
  19. * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
  20. * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
  21. * annotation that is equivalent to declaring {@code @Configuration},
  22. * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
  23. * * @since 1.2.0
  24. */
  25. @Target(ElementType.TYPE)
  26. @Retention(RetentionPolicy.RUNTIME)
  27. @Documented
  28. @Inherited
  29. @SpringBootConfiguration
  30. @EnableAutoConfiguration
  31. @ComponentScan(excludeFilters = {
  32. @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  33. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  34. public @interface SpringBootApplication {
  35. /**
  36. * Exclude specific auto-configuration classes such that they will never be applied.
  37. * @return the classes to exclude
  38. */
  39. @AliasFor(annotation = EnableAutoConfiguration.class)
  40. Class<?>[] exclude() default {};
  41. /**
  42. * Exclude specific auto-configuration class names such that they will never be
  43. * applied.
  44. * @return the class names to exclude
  45. * @since 1.3.0
  46. */
  47. @AliasFor(annotation = EnableAutoConfiguration.class)
  48. String[] excludeName() default {};
  49. /**
  50. * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
  51. * for a type-safe alternative to String-based package names.
  52. * @return base packages to scan
  53. * @since 1.3.0
  54. */
  55. @AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
  56. String[] scanBasePackages() default {};
  57. /**
  58. * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
  59. * scan for annotated components. The package of each class specified will be scanned.
  60. * <p>
  61. * Consider creating a special no-op marker class or interface in each package that
  62. * serves no purpose other than being referenced by this attribute.
  63. * @return base packages to scan
  64. * @since 1.3.0
  65. */
  66. @AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
  67. Class<?>[] scanBasePackageClasses() default {};
  68. }

    @SpringBootApplication 注解主要组合了 @Configuration 、@EnableAutoConfiguration、@ComponentScan;若不使用 @SpringBootApplication 注解则可以在入口类上面直接使用。

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

    例如,添加了 spring-boot-starter-web 依赖,会自动添加 Tomcat 和 springmvc 的依赖,那么 springboot 会对 Tomcat 和 springmvc 进行自动配置。

    又如,添加了 spring-boot-starter-data-jpa 依赖,springboot 会自动进行 jpa 相关的配置。

    springboot 会自动扫描 @SpringBootApplication  所在类的同级包以及包里的 bean (若为 JPA 项目还可以扫描 @Entity 的实体类)。建议入口类放置的位置在 groupid+arctifactID  组合包下。

2.关闭特定的自动配置

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

@SpringBootApplication(exclude = (xx.class))

3.定制 banner

1).修改 banner

    ①:在 springboot 启动的时候会有一个默认的启动图案,见上篇文章截图。

    ②:我们在 src/main/resources 下新建一个 banner.txt

    ③:通过 http://patorjk.com/software/taag 网站生成字符,复制到 banner.txt 



4.springboot 的配置文件

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


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

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

    ①:简单示例:

            将 Tomcat 的默认端口 8080 改为 9090,并将默认的访问路径 / 修改为 /helloboot。

            可以在 application.properties 中添加:

            

        application.yml:

            

        从上面的配置可以看出,在 springboot 中,context-path、contextPath 或者 CONTEXT_PATH 形式其实是通用的。并且,yaml  的配置更加简洁清晰。

5. starter pom

    spring boot 为我们提供了简化企业级开发绝大多数场景的 starter pom, 只要使用了应用场景需要的 starter pom,相关的技术配置将会消除,就可以得到 springboot 为我们提供的自动配置的 bean。有官方和第三方提供的 starter pom.

6.使用 xml 配置

    springboot 提倡零配置,即无 xml 配置,但是实际项目中,可能有一些特殊要求你必须使用 xml 配置,这时我们可以通过 spring 提供的 @ImportResource 来加载 xml 配置,例如:

@ImportResource({"a.xml","b.xml"})

外部配置

    springboot 允许使用 properties 文件、yaml 文件或者命令行参数作为外部配置。

1. 命令行参数配置

    springboot 可以是基于 jar 包运行的,打成 jar 包的程序可以直接通过下面命令运行:

java -jar xx.jar

    可以通过一下命令修改 Tomcat 端口号

java -jar xx.jar --server.port=9090

2.常规属性配置

    在 spring 环境下面,注入 properties 配置文件,只需要通过注解 @PropertySource 指明 properties 文件的位置,然后通过 @Value 注入值。在 springboot 里,只需在 application.properties 里定义属性,然后直接使用 @Value 即可。




3.类型安全的配置(基于 properties)

    上例中使用@Value 注入每个配置在实际项目中会显得格外麻烦,因为我们的配置通常会有许多个,若使用上例中的方式则要使用@Value 注入很多次。

    springboot 还提供了基于类型安全的配置方式,通过 @ConfigurationProperties 将 properties 属性和一个 bean 及其属性关联,从而实现类型的安全的配置。




之前,按照作者的书一路看过来,碰到了第一个错,springboot 会扫描所有的(或者同包下,猜测) Application 文件,另外一个 @RequestMapping 也使用了 / ,本以为我启动 SettingApplication  ,Application 不会有影响!小记。

日志配置

    springboot 支持 Java Util Logging、Log4J、Log4J2 和 Logback 作为日志框架,无论使用哪种日志框架,springboot 已为当前使用日志框架的控制台输出以及文件输出做好了配置。

Profile 配置

    Profile  是 spring 用来针对不同的环境对不同的配置提供支持,全局 Profile 配置使用 application-{profile}.properties ( 如 application-prod.properties)。

通过 application.properties 中设置 spring.profiles.active= prod 来指定活动的 Profile。

下面我们做一个示例,我们分为生产环境和开发环境,生产环境端口号为 80, 开发环境为 8888。




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值