SpringBoot简介和搭建

SpringBoot就是为了简化Spring应用的初始搭建和配置的框架,SpringBoot不须要用配置文件,经过注解就能够快速的搭建Spring应用,应用能够是Web 应用,也能够是通常的 Spring 应用。Spring+Web搭建Web应用,Spring+Netty搭建Socket应用。

Spring Boot的主要目标是:
    1. 为全部的Spring开发提供一个从根本上更快的和普遍使用的入门经验。
    2. 开箱即用, 但你能够经过不采用默认设置来摆脱这种方式。
    3. 提供一系列大型项目经常使用的非功能性特征( 好比, 内嵌服务器, 安全, 指标, 健康检测) 。
    4. 绝对不须要代码生成及XML配置。

项目github地址:https://github.com/AaronSheng/SpringBoot

建立Spring Boot Web服务器:
    1. Maven项目中引入Spring Boot:
        1)在pom.xml依赖spring-boot-starter-webjava

<!—依赖parent,paren会自动选择相应版本对象的包 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>


        2) 在pom.xml中添加Spring Boot的Maven插件git

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
​​​​​​</build>


    2. 建立application.properties配置文件github

spring.profiles.include=rds,redis
	
logging.config=classpath:log4j2.xml
	
# Tomcat或Jetty配置
server.port=8080
server.session-timeout=60
server.error.path=/error


    3. 创建一个启动类:web

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    public Application() {
        super();
        setRegisterErrorPageFilter(false);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}


    4. 建立Controller:redis

@Controller
@RequestMapping("/index")
public class HelloController {
    @Autowired
    private HelloService helloService;

    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public JSONObject get() {
        JSONObject json = new JSONObject();
        json.put("id", "1");
        json.put("name", "hello");
        return json;
    }
​​​​​​​}


运行和打包:
    运行:
    
     打包:
    
         jar包:
         pom.xml:spring

<packaging>jar</packaging>

        
        war包:

        pom.xml:json

<packaging>war</packaging>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>

​​​​

相关文章
相关标签/搜索