目录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的配置文件