<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<!--导入一个父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<dependencies>
<!--添加starter-web模块的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
package com;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//来标注一个主程序类,说明这是一个springBoot应用程序
@SpringBootApplication
public class Hello {
public static void main(String[] args)
{
//spring应用启动起来
SpringApplication.run(Hello.class,args);
}
}
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello()
{
return "hello world";
}
}
<!-- 将应用打包成可执行jar包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
命令行使用java -jar的命令执行这个jar包即可,记住先要来到当前jar包对应的目录,通过cd 目录路径–》来到对应目录
//来标注一个主程序类,说明这是一个springBoot应用程序
@SpringBootApplication
public class Hello {
public static void main(String[] args)
{
//spring应用启动起来
SpringApplication.run(Hello.class,args);
}
}
下面是@SpringBootApplication个注解的内部信息:
下面解释这里面的组合注解的意思:
//这个类的所有方法返回的数据直接写给浏览器(如果是对象转为json数据)
/*@ResponseBody
@Controller*/
@RestController//替换上面两个注解
public class helloController {
@RequestMapping("/hello")
public String hello()
{
return "大忽悠";
}
}
yaml:
server:
port: 8081
person:
name: 大忽悠
age: 18
boss: false
birth: 2002/1/2
maps: {k1: v1,k2: 12}
lists:
- 历史
- 语文
- 数学
dog:
name: 小狗
age: 1
peo类:
/*
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中的相关的配置进行绑定
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
* 只有这个组件是容器中的组件,才能使用容器中提供的@ConfigurationProperties功能
* */
@Controller
@ConfigurationProperties(prefix = "person")
public class peo
{
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@Override
public String toString() {
return "peo{" +
"name='" + name + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
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;
}
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> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
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;
}
}
springboot的注释配置处理器没有在类路径下找到,需要导入配置文件处理器的依赖
<!-- 导入配置文件处理器,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBootQuickStartOneApplicationTests {
@Autowired
peo people;
@Test
void contextLoads()
{
System.out.println(people);
}
}
#配置person的值
person.name=大忽悠
person.age=18
person.birth=2002/1/2
person.maps.k1=v1
person.maps.k2=14
person.lists=1,2,3
person.dog.name=dog
person.dog.age=2
IDEA springboot项目中properties配置文件 {针对将对应GBK改为UTF-8并勾选转为ASCII后仍无效情况} 运行时中文乱码解决
@Value注解使用演示:
@Controller
public class peo
{
/*
* <bean>
<property name="name" value="字面值/${key}从环境变量,配置文件中获取值/#{SPEL}"></property>
* </bean>
* */
@Value("${person.name}")
private String name;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
不要单独引入包,可能引入的包不全导致不生效!
直接引入SpringBoot 的starter
<!--引入JSR303校验启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
注解@ConfigurationProperties 和 @Value 对比
@PropertySource(value={"classpath:peo.properties"})
@Controller
@ConfigurationProperties(prefix = "person")
public class peo
{
private String name;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
@PropertySource和@ConfigurationProperties
【小家Spring】一篇文章彻底搞懂Spring Boot配置文件的加载顺序(项目内部配置和外部配置)
spring.xml
<?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.xsd">
<bean class="com.Bean.pig" id="pig"/>
</beans>
springBoot单元测试:
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBootQuickStartOneApplicationTests {
@Autowired
peo people;
@Autowired //注入IOC容器
ApplicationContext ioc;
@Test
void contextLoads()
{
//判断容器中是否由pig
boolean ret = ioc.containsBean("pig");
System.out.println(ret);
}
}
如果此时不在配置类上导入我们写的spring的配置文件,那么结果为false,即容器中没有保存对应的Bean
如果写了,如下面这样:
@ImportResource(locations = {"classpath:spring.xml"})
@SpringBootApplication
public class SpringBootQuickStartOneApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootQuickStartOneApplication.class, args);
}
}
配置类:
@Configuration//指明当前类是一个配置类,替代之前Spring配置文件
public class MyConfig
{
@Bean("pig")//将方法的返回值添加到容器中,容器中这个组件默认的id就是方法名(不是首字母小写)
public pig HelloPig()
{
return new pig();
}
}
单元测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
class SpringBootQuickStartOneApplicationTests {
@Autowired
peo people;
@Autowired //注入IOC容器
ApplicationContext ioc;
@Test
void contextLoads()
{
//判断容器中是否由pig
boolean ret = ioc.containsBean("HelloPig");
System.out.println(ret);
}
}
pringboot多环境(dev、test、prod) 配置详解可以看下面这篇文章
springboot 如何引用外部配置文件(spring.config.location)
1、命令行参数 2、来自 java:comp/env 的 JNDI 属性 3、使用“spring.config.location”改变默认的配置文件位置 4、Java 系统属性(System.getProperties()) 5、操作系统环境变量 6、RandomValuePropertySource 配置的 random.* 属性值 7、jar 包外部的 application-{profile}.properties 或 application.yml (带spring.profile) 配置文件 8、jar 包内部的 application-{profile}.properties 或 application.yml (带spring.profile) 配置文件 9、jar 包外部的 application.properties 或 application.yml (不带spring.profile) 配置文件 10、jar 包内部的 application.properties 或 application.yml (不带spring.profile) 配置文件 11、@Configuration注解类上的@PropertySource 12、通过SpringApplication.setDefaultProperties指定的默认属性
Spring Boot 配置文件加载位置与顺序、外部配置文件加载顺序
关与 @EnableConfigurationProperties 注解
关于 @EnableConfigurationProperties 注解