Spring-boot 学习笔记(1)

spring-boot 开发的目的是用来简化 spring 的配置和开发环境。spring-boot 通过使用注解来替代了 spring-io 中大量繁琐的 xml 文件配置。使基于 spring 开发的变得相对来说效率更高了。

依照官方文档的说明,spring boot 入门非常简洁。使用如下配置即可运行一个简单的 spring web application。前提是使用 Maven 构建项目。并且运行环境要求如下:

1 . jdk 1.6 ++
2 . tomcat 8.0.23 ++
3 . springframework 4.1.3 ++

denpendency 如下:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
</parent>

<dependencies>
    <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>
        </plugin>
    </plugins>
</build>

代码如下:

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
@ComponentScan
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

这是一个基于 rest 风格的 web application 。类 Example 的注解:
@RestController:作用相当于 spring-mvc 中的 @Controller 注解。作为一个 控制器。
@EnableAutoConfiguration:从字面意思理解就是 启用自动配置。
@ComponentScan:作用相当于 spring-mvc 中的 。启用的扫描拥有 @Component 注解的 JavaBean,注册在 Spring 的上下文中。

如果你仍然习惯于使用 xml 配置 spring 上下文环境或者不得不使用 xml 进行 spring 上下文的配置。可以使用 @ImportResource 导入 xml 配置文件。官方文档说明如下:

15.1 Importing additional configuration classes
You don’t need to put all your @Configuration into a single class. The @Import annotation can be used to import additional configuration classes. Alternatively, you can use @ComponentScan to automatically pickup all Spring components, including @Configuration classes.

15.2 Importing XML configuration
If you absolutely must use XML based configuration, we recommend that you still start with a @Configuration class. You can then use an additional @ImportResource annotation to load XML configuration files.

接着是 @RequestMapping(“/”):这个应该比较熟悉,spring-mvc 中的常用注解,表示标注了该注解的类或者方法将处理 上下文跟路径下的请求。

在 main 主方法中,

SpringApplication.run(Example.class, args);

官方文档解释如下:

11.3.3 The “main” method
The final part of our application is the main method. This is just a standard method that follows the Java convention for an application entry point. Our main method delegates to Spring Boot’s SpringApplication class by calling run. SpringApplication will bootstrap our application, starting Spring which will in turn start the auto-configured Tomcat web server. We need to pass Example.class as an argument to the run method to tell SpringApplication which is the primary Spring component. The args array is also passed through to expose any command-line arguments.

意思是说,应用程序启动时,SpringApplication.run() 方法将会自动运行在 tomcat 服务器中,默认端口号为 8080 。类似于往数据库中写入数据时,自动生成主键这一功能(这比喻很形象吗?)。

如此一来就完成了一个 基于 spring-boot 的 rest 风格的 web application 。最后进入到 命令行 只需要进入到项目所在目录下运行

$ mvn package
$ java -jar target/你打包所生成的jar包名.jar

即可访问程序。
打开你的浏览器 或 命令行 ,输入 http://localhost:8080,就可以看到 “ Hello World ”了。
更多参考可以关注 spring boot 的 quick start < – 戳这里传送

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值