一、导航

本节内容简介:

1. spring boot 配置文件,使用@SpringBootApplication注解

2. spring boot 修改Java版本 和项目编码

3. 一个标准的spring boot 代码结构

4. 查看当前项目自动配置了那些模块

5. 禁用自动配置

6. 自定义banner及关闭banner

一、spring boot 配置文件,使用@SpringBootApplication注解

spring boot 默认使用application.properties或者application.yml,放置在src/main/resources目录或者类路径的/config下,一般建议就放在src/main/resources

这里我们使用application.properties来配置,这里我们试着修改下端口和访问路径

目录结构如下:



配置代码:

server.port=8081
server.context-path=/boot
  • 1
  • 2

编写测试controller类

package com.likeoak.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 测试Controller
* The type Test controller.
*/
@RestController
public class TestController {
/**
* 返回 String 字符串,访问成功,返回“test ok”
* Test string.
*
* @return the string
*/
@RequestMapping("/test")
public String test(){ return "test ok!";
} }

启动main方法,及运行APP启动类

package com.likeoak;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; /**
* 默认启动类
*/
@SpringBootApplication
public class App
{
public static void main( String[] args )
{ SpringApplication.run(App.class,args);
}
}

访问:http://localhost:8081/boot/test

结果:test ok!

代码解释:

@SpringBootApplication 解释

先看下注解@SpringBootApplication的源码

@SpringBootApplication的源码

@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 {

里面包含@SpringBootConfiguration的源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

总结:@SpringBootApplication注解其实是@Configuration,@EnableAutoConfiguration,@ComponentScan这三个注解组合

注解解释

@Configuration 注解:标明该类使用Spring是基于java的配置

@EnableAutoConfiguration :开启自动配置注解,有这个注解spring boot就会根据我们所引用的jar包来自动配置我们需要的配置,这正是spring boot 魔力。

@ComponentScan:spring扫描注解,有这个注解spring boot 就会扫描(默认是以根路径为准)所有的包,来加载所有的@Bean,所有这里的TestController 就是被扫描到的,我们就可以访问了。

二、spring boot 修改Java版本 和项目编码

在使用spring bootde 过程中,想自定义java配置,可以使用以下配置,加载到pom.xml中即可

  <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>

三、一个标准的spring boot 代码结构

一个典型的spring boot 项目结构,这也是官网推荐的

com
+- example
+- myproject
+- Application.java
|
+- domain
| +- Customer.java
| +- CustomerRepository.java
|
+- service
| +- CustomerService.java
|
+- web
+- CustomerController.java

四、 查看当前项目自动配置了那些模块

查看当前项目有哪些自动配置,一共有三种方法

1. 直接运行jar -jar xxx.jar –debug

2. 在application中设置属性

debug=true
  1. 直接在启动的时候,增加启动参数

我们可以选着任何一种,访问结果如下

已启动配置:

Positive matches:

DispatcherServletAutoConfiguration matched:

- @ConditionalOnClass found required class 'org.springframework.web.servlet.DispatcherServlet'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

- @ConditionalOnWebApplication (required) found StandardServletEnvironment (OnWebApplicationCondition) DispatcherServletAutoConfiguration.DispatcherServletConfiguration matched:

- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

- Default DispatcherServlet did not find dispatcher servlet beans (DispatcherServletAutoConfiguration.DefaultDispatcherServletCondition) DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration matched:

- @ConditionalOnClass found required class 'javax.servlet.ServletRegistration'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

- DispatcherServlet Registration did not find servlet registration bean (DispatcherServletAutoConfiguration.DispatcherServletRegistrationCondition) ....

未自动配置:

Negative matches:

ActiveMQAutoConfiguration:

Did not match:

- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition) AopAutoConfiguration:

Did not match:

- @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition) ArtemisAutoConfiguration:

Did not match:

- @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition) BatchAutoConfiguration:

Did not match:

- @ConditionalOnClass did not find required classes 'org.springframework.batch.core.launch.JobLauncher', 'org.springframework.jdbc.core.JdbcOperations' (OnClassCondition) ...

五、 禁用自动配置

比如不想自动配置数据库连接,就可以用如何代码来关掉自动配置

/**
* 测试关闭数据库自动配置
* The type Data source config.
*/
@Configuration
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class DataSourceConfig { }

六、自定义banner及关闭banner

自定义spring boot 默认启动图案步骤:

1. 直接在src/main/resources下创建一个banner.txt

2. 访问网站http://patorjk.com/software/taag 生成字符,这里我们用”yiqixuejava”(一起学java),将生成的字符复制到banner.txt中,启动应用即可

启动结果:

        .__         .__                              __
___.__.|__| _____|__| ___ _____ __ ____ |__|____ ___ _______
< | || | / ____/ | \ \/ / | \_/ __ \ | \__ \\ \/ /\__ \
\___ || | < <_| | | > <| | /\ ___/ | |/ __ \\ / / __ \_
/ ____||__| \__ |__| /__/\_ \____/ \___ > /\__| (____ /\_/ (____ /
\/ |__| \/ \/ \______| \/ \/

后续会继续推出这一系列spring boot的文章

原文地址:http://blog.csdn.net/javastudyr/article/details/73824894

posted @
2018-01-24 09:33 
星朝 
阅读(...) 
评论(...) 
编辑 
收藏

最新文章

  1. Go结构体实现类似成员函数机制
  2. 让ABAP开发者更加轻松的若干快捷键
  3. Zookeeper(一)从抽屉算法到Quorum (NRW)算法
  4. 详解Bootstrap表单组件
  5. 2016 版 Laravel 系列入门教程(三)【最适合中国人的 Laravel 教程】
  6. MariaDB 加密特性及使用方法
  7. 如何让Form窗体接收KeyDown事件?
  8. Android 判断用户2G/3G/4G移动数据网络
  9. PHP项目中composer和Git的组合使用
  10. opencv linux
  11. SAP-SD-ABAP-VMOD 查找和应用SD模块用户出口(user exit) 好方法 .
  12. CodeForces 25E Test KMP
  13. html5 js控制音乐播放
  14. 2017最新技术java高级架构、千万高并发、分布式集群、架构师入门到精通视频教程
  15. Nginx学习——Nginx启动、停止、重启和信号控制以及平滑升级
  16. partition length exceeds the loop-partition-table-imposed maximum of 4294967295
  17. myeclipse 打开jsp文件出错
  18. 保留键的情况下取字典中最大的值(max\zip函数的联合使用)
  19. 添加本地jar包到maven仓库
  20. 利用redis List队列简单实现秒杀 PHP代码实现

热门文章

  1. 慢慢人生路,学点Jakarta基础-集合类
  2. call 方法和 apply方法
  3. host---域名查询
  4. Java Reflection - Getters and Setters
  5. ANSI-X99MAC算法和PBOC的3DES MAC算法
  6. 用 OPENSSL 生成不同格式的密钥
  7. mahout-distribution-0.9.tar.gz的安装的与配置、启动与运行自带的mahout算法
  8. CISP/CISA 每日一题 八
  9. 今天发现里一个非常好用的Listbox自绘类,带不同文字字体和图片,觉得很有必要记下来
  10. session应用二