Spring Boot 的神奇的不是借助于代码生成来实现的,而是通过调节注解来实现的,这是Spring4.x提供的新特性,Spring4.x提倡使用Java配置和注解配置组合,而Spring Boot不需要任何xml配置即可实现Spring的所有配置。
一下一些优点:
①快速构建项目;
②对主流开发框架的无配置集成;
③项目可独立运行,无需外部依赖Servlet容器;
④提供运行时的应用监控;
⑤极大地提高了开发、部署效率;
⑥与云计算的天然集成。
使用开发工具IDEA
创建一个SpringBoot项目
①
②
③选择相关依赖
等项目创建完成后,在自己包下创建一个HelloSpringBoot类。
@EnableAutoConfiguration
@RestController
@RequestMapping("/first_step")
public class HelloSpringBoot {
@GetMapping("/hello") //get请求与 @RequestMapping的结合
public String helloBoot() {
return "hello spring boot";
}
public static void main(String[] args) {
SpringApplication.run(HelloSpringBoot.class,args);
}
}
这样,一个简单的SpringBoot 之hello程序就完成了,是不是超简单,再也没有了那些繁琐的配置,因为SpringBoot已经默默地替我们完成了。
SpringBoot支持多种的运行方式,下面一一演示。
接下来运行看看
1.直接运行main函数运行
@SpringBootApplication(scanBasePackages = {"priv.hgs"})
public class BootstudyApplication {
public static void main(String[] args) {
SpringApplication.run(BootstudyApplication.class, args);
}
}
2.在当前项目路径下使用命令行
mvn spring-boot:run
运行成功后再控制台中将会看到如下打印
我们是做用浏览器访问下:http://127.0.0.1:8080/first_step/hello
ok 这就是我们的第一个SpringBoot程序,还记得我们当初学习SpringMVC,进行第一个Hello的艰辛吗?你一定会想那些配置都去哪了,其实这些都由SpringBoot替我们完成。以前的那么多的依赖关系,现在只需两个即可,剩下的都交给SpringBoot
pom中的依赖:
<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>
Spring 提供了一系列的start Pom 来简化Maven的依赖,上述两个start的依赖分别用于web和测试,将会引入所需的所有依赖包,你无需再一个一个的添加依赖。
3.使用jar进行运行
我们在创建Springboot程序时,你有没注意到有个Packaging选项,我选择了jar。你一定会问,怎么使jar,我们以前的项目部署中都是用war包,没关系jar依然可以运行
我们使用maven进行打包下 在项目目录下:
mvn package
之后我们可以在项目目录的G:\IDEAProject\bootstudy\target (以自己具体的项目目录为准)下看到打包好的jar包。bootstudy-0.0.1-SNAPSHOT.jar
运行看看,直接双击运行或者在命令
java -jar xxxx\bootstudy-0.0.1-SNAPSHOT.jar
启动成功后
此时我们再次访问,依然可以得到上面的结果。
4.打包成war文件
可是根据项目的规定,我一定要用war包怎么办,很简单 只需要将maven的pom.xml中的jar改成war,再进行打包即可。
<packaging>war</packaging>
依然在G:\IDEAProject\bootstudy\target目录下的到bootstudy-0.0.1-SNAPSHOT.war包。
当然你也可以在idea的maven快捷入口中进行打包,一样的效果。
有没有瞬间爱上SpringBoot,我们再也不用操心那些繁琐的配置了
有趣小贴士
修改banner,我们在启动Spring Boot时会在控制台中看到以下LOGO打印
如何修改它呢???
在src/main/resources下新建一个banner.txt ,把你想要输出的内容,放在这即可。
如以下内容
_____________________________________________________
| * * * * * |
| * |
| * |
| * |
| * * * |
| * |
| * |
| * |
| * * * * p r i n g Boot |
| |
_____________________________________________________
运行时即可打印出此logo,你可以试试。
SpringBoot 1.5.7版提供的starter有:
包名 | starter | 版本 |
---|---|---|
org.springframework.boot | spring-boot-starter | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-activemq | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-actuator | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-amqp | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-aop | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-artemis | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-batch | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-cache | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-cloud-connectors | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-cassandra | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-couchbase | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-elasticsearch | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-gemfire | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-jpa | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-ldap | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-mongodb | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-neo4j | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-redis | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-rest | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-data-solr | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-freemarker | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-groovy-templates | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-hateoas | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-integration | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jdbc | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jersey | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jetty | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jooq | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jta-atomikos | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jta-bitronix | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-jta-narayana | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-log4j2 | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-logging | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-mail | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-mobile | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-mustache | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-remote-shell | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-security | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-social-facebook | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-social-linkedin | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-social-twitter | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-test | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-thymeleaf | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-tomcat | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-undertow | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-validation | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-web | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-web-services | 1.5.7.RELEASE |
org.springframework.boot | spring-boot-starter-websocket | 1.5.7.RELEASE |