一、导航

本节内容简介:

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 
星朝 
阅读(...) 
评论(...) 
编辑 
收藏

一起学JAVA之《spring boot》03 - 开始spring boot基本配置及项目结构(转)的更多相关文章

  1. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件

    精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件 内容简介:本文介绍 Spring Boot 的配置文件和配置管理,以及介绍了三种读取配置文 ...

  2. 五分钟学Java:一篇文章搞懂spring和springMVC

    原创声明 本文作者:黄小斜 转载请务必在文章开头注明出处和作者. 本文思维导图 什么是Spring,为什么你要学习spring? 你第一次接触spring框架是在什么时候?相信很多人和我一样,第一次了 ...

  3. Spring笔记03(Spring创建对象的三种方式)

    1.创建对象的三种方式和bean的生命周期的验证: Animal接口代码: package cn.pb.dao; /** * 动物接口 */ public interface Animal { //吃 ...

  4. 读懂这些spring boot的核心注解,快速配置完成项目搭建

    在spring boot中,摒弃了spring以往项目中大量繁琐的配置,遵循约定大于配置的原则,通过自身默认配置,极大的降低了项目搭建的复杂度.同样在spring boot中,大量注解的使用,使得代码 ...

  5. 从零开始学 Java - 我放弃了 .NET ?

    这不是一篇引起战争的文章 毫无疑问,我之前是一名在微软温暖怀抱下干了近三年的 .NET 开发者,为什么要牛(sha)X一样去搞 Java 呢?因为我喜欢 iOS 阿!哈哈,开个玩笑.其实,开始学 Ja ...

  6. 教妹学Java:Spring 入门篇

    你好呀,我是沉默王二,一个和黄家驹一样身高,刘德华一样颜值的程序员(管你信不信呢).从两位偶像的年纪上,你就可以断定我的码龄至少在 10 年以上,但实话实说,我一直坚信自己只有 18 岁,因为我有一颗 ...

  7. 51. spring boot属性文件之多环境配置【从零开始学Spring Boot】

    原本这个章节是要介绍<log4j多环境不同日志级别的控制的>但是没有这篇文章做基础的话,学习起来还是有点难度的,所以我们先一起了解下spring boot属性文件之多环境配置,当然文章中也 ...

  8. Spring Boot-初学01 -使用Spring Initializer快速创建Spring Boot项目 -@RestController+spEL -实现简单SpringBoot的Web页面

    1.IDEA:使用 Spring Initializer快速创建项目 IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目: 选择我们需要的模块:向导会联网创建Spring ...

  9. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

随机推荐

  1. ASP.NET Web.config学习

    花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. <? ...

  2. C/C++(数据结构链表的实现)

    链表 List 链表实现了内存零碎片的有效组织. 静态链表 链表中有两个成员,数据域和指针域 数据域:我们存储的数据. 指针域:指针指向下一个具体的节点,代表了下一个节点的类型是链表类型. 所谓的指针 ...

  3. 什么是事件委托?jquery和js怎么去实现?

    事件委托又叫事件代理,事件委托就是利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件. js: window.onload = function(){ var oul = docume ...

  4. c# 多态的美丽(虚方法、抽象、接口实现)

    面向对象3大特性:封装.继承.多态. 面向对象2大原则: 1)里氏替换原则:子类可以给父类,父类不能赋给子类. 2)开放封闭原则: 封装变化,降低耦合.(对扩展开放,对修改封闭) ********** ...

  5. python生成md5, shell生成md5

    echo -n 'aaa'|md5sum|cut -d ' ' -f1 python用hashlib md5=hashlib.md5(mid.upper()).hexdigest().upper()

  6. 怎样在nat方式的虚拟机下做ssh连接

    很多人在本机做測试都是用桥接的方式让虚拟机上网. 假设ip地址紧张或者根本就不同意我们拥有一个局域网的ip.这时候便能够使用NAT方式+putty来远程操作. 第一步,打开设备-Network-更改网 ...

  7. css大会站点顶部的一个特效

    看到http://css.w3ctech.com/ 上一个效果认为挺赞的. 然后学些了一下. demo地址:http://codepen.io/tianzi77/pen/mJaLWq html结构非常 ...

  8. numpy_basic

    一.Numpy是什么 Numerical Python,数值的Python,补充了Python语言所欠缺的数值计算能力. Numpy是其它数据分析及机器学习库的底层库. Numpy完全标准C语言实现, ...

  9. 洛谷 P1194 买礼物

    洛谷 P1194 买礼物 题目描述 又到了一年一度的明明生日了,明明想要买B样东西,巧的是,这B样东西价格都是A元. 但是,商店老板说最近有促销活动,也就是: 如果你买了第II样东西,再买第J样,那么 ...

  10. 【2017 Multi-University Training Contest - Team 7】Kolakoski

    [Link]:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=765 [Description] 有一种 ...