第一节:项目内置属性

application.properties配置整个项目的,相当于以前的web.xml;
注意到上一节的访问HelloWorld时,项目路径也没有加;直接是http://localhost:8080/helloWorld;
因为它默认的server.servlet.context-path=/
修改如下:
src/main/resource/application.properties:
server.port=8888
server.servlet.context-path=/HelloWorld

重新启动,输入http://localhost:8888/HelloWorld/helloWorld,页面显示spring boot你好;

1.port端口变成了8888;

2.项目根路径变了,context-path是/HelloWorld了;

第二节:自定义属性

可以在application.properties中配置一些自定义属性:xx.xx也行:

使用@Value("${key}")来注入到属性值中;

server.port=8888
server.servlet.context-path=/HelloWorld helloWorld=spring boot hello! mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_root
mysql.userName=root
mysql.password=123456

com.cy.controller.HelloWorldController.java:

package com.cy.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloWorldController { @Value("${helloWorld}")
private String helloWorld; @Value("${mysql.jdbcName}")
private String jdbcName; @Value("${mysql.dbUrl}")
private String dbUrl; @Value("${mysql.userName}")
private String userName; @Value("${mysql.password}")
private String password; @RequestMapping("/helloWorld")
public String say(){
return helloWorld;
} @RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+jdbcName+"<br/>"
+"mysql.dbUrl:"+dbUrl+"<br/>"
+"mysql.userName:"+userName+"<br/>"
+"mysql.password:"+password+"<br/>";
}
}

浏览器http://localhost:8888/HelloWorld/helloWorld,显示:spring boot hello!

浏览器http://localhost:8888/HelloWorld/showJdbc,显示:

mysql.jdbcName:com.mysql.jdbc.Driver
mysql.dbUrl:jdbc:mysql://localhost:3306/db_root
mysql.userName:root
mysql.password:123456

第三节:ConfigurationProperties 配置
上面自定义属性,如果写了很多呢,在多个地方用到,那么还一个一个的写,@Value("${key}"),就很麻烦了。
使用封装。
com.cy.properties.MysqlProperties.java:
package com.cy.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; /**
* mysql属性配置文件
* @author CY
*
*/
@Component
@ConfigurationProperties(prefix="mysql")
public class MysqlProperties {
private String jdbcName;
private String dbUrl;
private String userName;
private String password; public String getJdbcName() {
return jdbcName;
}
public void setJdbcName(String jdbcName) {
this.jdbcName = jdbcName;
}
public String getDbUrl() {
return dbUrl;
}
public void setDbUrl(String dbUrl) {
this.dbUrl = dbUrl;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
} }

HelloWorldController.java中使用它:

package com.cy.controller;

import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.cy.properties.MysqlProperties; @RestController
public class HelloWorldController { @Resource
private MysqlProperties mysqlProperties; @Value("${helloWorld}")
private String helloWorld; @RequestMapping("/helloWorld")
public String say(){
return helloWorld;
} @RequestMapping("/showJdbc")
public String showJdbc(){
return "mysql.jdbcName:"+mysqlProperties.getJdbcName()+"<br/>"
+"mysql.dbUrl:"+mysqlProperties.getDbUrl()+"<br/>"
+"mysql.userName:"+mysqlProperties.getUserName()+"<br/>"
+"mysql.password:"+mysqlProperties.getPassword()+"<br/>";
}
}

浏览器http://localhost:8888/HelloWorld/showJdbc,显示之前一样;

 
 
 
 
 
 
 

spring boot学习(2) SpringBoot 项目属性配置的更多相关文章

  1. Github点赞超多的Spring Boot学习教程+实战项目推荐!

    Github点赞接近 100k 的Spring Boot学习教程+实战项目推荐!   很明显的一个现象,除了一些老项目,现在 Java 后端项目基本都是基于 Spring Boot 进行开发,毕竟它这 ...

  2. Spring Boot 系列(三)属性配置&自定义属性配置

    在使用spring boot过程中,可以发现项目中只需要极少的配置就能完成相应的功能,这归功于spring boot中的模块化配置,在pom.xml中依赖的每个Starter都有默认配置,而这些默认配 ...

  3. spring boot 学习(十)SpringBoot配置发送Email

    SpringBoot配置发送Email 引入依赖 在 pom.xml 文件中引入邮件配置: <dependency> <groupId>org.springframework. ...

  4. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  5. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  6. SpringBoot项目属性配置-第二章

    SpringBoot入门 1. 相信很多人选择Spring Boot主要是考虑到它既能兼顾Spring的强大功能,还能实现快速开发的便捷.我们在Spring Boot使用过程中,最直观的感受就是没有了 ...

  7. Spring Boot学习记录03_一些属性配置文件

    转自:http://blog.didispace.com/springbootproperties/ 多环境配置(这个地方跟maven的profile配置有点类似) 我们在开发Spring Boot应 ...

  8. spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)

    第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...

  9. SpringBoot项目属性配置

    如果使用IDEA创建Springboot项目,默认会在resource目录下创建application.properties文件,在SpringBoot项目中,也可以使用yml类型的配置文件代替pro ...

随机推荐

  1. The "Double-Checked Locking is Broken" Declaration

    双重检查锁定在延迟初始化的单例模式中见得比较多(单例模式实现方式很多,这里为说明双重检查锁定问题,只选取这一种方式),先来看一个版本: public class Singleton { private ...

  2. web.xml的分析

    <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" ...

  3. addslash()

    php addslashes函数的作用是在预定义的字符前面加上反斜杠,这些预定义字符包括: 单引号(') 双引号(") 反斜杠(\) NULL addslashes函数经常使用在向数据库插入 ...

  4. P1002 谁拿了最多奖学金

    P1002 谁拿了最多奖学金 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 NOIP2005复赛提高组第一题 描述 某校的惯例是在每学期的期末考试之后发放奖 ...

  5. Apache和iis的冲突处理

    http://wenku.baidu.com/link?url=N4GYFpkQyr8G0kVEy3AR2Q5FBho8EOle-_5inEfEq6QSxlyzB3xSbcpeugRdExkSU-tw ...

  6. js获取 gps坐标

    if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(pos) { model.address. ...

  7. php 加载字体 并保存成图片

    // Set the content-type header("Content-type: image/png"); // Create the image $im = image ...

  8. hdu2065 "红色病毒"问题 指数型母函数

    关于指数型母函数的题目,通过用公式并展开得到系数做的吧,取最后两位就是对100取模 #include<stdio.h> int QuickPow(int a,long long n,int ...

  9. MySQL表类型MyISAM/InnoDB的区别(解决事务不回滚的问题)(转)

    本文参考: http://mysqlpub.com/thread-5383-1-1.html http://blog.csdn.net/c466254931/article/details/53463 ...

  10. MySQL--修改表字段

    ##========================================================================## ## 修改表字段 ## CHANGE和MODI ...