Spring Boot 配置
项目
pom.xml


1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <parent> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-parent</artifactId> 9 <version>2.1.6.RELEASE</version> 10 <relativePath/> <!-- lookup parent from repository --> 11 </parent> 12 13 <groupId>com.ahabest.springboot</groupId> 14 <artifactId>Configuration</artifactId> 15 <version>0.0.1-SNAPSHOT</version> 16 <name>Configuration</name> 17 <description>Configuration</description> 18 19 <properties> 20 <java.version>1.8</java.version> 21 </properties> 22 23 <dependencies> 24 <dependency> 25 <groupId>org.springframework.boot</groupId> 26 <artifactId>spring-boot-starter-web</artifactId> 27 </dependency> 28 <!-- 导入配置文件处理器,配置文件编写会有提示--> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-configuration-processor</artifactId> 32 <optional>true</optional> 33 </dependency> 34 35 <dependency> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-starter-test</artifactId> 38 <scope>test</scope> 39 </dependency> 40 </dependencies> 41 42 <build> 43 <plugins> 44 <plugin> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-maven-plugin</artifactId> 47 </plugin> 48 </plugins> 49 </build> 50 51 </project>
Application.java
1 package com.ahabest.springboot; 2 import org.springframework.boot.SpringApplication; 3 import org.springframework.boot.autoconfigure.SpringBootApplication; 4 import org.springframework.context.annotation.ImportResource; 5 //加载spring配置文件 6 @ImportResource(locations = {"classpath:beans.xml"}) 7 @SpringBootApplication 8 public class ConfigurationApplication { 9 public static void main(String[] args) { 10 SpringApplication.run(ConfigurationApplication.class, args); 11 } 12 }
注解类型
@ConfigurationProperties与@Value获取全局配置文件属性值
Person.java
1 package com.ahabest.springboot.bean; 2 import java.util.Date; 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 //将配置文件的属性值映射到这个组件中 6 //添加组件 7 @Component 8 //加载配置文件及引用配置文件属性 9 @ConfigurationProperties(prefix = "person") 10 public class Person { 11 private String LastName; 12 private Integer age; 13 private Boolean boss; 14 private Date birth; 15 private Dog dog; 16 public String getLastName() { 17 return LastName; 18 } 19 public void setLastName(String lastName) { 20 LastName = lastName; 21 } 22 public Integer getAge() { 23 return age; 24 } 25 public void setAge(Integer age) { 26 this.age = age; 27 } 28 public Boolean getBoss() { 29 return boss; 30 } 31 public void setBoss(Boolean boss) { 32 this.boss = boss; 33 } 34 public Date getBirth() { 35 return birth; 36 } 37 public void setBirth(Date birth) { 38 this.birth = birth; 39 } 40 public Dog getDog() { 41 return dog; 42 } 43 public void setDog(Dog dog) { 44 this.dog = dog; 45 } 46 @Override 47 public String toString() { 48 return "Person [LastName=" + LastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", dog=" + dog 49 + "]"; 50 } 51 }
Dog.java
1 package com.ahabest.springboot.bean; 2 public class Dog { 3 private String name; 4 private Integer age; 5 public String getName() { 6 return name; 7 } 8 public void setName(String name) { 9 this.name = name; 10 } 11 public Integer getAge() { 12 return age; 13 } 14 public void setAge(Integer age) { 15 this.age = age; 16 } 17 @Override 18 public String toString() { 19 return "Dog [name=" + name + ", age=" + age + "]"; 20 } 21 }
只在某个业务逻辑中获取一下配置文件中的某项值使用@Value比较合适
1 package com.ahabest.springboot.bean; 2 import java.util.Date; 3 import org.springframework.beans.factory.annotation.Value; 4 //import org.springframework.boot.context.properties.ConfigurationProperties; 5 import org.springframework.stereotype.Component; 6 //将配置文件的属性值映射到这个组件中 7 //添加组件 8 @Component 9 //加载配置文件及引用配置文件属性 10 //@ConfigurationProperties(prefix = "person") 11 public class Person { 12 //@Value获取配置文件属性值及给属性赋值 13 @Value("${person.last-name}") 14 private String LastName; 15 @Value("#{11*2}") 16 private Integer age; 17 @Value("true") 18 private Boolean boss; 19 private Date birth; 20 private Dog dog; 21 public String getLastName() { 22 return LastName; 23 } 24 public void setLastName(String lastName) { 25 LastName = lastName; 26 } 27 public Integer getAge() { 28 return age; 29 } 30 public void setAge(Integer age) { 31 this.age = age; 32 } 33 public Boolean getBoss() { 34 return boss; 35 } 36 public void setBoss(Boolean boss) { 37 this.boss = boss; 38 } 39 public Date getBirth() { 40 return birth; 41 } 42 public void setBirth(Date birth) { 43 this.birth = birth; 44 } 45 public Dog getDog() { 46 return dog; 47 } 48 public void setDog(Dog dog) { 49 this.dog = dog; 50 } 51 @Override 52 public String toString() { 53 return "Person [LastName=" + LastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", dog=" + dog 54 + "]"; 55 } 56 }
HelloController.java
1 package com.ahabest.springboot.controller; 2 import org.springframework.beans.factory.annotation.Value; 3 import org.springframework.web.bind.annotation.RequestMapping; 4 import org.springframework.web.bind.annotation.RestController; 5 @RestController 6 public class HelloController { 7 @Value("${person.last-name}") 8 private String name; 9 @RequestMapping("/sayHello") 10 public String sayHello() { 11 return "Hello" + name; 12 } 13 }
@PropertySource获取指定配置文件
package com.ahabest.springboot.bean; import java.util.Date; //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; //将配置文件的属性值映射到这个组件中 //加载指定的配置文件及属性 @PropertySource(value = { "classpath:person.properties" }) //添加组件 @Component //加载全局配置文件及引用全局配置文件属性 @ConfigurationProperties(prefix = "person") public class Person { //@Value获取配置文件属性值及给属性赋值 //@Value("${person.last-name}") private String LastName; //@Value("#{11*2}") private Integer age; //@Value("true") private Boolean boss; private Date birth; private Dog dog; public String getLastName() { return LastName; } public void setLastName(String lastName) { 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 Dog getDog() { return dog; } public void setDog(Dog dog) { this.dog = dog; } @Override public String toString() { return "Person [LastName=" + LastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", dog=" + dog + "]"; } }
@ImportResource导入spring的配置文件使配置文件里的内容生效
Application.java beans.xml
HelloService.java
1 package com.ahabest.springboot.service; 2 3 public class HelloService { 4 5 }
@Bean注册类
MyAppConfig.java
1 package com.ahabest.springboot.config; 2 import org.springframework.context.annotation.Bean; 3 import org.springframework.context.annotation.Configuration; 4 import com.ahabest.springboot.service.HelloService; 5 //说明当前类是一个配置类用来代替spring配置文件 6 @Configuration 7 public class MyAppConfig { 8 //将方法的返回值添加到容器中,容器中的这个组件的默认的id就是方法名 9 @Bean 10 public HelloService helloService() { 11 return new HelloService(); 12 } 13 }
配置文件类型
application.properties与application.yml全局配置文件
application.properties
#配置服务器端口号 server.port=9001 #配置person属性值 person.last-name=zhangsan person.age=26 person.boss=false person.birth=1992/12/13 person.dog.name=xiaohua person.dog.age=2
application.yml
#配置服务器端口号 server: port: 9002 #配置person属性 person: last-name: lisi age: 28 boss: true birth: 1992/12/14 dog: name: xiaoli age: 3
person.propertites指定的配置文件
person.properties
person.last-name=wangwu person.age=26 person.boss=false person.birth=1992/12/13 person.dog.name=xiaohua person.dog.age=2
beans.xml spring的配置文件
<?xml version="1.0" encoding="UTF-8"?> <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-3.0.xsd"> <bean id="helloService" class="com.ahabest.springboot.service.HelloService"></bean> </beans>
test.java
1 package com.ahabest.springboot; 2 import org.junit.Test; 3 import org.junit.runner.RunWith; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.test.context.SpringBootTest; 6 import org.springframework.context.ApplicationContext; 7 import org.springframework.test.context.junit4.SpringRunner; 8 import com.ahabest.springboot.bean.Person; 9 @RunWith(SpringRunner.class) 10 @SpringBootTest 11 public class ConfigurationApplicationTests { 12 @Autowired 13 //测试全局配置文件 14 Person person; 15 @Autowired 16 //测试@ImportResource配置文件 17 ApplicationContext ioc; 18 @Test 19 public void testHelloService() { 20 boolean b = ioc.containsBean("helloService"); 21 //输出true说明spring配置文件成功 22 System.out.println(b); 23 } 24 @Test 25 public void contextLoads() { 26 System.out.println(person); 27 } 28 }