搭建自己的第一个SpringBoot框架(1) 小咪咪 2021-07-09 13:19 282阅读 0赞 今天用空在学习SpringBoot框架,对照着资料在自己电脑上搭建第一个hellow 首先第一步新建一个maven项目,本人使用的是Myeclipse,右键new-->other-->MavenProject![20180122094447247][] 选择下一步, ![20180122094732238][] 新建Maven项目后的结构是: ![20180122094917413][] 这里要注意下,SpringBoot只支持JDK1.7以上。之后再来配置pm.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</groupId> <artifactId>FirstSpringBoot</artifactId> <version>0.0.1-SNAPSHOT</version> <build/> <!-- 引入继承SpringBoot,实现jar的依赖管理 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> <relativePath/> </parent> <dependencies> <!-- 引用web组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> 到这里可能项目会有个小×, ![20180122095543607][] 没事,把本项目update一下就好了。![20180122095807478][] 之后新建一个mian方法。 ![20180122100019542][] 主函数的程序如下: package test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class TestSpringBoot { @RequestMapping("/hellow") public String hello(){ return "success"; } public static void main(String[] args) { //传入函数开始运行 SpringApplication.run(TestSpringBoot.class, args); } } 最后直接右键运行; ![20180122100545482][] 启动成功后,控制台,我们直接打开浏览器,输入:http://localhost:8080/hellow 回车直接运行: ![20180122100755903][]成功。 补充:当运行两个的时候,先后启动就会报错,因为一次只能运行一个,不然会显示端口号被占用。接下来就要先一个用来公共管理的主程序。 ![SouthEast][] 在App.class中 package app; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; //有多个项目的时候,要加入这个注解,用来扫描包 @ComponentScan(basePackages="package testcontroller") //只能使用者一个注解 @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } } 之前的两个主程序全部都注释掉: ![SouthEast 1][] 2、 ![SouthEast 2][] 之后运行App中的主程序即可。 [20180122094447247]: /images/20210708/15c11e40f06c49a4b06b6fff0e64d363.png [20180122094732238]: /images/20210708/42d06ceb7e58461d87ac39f189384ab2.png [20180122094917413]: /images/20210708/87100a939dfe404698891febdf07274d.png [20180122095543607]: /images/20210708/4e33233a299e4f01b9251812d6a81f7a.png [20180122095807478]: /images/20210708/4370f624c21244eeacb84750ac734e8f.png [20180122100019542]: /images/20210708/32f16400b46045d5bc3bac897590609a.png [20180122100545482]: /images/20210708/0e7747b2cc5d492b97ef53dbb1a3090f.png [20180122100755903]: /images/20210708/867690b6207d4e62a433c8a067604bce.png [SouthEast]: /images/20210708/3e244788cb3a4e0fa26a135cc7f7ef93.png [SouthEast 1]: /images/20210708/e324b18eed66406e974f9cf208816a7f.png [SouthEast 2]: /images/20210708/189b9e37dcc24059a3b08158eb9e430e.png
还没有评论,来说两句吧...