美文网首页
初学springboot之一 【第一个springboot工程

初学springboot之一 【第一个springboot工程

作者: HoooooG | 来源:发表于2017-12-18 15:39 被阅读0次

1.建立springboot的第一个工程使用环境为:Tomcat8、jdk1.8、myeclispe2016、maven3.5.2。
首先建立一个maven工程,我的工程包为:com.lhkj,工程名为:pluto。建立好工程之后修改jar包:jdk1.8,tomcat8。修改编码为utf-8。


2.修改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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  
  <groupId>com.lhkj</groupId>
  <artifactId>pluto</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>pluto</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

     <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.0.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <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>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!-- 不打包的时候用,打包的时候注释 -->
            <scope>compile</scope> 
            <!-- 打包的时候用,不打包的时候注释   --> 
            <!--<scope>provided</scope>-->    
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>


3.修改App.java文件

package com.lhkj.pluto;

import java.util.concurrent.TimeUnit;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lhkj.pluto.file.controller.FileController;
import com.lhkj.pluto.user.controller.UserController;



/*
 * 发现@SpringBootApplication是一个复合注解,包括@ComponentScan,和@SpringBootConfiguration,@EnableAutoConfiguration
 * 
 */

@RestController
@SpringBootApplication
public class App 
{   
    
    @RequestMapping(value="/hello")
    public String Hello(){
        return "hello";
    }
    
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
    
    
    @Bean
    public EmbeddedServletContainerFactory servletFactory(){
        TomcatEmbeddedServletContainerFactory tomcatFactory = 
                new TomcatEmbeddedServletContainerFactory();
        //tomcatFactory.setPort(8011);
        tomcatFactory.setSessionTimeout(10,TimeUnit.SECONDS);
        return tomcatFactory;
        
    }
}

特别注意:
*@RestController和@SpringBootApplication这两个注解一定要同时存在。
运行main()方法,在浏览器中输入:localhost:8080/hello即可看到结果。

相关文章

网友评论

      本文标题:初学springboot之一 【第一个springboot工程

      本文链接:https://www.haomeiwen.com/subject/hkonixtx.html