springboot学习(一)

来源:互联网 发布:家具淘宝店名大全 编辑:程序博客网 时间:2024/05/29 03:14

    公司最近要进行一个新的项目,之前因为比较懒,所以也没有了解过springboot。现在来个临时抱佛脚。并做下记录。

资料:

官网地址:http://projects.spring.io/spring-boot/

先参考官方文档来个Quick Start:

本人开发环境:apache-maven-3.5.0

    eclipse-jee-luna-SR2-win32-x86_64

    jdk1.8.0_131

采用maven方式构建项目:

  1、在pom文件中添加依赖:

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.6.RELEASE</version></parent><dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency></dependencies>
问题:项目报错,需要maven==》update project

           报错:Java compiler level does not match the version of the installed Java project facet.

                       Dynamic Web Module 3.1 requires Java 1.7 or newer.

                       One or more constraints have not been satisfied.

           原因:jdk默认版本是1.6,换成1.8即可。

官方建议:尽管你可以在Java6或Java7环境下使用Spring Boot,通常建议尽可能使用Java8。

这里采用maven插件的方式。

 <build>
     <plugins>
<!--      jdk版本 -->
       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <compilerArguments>
                        <verbose />
                        <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
        </plugins>
   </build>


更正:通过上面的方法可以达到目的,但是好像直接添加

<properties>
<java.version>1.8</java.version>
</properties>
即可覆盖。看来我的maven实在不怎么样。有空的学习。。。。。。

    2.启动类。

package hello;import org.springframework.boot.*;import org.springframework.boot.autoconfigure.*;import org.springframework.stereotype.*;import org.springframework.web.bind.annotation.*;//@RestController  @RestController注解相当于@ResponseBody+@Controller合在一起@Controller@EnableAutoConfigurationpublic class SampleController {    @RequestMapping("/")    @ResponseBody    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(SampleController.class, args);    }}
OK。按照官方的Quick Start springboot的入门就这样了。现在直接用main启动。ulr:http://localhost:8080/就可以访问到了。

@EnableAutoConfiguration ,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。由于 spring-boot-starter-web 添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用,并对Spring进行相应地设置。


至此结束,不足之处欢迎指出。Thanks。。。。


原创粉丝点击