1. 生成可直接运行的jar包
pom.xml–>idea右侧maven–>Lifecyde.package–>生成target.xxx.jar包–>win+R–>cmd–>java -jar jar包路径–>回车
C:\Users\朱朱呀>java -jar E:\work\msb\springboot\springboot-study\target\springboot-study-0.0.1-SNAPSHOT.jar
___ ___ ___
|_ / |_ / |_ /
_/__| _/__| _/__|
_|"""""|_|"""""|_|"""""|
"`-0-0-'"`-0-0-'"`-0-0-'
2021-10-29 13:55:13.215 INFO 12164 --- [ main] c.mashibing.SpringbootStudyApplication : Starting SpringbootStudyApplication v0.0.1-SNAPSHOT using Java 1.8.0_91 on DESKTOP-11QBP50 with PID 12164 (E:\work\msb\springboot\springboot-study\target\springboot-study-0.0.1-SNAPSHOT.jar started by 朱朱呀 in C:\Users\朱朱呀)
2021-10-29 13:55:13.228 INFO 12164 --- [ main] c.mashibing.SpringbootStudyApplication : No active profile set, falling back to default profiles: default
2021-10-29 13:55:17.839 INFO 12164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-29 13:55:17.884 INFO 12164 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-29 13:55:17.885 INFO 12164 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.54]
2021-10-29 13:55:18.074 INFO 12164 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-29 13:55:18.075 INFO 12164 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 4666 ms
2021-10-29 13:55:19.334 INFO 12164 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-29 13:55:19.387 INFO 12164 --- [ main] c.mashibing.SpringbootStudyApplication : Started SpringbootStudyApplication in 7.456 seconds (JVM running for 8.917)
2. 加载配置文件的两种方式
- 加载yaml:创建resources.application.yaml文件
SpringBoot学习文档:
https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/html/spring-boot-features.html#boot-features-external-config-loading-yaml
environments:
dev:
url: https://dev.example.com
name: Developer Setup
prod:
url: https://another.example.com
name: My Cool App
- 创建resources.application.properties文件
environments.dev.url=https://dev.example.com
environments.dev.name=Developer Setup
environments.prod.url=https://another.example.com
environments.prod.name=My Cool App
- 两种配置文件都设置时,默认用第二种
3. 把yaml里的值赋值给Person.java类的属性
- application.yaml文件内容
// 自定义属性:
person:
name: zhuzhu
age: 24
sex: 女
likes:
- book
- movie
- run
- com.mashibing.entity.Person.java内容
package com.mashibing.entity;
import java.util.List;
public class Person {
private String name;
private Integer age;
private String sex;
private List<String> likes;
public Person() {
}
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setSex(String sex) {
this.sex = sex;
}
public void setLikes(List<String> likes) {
this.likes = likes;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
public String getSex() {
return sex;
}
public List<String> getLikes() {
return likes;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex='" + sex + '\'' +
", likes=" + likes +
'}';
}
}
- 把yaml里的值赋值给Person.java类的属性
在Person类前面加注解:
@ConfigurationProperties(prefix = “person”)
@Component
package com.mashibing.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
// 省略,同2
}
测试是否赋值成功:
在文件test\java\com\mashibing\SpringbootStudyApplicationTests.java中测试
package com.mashibing;
import com.mashibing.entity.Person;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class SpringbootStudyApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
输出:
// 省略没用的部分
Person{name='zhuzhu', age=24, sex='女', likes=[book, movie, run]}
赋值成功。
4. 自定义注解
package com.mashibing.annotation;
import java.lang.annotation.*;
@Target({ElementType.FIELD,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
String name() default "zhangsan";
}
class Test{
@MyAnnotation(name="hehe")
private String name;
@MyAnnotation
public void hehe(){
}
}