极速切入Spring Boot微服务框架
前言</span></span> pom.xml 文件非常简单,主要是需要添加一个parent的节点来配置改pom继承spring-boot-starter-parent的配置,并且添加一个构建 web需要的依赖spring-boot-starter-web,其次就是maven打包需要的插件spring-boot-maven-plugin 了。最下面是配置的maven源,官方文档说非snapshot版本的Spring Boot是不需要该配置的,我没有试。 接下来创建一个App.java类,这个类是Spring Boot启动类:
$ brew tap pivotal/tap $ brew install springboot
@RestController class ThisWillActuallyRun{ @RequestMapping("/") String home(){ "Hello World" } }
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.2.5.RELEASE)
<?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> <groupId>com.floatlu.lab</groupId> <artifactId>spriboot</artifactId> <version>1.0-SNAPSHOT</version> <!--继承spring-boot-starter-parent的maven配置--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.3.RELEASE</version> </parent> <properties> <spring.version>4.1.6.RELEASE</spring.version> </properties> <dependencies> <!--创建web需要的依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!--打包需要的插件--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.2.3.RELEASE</version> </plugin> </plugins> </build> <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>
@SpringBootApplication @ComponentScan(value = "com.spriboot.controller") public class App { public static void main(String ...s){ SpringApplication.run(App.class, s); } }
其中ComponentScan注解配置Spring扫描组件的路径,这个和通常的Spring用法一致,在Controller包里面我创建了一个Example类并且打上@RestControllrt注解: @RestController public class Example { @RequestMapping("/home") public String home(){ return "homepage"; } }
在项目下面执行mvn package来把项目打包成jar:
来自:http://jvmplus.duapp.com/