1、IDEA:使用 Spring Initializer快速创建项目
IDE都支持使用Spring的项目创建向导快速创建一个Spring Boot项目;
选择我们需要的模块;向导会联网创建Spring Boot项目;
默认生成的Spring Boot项目;
主程序已经生成好了,我们只需要我们自己的逻辑
resources文件夹中目录结构
static:保存所有的静态资源; js css images;
templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;
2.项目结构
3.代码组织
- pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.szs</groupId>
<artifactId>spring-boot-02-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-02-config</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project> - Person javaBean
package com.szs.springboot.bean;
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; /**将配置文件中配置的每一个属性的值,映射到这个组件中;
* @ConfigurationProperties : 告诉springBoot将本类中的所有属性和
* 配置文件中相关的配置进行绑定;
* prefix = "person": 配置文件中哪个下面的所有属性进行一一映射;
*
* 只有这个组件是容器中的组件, 才能使用提供容器的ConfigurationProperties功能;
*/
//@PropertySource(value = {"classpath:person.properties"})
@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
/**
* <bean class=""Person>
* <property name="lastName" value="字面量/${key}从环境变量和配置文件中获取值/#{spel表达式}" ></property>
*
* </bean>
*/
@Value("${person.last-name}")
private String lastName;
@Value("#{11*2}")
private Integer age;
@Value("true")
private Boolean boss;
private Date birth; private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public Person() {
} @Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", 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> 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;
}
} - 使用application.properties 这个文件来进行person的初值的赋值!
#server.port=8081
#idea配置的properties文件是utf-8 ,需要进行修改默认properties文件的gbk
person.last-name=阿尔法
person.age=666
person.birth=2017/12/23
person.boss=true
person.dog.name=小花
person.dog.age=1 person.lists=a,b,c
person.maps.k1=v1
person.maps.k2=v222223 - HelloController 这个@RestController使用spEL:@Value("#{person.toString()}") 取值,然后设置@RequestMapping("/sayHello")来进行跳转.
package com.szs.springboot.controller; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
public class HelloController {
@Value("#{person.toString()}")
private String info;
@RequestMapping("/sayHello")
public String method1(){
return "hello "+info;
}
} - 设置@SpringBootApplication 创建Springboot的Main类启动类
package com.szs.springboot; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringBoot02ConfigApplication { public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
} }
4.运行结果
"C:\Program Files\Java\jdk1.8.0_181\bin\java" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=63283 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:D:\IntelliJ IDEA 2018.1\lib\idea_rt.jar=63284:D:\IntelliJ IDEA 2018.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_181\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\DevInstall\WorkSpace_20190111\spring-boot-02-config\target\classes;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-starter-web\2.1.2.RELEASE\spring-boot-starter-web-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-starter\2.1.2.RELEASE\spring-boot-starter-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot\2.1.2.RELEASE\spring-boot-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-autoconfigure\2.1.2.RELEASE\spring-boot-autoconfigure-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-starter-logging\2.1.2.RELEASE\spring-boot-starter-logging-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\DevInstall\RepMaven\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\DevInstall\RepMaven\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\DevInstall\RepMaven\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\DevInstall\RepMaven\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\DevInstall\RepMaven\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\DevInstall\RepMaven\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-starter-json\2.1.2.RELEASE\spring-boot-starter-json-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\core\jackson-databind\2.9.8\jackson-databind-2.9.8.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\core\jackson-core\2.9.8\jackson-core-2.9.8.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.8\jackson-datatype-jdk8-2.9.8.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.8\jackson-datatype-jsr310-2.9.8.jar;D:\DevInstall\RepMaven\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.8\jackson-module-parameter-names-2.9.8.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-starter-tomcat\2.1.2.RELEASE\spring-boot-starter-tomcat-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\apache\tomcat\embed\tomcat-embed-core\9.0.14\tomcat-embed-core-9.0.14.jar;D:\DevInstall\RepMaven\org\apache\tomcat\embed\tomcat-embed-el\9.0.14\tomcat-embed-el-9.0.14.jar;D:\DevInstall\RepMaven\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.14\tomcat-embed-websocket-9.0.14.jar;D:\DevInstall\RepMaven\org\hibernate\validator\hibernate-validator\6.0.14.Final\hibernate-validator-6.0.14.Final.jar;D:\DevInstall\RepMaven\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\DevInstall\RepMaven\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\DevInstall\RepMaven\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\DevInstall\RepMaven\org\springframework\spring-web\5.1.4.RELEASE\spring-web-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-beans\5.1.4.RELEASE\spring-beans-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-webmvc\5.1.4.RELEASE\spring-webmvc-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-aop\5.1.4.RELEASE\spring-aop-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-context\5.1.4.RELEASE\spring-context-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-expression\5.1.4.RELEASE\spring-expression-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\boot\spring-boot-configuration-processor\2.1.2.RELEASE\spring-boot-configuration-processor-2.1.2.RELEASE.jar;D:\DevInstall\RepMaven\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\DevInstall\RepMaven\org\springframework\spring-core\5.1.4.RELEASE\spring-core-5.1.4.RELEASE.jar;D:\DevInstall\RepMaven\org\springframework\spring-jcl\5.1.4.RELEASE\spring-jcl-5.1.4.RELEASE.jar" com.szs.springboot.SpringBoot02ConfigApplication . ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.2.RELEASE) 2019-01-22 14:03:53.740 INFO 8588 --- [ main] c.s.s.SpringBoot02ConfigApplication : Starting SpringBoot02ConfigApplication on DESKTOP-IHFHKDB with PID 8588 (D:\DevInstall\WorkSpace_20190111\spring-boot-02-config\target\classes started by dell in D:\DevInstall\WorkSpace_20190111\spring-boot-02-config)
2019-01-22 14:03:53.742 INFO 8588 --- [ main] c.s.s.SpringBoot02ConfigApplication : No active profile set, falling back to default profiles: default
2019-01-22 14:03:54.402 INFO 8588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-01-22 14:03:54.415 INFO 8588 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-01-22 14:03:54.415 INFO 8588 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-22 14:03:54.419 INFO 8588 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.18] of the APR based Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.19]
2019-01-22 14:03:54.419 INFO 8588 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded APR based Apache Tomcat Native library [1.2.18] using APR version [1.6.5].
2019-01-22 14:03:54.420 INFO 8588 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2019-01-22 14:03:54.420 INFO 8588 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
2019-01-22 14:03:54.422 INFO 8588 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1 11 Sep 2018]
2019-01-22 14:03:54.493 INFO 8588 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-01-22 14:03:54.493 INFO 8588 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 719 ms
2019-01-22 14:03:54.638 INFO 8588 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-22 14:03:54.768 INFO 8588 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-01-22 14:03:54.770 INFO 8588 --- [ main] c.s.s.SpringBoot02ConfigApplication : Started SpringBoot02ConfigApplication in 1.305 seconds (JVM running for 1.931)
2019-01-22 14:04:02.740 INFO 8588 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-01-22 14:04:02.740 INFO 8588 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2019-01-22 14:04:02.743 INFO 8588 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 3 ms