Myeclipse下使用Maven搭建spring boot项目(第二篇)
上一篇,已经使用Maven搭建了一个WEB项目
http://blog.csdn.net/a78270528/article/details/77532781
二、需要在pom.xml中引入spring-boot-starter-web,spring官方解释spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。
三、如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话就不需要此配置。
四、下面开始写程序,我们需要一个启动类,然后在启动类声明让spring boot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问hello world字样的例子:
其中@SpringBootApplication声明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。
@RestController返回json字符串的数据,直接可以编写RESTFul的接口。
五、运行我们的Application,我们先介绍第一种运行方式。第一种方式特别简单:右键Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/home 就可以看到Hello world!了。
第二种方式右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。
六、运行报错
启动时报错NoClassDefFoundError: org/apache/juli/logging/LogFactory
参见下面的解决方法:
http://blog.csdn.net/a78270528/article/details/77548779
至些,我们的Spring boot项目已经搭建完成,并成功的运行了HelloWorld的项目。
http://blog.csdn.net/a78270528/article/details/77532781
现在需要搭建spring boot框架,并实现一个HelloWorld的项目,让程序真正运行起来。
一、在pom.xml中引入spring-boot-start-parent,spring官方的叫stater poms,它可以提供dependency management,也就是依赖管理,引入以后在声明其它dependency的时候就不需要version了。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent>
二、需要在pom.xml中引入spring-boot-starter-web,spring官方解释spring-boot-start-web包含了spring webmvc和tomcat等web开发的特性。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
三、如果我们要直接Main启动spring,那么以下plugin必须要添加,否则是无法启动的。如果使用maven的spring-boot:run的话就不需要此配置。
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build>
四、下面开始写程序,我们需要一个启动类,然后在启动类声明让spring boot自动给我们配置spring需要的配置,比如:@SpringBootApplication,为了可以尽快让程序跑起来,我们简单写一个通过浏览器访问hello world字样的例子:
@RestController @SpringBootApplication public class ExampleSpringBoot { @RequestMapping("/home") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(ExampleSpringBoot.class, args); } }
其中@SpringBootApplication声明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan。
@RestController返回json字符串的数据,直接可以编写RESTFul的接口。
五、运行我们的Application,我们先介绍第一种运行方式。第一种方式特别简单:右键Run As -> Java Application。之后打开浏览器输入地址:http://127.0.0.1:8080/home 就可以看到Hello world!了。
第二种方式右键project – Run as – Maven build – 在Goals里输入spring-boot:run ,然后Apply,最后点击Run。
六、运行报错
启动时报错NoClassDefFoundError: org/apache/juli/logging/LogFactory
参见下面的解决方法:
http://blog.csdn.net/a78270528/article/details/77548779
七、完整的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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.bocom</groupId> <artifactId>maventest</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <!-- 继承父包 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> <relativePath></relativePath> </parent> <name>maventest</name> <url>http://maven.apache.org</url> <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><!--$NO-MVN-MAN-VER$--> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-logging-juli</artifactId> <version>8.0.23</version> </dependency> </dependencies> <build> <finalName>maventest</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin </artifactId> </plugin> </plugins> </build> </project>
至些,我们的Spring boot项目已经搭建完成,并成功的运行了HelloWorld的项目。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: Nginx:承受3万并发连接数,胜过Apache 10倍
- 下一篇: Nginx查看并发链接数