spring-boot 使用

  spring-boot不愧是框架之王,我们在使用ssh或者ssm的时候,要配置一大堆的文件,但是使用spring-boot后简直就爽翻了!!!

看一下spring-boot下简短的配置文件:

<?xml version="1.0" encoding="UTF-8"?>

<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>cn.czy</groupId>
  <artifactId>netClick</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>
  
  
  <name>netClick</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version><!-- 指定各种版本 -->
</parent>


  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!-- 设置编码 -->
  </properties>

  <dependencies>
	<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId><!-- 自动引入tomcat和spring-mvc等相关包 -->
    </dependency>
    
    <dependency>
	      <groupId>org.springframework.boot</groupId>
	      <artifactId>spring-boot-starter-thymeleaf</artifactId>
	</dependency>

  </dependencies>
</project>

看下controller

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
public class DemoController {
    
        @RequestMapping("/")
        @ResponseBody
        String home() {
            return "Hello World!";
        }
      
}

spring boot项目的启动入口mian方法:注意 这个方法需要放在cn.czy(也就是名字与group id相同)包下


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

说说springBootApplication这个注解  ,这是一个符合注解   

SpringBootApplication=Configuration+EnableAutoConfiguration+ComponentScan+......

整合了最重要的3个注解:

Configuration----该类由spring容器管理
EnableAutoConfiguration----用于启动服务的自动配置功能
ComponnentScan----用于扫描类的注解

启动main类,访问web

就是这么快..............

扫描二维码关注公众号,回复: 2751934 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_39512671/article/details/81269933