SpringBoot - 简介

Ps:一般不会用Maven来创建SpringBoot项目,会选择SpringBoot Initializr构建SpringBoot应用。

Ps:@Entiy 针对于data-jpa使用。

Ps:如果使用debug=true,则SpringBoot自动覆盖logging.level.root=debug。

Ps:YAML的出现避免了将所有的配置没有集中性的在一起,导致需要修改某配置的时候,需要一条一条肉眼搜索。

Ps:Jar包可自动加载同目录的application配置文件:意思是如果出现修改一些配置文件信息,总不能将package好的jar包解压取出配置文件--修改--再package,太麻烦,直接复制出需要的配置文件信息--修改好后--粘贴到与jar包同目录下的地方即可,重启启动Jar包会优先使用同目录下的配置文件覆盖Jar包里的同名的配置文件。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.imooc</groupId>
	<artifactId>myspringboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>myspringboot</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

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

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
// application.yml

spring:
 profiles:
  active: prod
  
#debug: true
##logging.level.root
##logging.file
#logging:
# level:
#  root: info
# file: e:/myspringboot.log
#
#spring:
#  datasource:
#    driver-class-name: com.mysql.jdbc.Driver
#    url: jdbc:mysql://localhost:3306/test
#    username: root
#    password: 123456
#mall:
# config:
#  name: 爱美商城
#  description: 这是一家化妆品特卖网站
#  hot-sales: 20
#  show-advert: true
// application-dev.yml

debug: true

#logging.level.root
#logging.file
logging:
 level:
  root: info
 file: e:/myspringboot.log

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test
    username: root
    password: 123456

mall:
 config:
  name: 爱美商城
  description: 这是一家化妆品特卖网站
  hot-sales: 20
  show-advert: true
// application-prd.yml

debug: false

#logging.level.root
#logging.file
logging:
 level:
  root: info
 file: /local/user/app-prd.log

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://155.32.55.88:3307/prd
    username: root1
    password: 3313@#!

mall:
 config:
  name: 优美商城
  description: 这是一家化妆品特卖网站
  hot-sales: 20
  show-advert: true

server:
  port: 80
package com.imooc.myspringboot.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {

    @Value("${mall.config.name}")
    private String name;

    @Value("${mall.config.description}")
    private String description;

    @Value("${mall.config.hot-sales}")
    private Integer hotSales;

    @Value("${mall.config.show-advert}")
    private Boolean showAdvert;

    @RequestMapping("/out")
    @ResponseBody
    public String out(){
        return "success";
    }

    @RequestMapping("/info")
    @ResponseBody
    public String info(){
        return String.format("name:%s,description:%s,hot-sales:%s,show-advert:%s",
                name,description,hotSales,showAdvert);
    }
}
package com.imooc.myspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyspringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyspringbootApplication.class, args);
    }
}
package com.imooc.myspringboot;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyspringbootApplicationTests {

	@Test
	public void contextLoads() {
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陆氪和他的那些代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值