ä»ç»ï¼example02
ç¸ä¿¡å¾å¤äººéæ©Spring Bootä¸»è¦æ¯èèå°å®æ¢è½å ¼é¡¾Springç强大åè½ï¼è¿è½å®ç°å¿«éå¼åç便æ·ãæä»¬å¨Spring Boot使ç¨è¿ç¨ä¸ï¼æç´è§çæåå°±æ¯æ²¡æäºåæ¥èªå·±æ´åSpringåºç¨æ¶ç¹å¤çXMLé ç½®å å®¹ï¼æ¿ä»£å®çæ¯å¨pom.xmlä¸å¼å ¥æ¨¡ååçStarter POMsï¼å ¶ä¸å个模å齿èªå·±çé»è®¤é ç½®ï¼æä»¥å¦æä¸æ¯ç¹æ®åºç¨åºæ¯ï¼å°±åªéè¦å¨application.propertiesä¸å®æä¸äºå±æ§é 置就è½å¼å¯å模åçåºç¨ã
é ç½®æä»¶application.propertiesç使ç¨ï¼ä¸»è¦ç¨æ¥é ç½®æ°æ®åºè¿æ¥ãæ¥å¿ç¸å ³é ç½®çãé¤äºè¿äºé ç½®å 容ä¹å¤ï¼æ¬æå°å ·ä½ä»ç»ä¸äºå¨application.propertiesé ç½®ä¸çå ¶ä»ç¹æ§åä½¿ç¨æ¹æ³ã
æä»¬å¨ä½¿ç¨Spring Bootçæ¶åï¼é常ä¹éè¦å®ä¹ä¸äºèªå·±ä½¿ç¨ç屿§ï¼æä»¬å¯ä»¥å¦ä¸æ¹å¼ç´æ¥å®ä¹ï¼
application.properties
test.springboot.name=å¦ä¹
test.springboot.context=æ
#åæ°é´å¼ç¨
test.springboot.desc=${test.springboot.context}ç±${test.springboot.name}
#éæºæ°
##éæºå符串
test.springboot.value=${random.value}
##éæºint
test.springboot.number=${random.int}
##éæºlong
test.springboot.bignumber=${random.long}
##10以å
çéæºæ°
test.springboot.test1=${random.int()}
##10-20çéæºæ°
test.springboot.test2=${random.int[,]}
#å¤ç¯å¢é
ç½®æä»¶æ¿æ´»å±æ§
spring.profiles.active=dev
# æå¡ç«¯å£
server.port=
##设置utf-8ç¼ç é
server.tomcat.uri-encoding=UTF-
spring.http.encoding.charset=UTF-
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-
test.springboot.descåæ°å¼ç¨äºä¸æä¸å®ä¹çnameåcontext屿§ï¼æåè¯¥å±æ§çå¼å°±æ¯:æç±å¦ä¹
å¼ç¨å¼ï¼éè¿@Value(â${屿§å}â)注解æ¥å 载对åºçé ç½®å±æ§ï¼å ·ä½å¦ä¸ï¼
å建å®ä½ç±»ï¼TestProperties
package com.lyd.entity;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class TestProperties {
@Value("${test.springboot.name}")
private String name;
@Value("${test.springboot.context}")
private String context;
@Value("${test.springboot.desc}")
private String desc;
//æµè¯éæºæ°é
ç½®
@Value("${test.springboot.value}")
private String value;
//get/setæ¹æ³...
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Application.java
package com.springboot.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author : xiaolin
* @Description: Created by Administrator on 2017/11/22.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
ApplicationTest.javaåå æµè¯ç±»
package com.lyd;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.validator.internal.util.privilegedactions.GetConstraintValidatorList;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.lyd.entity.TestProperties;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTest {
private static final Log log = LogFactory.getLog(ApplicationTest.class);
@Autowired
private TestProperties testProperties;
@Test
public void getHello(){
Assert.assertEquals(testProperties.getName(), "å¦ä¹ ");
Assert.assertEquals(testProperties.getDesc(), "æç±å¦ä¹ ");
System.out.println(testProperties.getName());
log.info("éæºå符串æµè¯ï¼"+testProperties.getValue());
}
}
pomæä»¶
<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>
<parent>
<groupId>com.lyd</groupId>
<artifactId>springboot-lyd</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>example02</artifactId>
</project>
项ç®ç®å½

è¿è¡getHelloæ¹æ³ï¼å¯ä»¥è¯»å°application.propertiesä¸çæ°æ®ï¼