@Value不支持松散语法绑定,不支持复杂类型封装
@ConfigurationProperties 批量注入配置文件属性,不支持SPEL,支持JSR303数据校验
@Validate
@Email
如果需要在
@PropertySource指定要加载的配置文件
@ImportSource加载自己写的beans.xml
@Bean给容器中添加组件
随机数、:指定默认值
默认使用application.properties
激活指定profile spring.profiles.acvate=
命令行激活
yml支持多文档块
<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.0modelVersion>
<groupId>com.xinggroupId>
<artifactId>spring-boot-01-helloworldartifactId>
<version>1.0-SNAPSHOTversion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-configuration-processorartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
package com.xing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@ImportResource(locations = {"classpath:beans.xml"})
//标注一个主程序类
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//spring 启动
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
package com.xing;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将本类中的所有属性和配置文件中相关配置进行绑定
*
* 只有这个组件是容器中的组件,才能提供@ConfigurationProerpties的功能。
* 默认从全局配置文件中获取
*/
@PropertySource(value = {"classpath:person.properties"})
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
/**
*
*/
@Value("${person.last-name}")
private String lastName;
@Value("#{11*22}")
private Integer age;
@Value("true")
private Boolean boss;
private Date birth;
private Map<String,Object> mps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", mps=" + mps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMps() {
return mps;
}
public void setMps(Map<String, Object> mps) {
this.mps = mps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
package com.xing;
public class Dog {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.xing.service;
public class HelloService {
}
package com.xing.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.RestController;
@RestController
public class HelloController {
@Value("${person.last-name}")
private String name;
@RequestMapping("/hello")
public String hello(){
return "Hello,World!"+name;
}
}
package com.xing.config;
import com.xing.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:指明当前类是是一个配置类,就是代替之前的spring配置文件
* 以前是在 添加组件
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中,容器中这个组件的默认id即方法名
@Bean
public HelloService helloService(){
System.out.println("配置类@Bean给容器中添加组件了");
return new HelloService();
}
}
#server.port=8080
#配置person属性
#person.last-name=张国伟
#person.age=18
#person.birth=1999/12/19
#person.boss=false
#person.mps.k1=v1
#person.maps.k2=14
#person.lists=a,b,c
#person.dog.name=dog
#person.dog.age=10
server:
port: 8080
person:
lastName: zhangsan
age: 18
boss: false
birth: 2017/12/12
maps: {k1: v1,k2: 12}
lists:
- lisi
- zhaoliu
- wangwu
dog:
name: 小狗
age: 2
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloService" class="com.xing.service.HelloService">bean>
beans>
#配置person属性
person.last-name=张国伟
person.age=18
person.birth=1999/12/19
person.boss=false
person.mps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=10
import com.xing.HelloWorldMainApplication;
import com.xing.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* spring boot单元测试
* 可以在测试期间很方便类似编码一样进行自动注入
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HelloWorldMainApplication.class)
public class ApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void contextLoads(){
System.out.println(person);
}
@Test
public void testHelloService(){
System.out.println(ioc.containsBean("helloService"));
}
}