赞
踩
原文:https://www.cnblogs.com/awzf/p/9953216.html
作者:我风依旧
出处:https://www.cnblogs.com/awzf/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
目录
注意: jdk必须1.8
Finish 完成创建
由于springboot先天性集成了spring/springmvc,所以我们不需要编写相关配置文件,直接开发即可。
Springboot的核心配置文件主要是来修改一些基本配置(服务器端口号、项目访问名字)以及集成配置(数据源、mybatis、redis等等)
Springboot的核心配置文件分为两种形式:(注意:名字都必须为application)
Springboot的application.properties/application.yml 文件还可以配置一些自定义属性,用来给对象属性赋值,方便代码维护,以及解耦合
应用场景:springMvc文件上传,定义全局文件夹路径。
1、在application.yml中添加自定义属性配置
方式一:通过@Value(“${upload.imagePath}”)
方式二:定义通用映射解析
2、在application.properties中添加自定义属性配置
1.application.properties配置文件中定义自定义属性
com.sam.name=sam com.sam.age=11 com.sam.desc=magical sam
2、编写Bean类,加载属性
Sam类需要添加@Component注解,让spring在启动的时候扫描到该类,并添加到spring容器中。
第一种:使用spring支持的@Value()加载
package com.sam.demo.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
public class Sam {
//获取application.properties的属性
@Value("${com.sam.name}")
private String name;
@Value("${com.sam.age}")
private int age;
@Value("${com.sam.desc}")
private String desc;
//getter & setter
}
第二种:使用@ConfigurationProperties(prefix="") 设置前缀,属性上不需要添加注解。
package com.sam.demo.conf;
import org.springframework.stereotype.Component;
/**
* @author sam
* @since 2017/7/15
*/
@Component
@ConfigurationProperties(prefix = "com.sam")
public class Sam {
private String name;
private int age;
private String desc;
//getter & setter
}
3、在controller中注入并使用Sam这个Bean。
package com.sam.demo.controller;
import com.sam.demo.conf.Sam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author sam
* @since 2017/7/14
*/
@RestController
public class IndexController {
@Autowired
private Sam sam;
@RequestMapping("/index")
public String index() {
System.out.println(sam.getName() + " " + sam.getAge() + " " + sam.getDesc());
return "index";
}
}
在实际开发过程中,不同的开发环境,我们使用的配置信息也是不一样的,这关系到连接池配置、工厂配置等等
例如:
生产环境(线上环境)
开发环境(线下环境)
测试环境(单元测试)
不同的环境我们要指定不同的配置文件,相对springboot给我们提供了这样的便利。
注意:也可以在application.properties总配置文件中引入application-dev.properties(开发)或application-prod.properties(生产)或application-test.properties(测试) 达到动态切换环境的目的
application.properties:
<!--mybatis 依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!--连接池配置-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.31</version>
</dependency>
<!--log4j-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
3.Demo测试
1.手动封装拦截策略
2.直接引入xml配置文件
在启动时 在启动类上通过@ImportResource(“classpath:shiro-config.xml”),初始化shiro配置
4.启动测试
注意: 如果在访问jsp的过程中 发生下载现象
SpringBoot不推荐使用JSP作为View,而是推荐我们使用模板(如:thymeleaf、freemarker等模板引擎),原因如下:
1. JSP性能较差
2. 绝对的前后端分离思想,JSP并不利于页面调试(运行依赖于web容器)
3. SpringBoot对内嵌web容器的支持默认也是用tomcat。但tomcat对web资源的处理上写死了要使用文件目录,对于打包成jar包的SpringBoot
应用来说,显然不行,也有的人打包成war,然后还是部署到tomcat上,这样违反了SpringBoot的初衷,这样一来,等于否定了嵌入式容
器,而且程序员还要处理嵌入式环境和外部tomcat环境的不同带来的问题。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。