springboot-shiro chapter01——创建springboot webmvc环境

简介:本章提供了springboot简单例子,主要包含以下内容

1.pom.xml依赖资源

2.springboot配置

3.web应用spring mvc

环境:

IDEA15+

JDK1.8+

Maven3+

代码:

https://git.oschina.net/xinshu/springboot-shiro

一、pom.xml依赖资源

由于功能相对简单,这里引用的第三方资源文件相对较少

spring-boot-starter:约定大于配置,目的解放劳动力.

spring-boot-starter-tomcat: 主要内嵌servlet容器(tomcat)

spring-webmvc: spring核心jar及mvc功能

具体pom.xml配置如下:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring-webmvc.version}</version>
        </dependency>
    </dependencies>

由于dependency具有依赖传递性,整个依赖树如下:

springboot-shiro-chapter01

另外,在执行的过程中需要依赖于spring-boot-maven-plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-maven-plugin.version}</version>
            </plugin>
        </plugins>
    </build>

pom.xml相关的配置基本上就这么多

二、springboot配置

springboot基于“约定大于配置”的思想,同时提供了个性化定制的方案或扩展的入口,application.properties, 这里可以用属性键值对的方式进行定制,比如:

logging.level.: DEBUG #日志默认level: INFO

server.port: 8001 #tomcat 访问端口设置为8001, 默认8080

springboot执行入口:Chapter01Application.java

@SpringBootApplication
public class Chapter01Application {

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

非常简单,与一般java类没有太大的区别,多了一个注解@SpringBootApplication, 另外提供了main方法,至此一个简单springboot环境搭建好了

三、web应用springboot

传统配置springmvc 时需要自定义Servlet指定DispatcherServlet,配置映射规则url-pattern。这里由于使用了springboot,我们不需要进行额外的配置。直接创建Controller即可

@Controller
public class Chapter01Controller {

    @Autowired
    private HelloService helloService;

    @RequestMapping("/")
    @ResponseBody
    public String helloWord(){
        return helloService.getHelloMessage();
    }
}

当我们访问localhost:1010/时,会执行helloWord方法

启动工程,在IDEA环境下会检测Springboot环境,自动创建了Run/Debug Configurations。这里会指定Main class: com.shujushow.chapter01.Chapter01Application。 我们只需要Shift+F10即可执行,启动完成后,我们可以访问localhost:8001

image

原文地址:https://www.cnblogs.com/i-bugs/p/5342164.html