SpringBoot的诞生就是为了解决spring配置繁杂,集成其他框架过程中冲突频发,因此,SpringBoot的特点就在于,配置简单,集成方便,开发流畅;
1.pom.xml:maven依赖配置文件:
--1.项目信息;
--2.jdk版本;
--3.项目依赖(开发过程中所需依赖加入此处);
--4.项目有maven构建
2.application.properties核心配置文件(springBoot配置文件有两种形式:application.properties(推荐),application.yml(生态不全,很多框架支持不完善)):
文件中可以配置非常多的属性(springBoot 1X 和springBoot 2X略有不同);
3.Resources/static资源文件,放一些dao层接口的映射文件xml,Resources/templates前端文件,前后分离很少在用;
4.Application启动类:
4.Application Test测试用例入口类:
5.构建一个简单的springBoot项目:<访问页面的时候输出一句话>
配置文件及依赖如上1,2项;
--1. IndexController类:
package com.zxy.springbootquickstart.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
/**
* 1.web项目此处使用RestController注解(推荐使用)
* 2.RestController与Controller的区别在于其提供对外的rest服务
* 3.RestController接收返回的都是josn
* 4.RestController的实现就是封装了@Controller和@ResponseBody
* @author zxy
*
*
*/
@RestController
public class IndexController {
/**
* 1.创建一个url入口
*/
@RequestMapping("/index")
public String index() {
return "---------hello welcome to SpringBoot!---------";
}
}
--2.AppConfig自定义配置类:
package com.zxy.springbootquickstart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**实现对于其他xml的配置与自定义需要扫描的文件或类,通常创建XXconfig;
* 1.创建AppConfig类增加Configuration;
* 2.Configuration是springframework即spring提供;
* 3.使用此注解代表让整个spring容器识别此类,并且加载此类;
* 4.可以理解为此类就是一个xml文件,被天然的引入到spring中;
* @author zxy
*
*/
@Configuration
public class AppConfig {
/**
* 1.数据源配置
* 2.参数配置
* 都可以直接写入此类
*/
/**
* 调用application.properties中自定义属性的方法有两种:
* 1. 通过注解的形式(推荐)
*/
@Value("${custom.group}")
private String customGroup;
/**
* 2.通过注入全局类方式
*/
@Autowired
private Environment environment;
public void output() {
System.out.println("通过@Value注解读取配置:"+this.customGroup);
System.out.println("通过注入Environment对象读取配置:"+environment.getProperty("custom.team"));
}
}
--3.修改Aplication入口程序:
package com.zxy.springbootquickstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//SpringApplication.run(Application.class, args);
ConfigurableApplicationContext ctx = SpringApplication.run(Application.class, args);
/**获取ConfigurableApplicationContext
* 设置引用启动时,打印AppConfig内容
* 通过反射获取AppConfig
*/
AppConfig appCfg = ctx.getBean(AppConfig.class);
appCfg.output();
}
}
输出结果: