Spring Boot 1.4.3搭建

Posted 自在时刻

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Boot 1.4.3搭建相关的知识,希望对你有一定的参考价值。

做项目时候,为了分出一个获取统一token的模块,采用spring boot进行项目构建。

pom.xml文件内容如下:

<?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>com.vi</groupId>
    <artifactId>personal</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!-- 继承父包 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

    <!-- spring-boot的web启动的jar包 -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--jpa的jar包 ,操作数据库的,类似hibernate-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!--mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!--thymeleaf模板jar,是很不错的html数据传递取值,类似jsp的jstl-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>sqljdbc4</artifactId>
            <version>4.0</version>
            <scope>system</scope>
            <systemPath>$project.basedir/src/main/lib/sqljdbc4.jar</systemPath>
        </dependency>
    </dependencies>

    <!--maven的插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>

        <!-- 配置java版本 不配置的话默认父类配置的是1.6-->
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

因为使用了sqlserver数据库,所以添加了sqlserver驱动部分内容。


在resources文件夹下防止配置文件,application.properties:

spring.datasource.platform=mysql
spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
# Advanced configuration...
spring.datasource.max-active=50
spring.datasource.max-idle=6
spring.datasource.min-idle=2
spring.datasource.initial-size=6

#create table
spring.jpa.hibernate.ddl-auto= validate



server.port=8888
server.session-timeout=30
server.tomcat.uri-encoding=UTF-8


spring.thymeleaf.prefix=classpath:templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false

定义的controller内容:

package mk.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by yangyang on 17/1/26.
 */
@RestController
public class WeController 
    @RequestMapping(value = "/")
    public String index() 
        return "welcome";
    



定义application,作为spring boot入口。springboot同样要去指定的目录下扫描controller,service等各种组建来进行依赖注入,这部分的配置可以在入口通过componetScan指定扫描包名,不然无法找到相应的bean。运行Application的main方法,注意看到我们的 weController 是否成功启动。

package mk.boot;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

/**
 * Created by yangyang on 17/1/26.
 */

@SpringBootApplication
@ComponentScan(value = "mk.controller")
public class Application 
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

    @Bean
    public CommandLineRunner commandLineRunner(final ApplicationContext ctx) 
        return new CommandLineRunner() 
            @Override
            public void run(String... strings) throws Exception 
                System.out.println("Let's inspect the beans provided by Spring Boot:");
                String[] beanNames = ctx.getBeanDefinitionNames();
                Arrays.sort(beanNames);
                for (String beanName : beanNames) 
                    System.out.println(beanName);
                
            
        ;

    

直接访问localhost:8888 即可看到我们的welcome页面。

以上通过spring boot,相当于整合了spring和spring mvc构建了一个基本的框架,但是数据访问层还没有进行整合,可以采用mybatis等。

以上是关于Spring Boot 1.4.3搭建的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot快速搭建Spring框架

Spring-Boot快速搭建web项目详细总结

Spring Boot系列之环境搭建

用 Spring boot 简单搭建一个微服务项目

搭建基于 docker 的 Kafka 集群及 Spring Boot 应用访问

[spring boot] 01 环境搭建 - 配置java和mvn环境