目錄java
編寫M2_HOME/conf/setting.xmlweb
1. 指定本地maven倉庫 <localRepository>D:\APP\repository</localRepository> 2. 配置遠程倉庫鏡像(以阿里云爲例) <mirror> <id>alimaven</id> <name>aliyun maven</name> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> <mirrorOf>central</mirrorOf> </mirror> 3. 配置全局jdk版本 <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>
1. web項目基礎依賴 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
啓動類須要與service、controller、dao等包同級,便於後面自動掃描spring
1.啓動類apache
@SpringBootApplication public class MySpringBootApplication { public static void main(String[] args) { SpringApplication.run(MySpringBootApplication.class,args); } }
2.接口類
當@ResponseBody加在方法上表示該方法返回的對象之間返回給瀏覽器,若是加載在類上,表示當前類下的全部方法都把返回對象之間返回給瀏覽器
@RestController == @Controller + @ResponseBody數組
@Controller public class Helloword { @RequestMapping("/hello") @ResponseBody public String hello() { return "Hello World"; } }
1.配置pom.xml文件,默認打成jar包 <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
點擊idea左下角的圖標,點擊maven projects => spring-boot-helloword =>
lifecycle => package瀏覽器
取target文件夾下的spring-boot-helloword-1.0-SNAPSHOT.jar,
運行java -jar spring-boot-helloword-1.0-SNAPSHOT.jarspringboot
1.使用IDE生成
2.使用spring官網地址:https://start.spring.io服務器
對象、map
```yml
#1.普通寫法
student:
name: song
age: 1app
數組list、set
yml #1.普通寫法 pets: - cat - dog - pig #2.行內寫法 pets: {cat,dog,pig}
dom
咱們本身編寫的xxx.xml配置文件,不會被spring和springboot識別,可使用如下方法把xxx.xml中的bean引入到spring容器中
#application.properties #激活dev環境的配置 spring.profiles.active=dev #application-dev.properties server.port=8081 #application-prd.properties server.port=8082
#application.yml #激活dev環境的配置 server port: 8080 spring profiles active: dev --- server port: 8081 spring profiles: dev --- server port: 8082 spring profiles: prd
1.虛擬機指定 VM options: -Dspring.profiles.active=dev 2.java命令指定 java -jar xxx.jar --spring.profiles.active=dev
file類型 ./config/ ./ classpath類型 ./config/ ./
優先級由高到低:
==用好springboot配置文件的精髓==
在spring.factories中以 org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,/ 爲例:
@Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class}) @ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) @AutoConfigureOrder(-2147483638) @AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class}) public class WebMvcAutoConfiguration { ... }
@AutoConfigureOrder(-2147483648) @Configuration @ConditionalOnWebApplication(type = Type.SERVLET) @ConditionalOnClass({DispatcherServlet.class}) @AutoConfigureAfter({ServletWebServerFactoryAutoConfiguration.class}) public class DispatcherServletAutoConfiguration { ... }
@Configuration @AutoConfigureOrder(-2147483648) @ConditionalOnClass({ServletRequest.class}) @ConditionalOnWebApplication(type = Type.SERVLET) @EnableConfigurationProperties({ServerProperties.class}) @Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class}) public class ServletWebServerFactoryAutoConfiguration { ... }
@ConfigurationProperties( prefix = "server", ignoreUnknownFields = true ) public class ServerProperties { private Integer port; private InetAddress address; public Integer getPort() { return this.port; } public void setPort(Integer port) { this.port = port; } public InetAddress getAddress() { return this.address; } public void setAddress(InetAddress address) { this.address = address; } //... }
控制檯判斷哪些自動配置類生效
application.properties中配置:debug=true
總結:經過ServerProperties.class中的屬性能夠設置application.properties中的一些關於服務器的配置文件屬性,例如:server.port,一樣根據其餘的xxx.properties也能夠學習配置springboot的配置文件