Srping boot的介绍 百度。。
开始:
Pom.xml文件:
<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>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<!-- Package as an executable jar -->
<build>
<plugins>
<!-- <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin> -->
<!-- Maven AssemblyPlugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.lgp.SpringBoot.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories -->
<!-- (you don't need this if you are using a .RELEASE version) -->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
测试代码:DemoApplication.java:
@RestController
@EnableAutoConfiguration
public class DemoApplication {
/**
* 访问 http://localhost:8080/w 不需要加项目名称
* @return
*/
@RequestMapping("/h")
public String home() {
return "Hello";
}
/**
*
* @param id访问 http://localhost:8080/w/88889 参数8889
* @return
*/
@RequestMapping("/w/{id}")
public String word(@PathVariable String id) {
return "World"+id;
}
public static void main( String[] args )
{
System.out.println( "Hello World !!" );
SpringApplication.run(DemoApplication.class, args);
}
/*@Override
public void customize(ConfigurableEmbeddedServletContainer arg0) {
arg0.setPort(8081);//设置默认端口 需要implements EmbeddedServletContainerCustomizer
}
*/
/**
* @Bean意思是@Bean明确地指示了一种方法,什么方法呢——产生一个bean的方法,
* 并且交给Spring容器管理;从这我们就明白了为啥@Bean是放在方法的注释上了
* ,因为它很明确地告诉被注释的方法,你给我产生一个Bean,然后交给Spring容器
*/
@Bean
public EmbeddedServletContainerFactory servletFactory(){
TomcatEmbeddedServletContainerFactory containerFactory = new TomcatEmbeddedServletContainerFactory();
containerFactory.setPort(8081);
containerFactory.setSessionTimeout(10, TimeUnit.SECONDS);
return containerFactory;
}
}
直接运行main函数即可
如果编译报错请查看jdk版本是否正确。