Spring Maven项目集成Springboot

Maven管理的Spring项目,准备集成Springboot做接口

1、Springboot对Spring有版本要求

   我用的Springboot版本:1.4.5.RELEASE,对应Spring的版本为:4.3.7.RELEASE

2、项目的pom文件添加springboot引用:

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

3、nspringboot模块的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>
  <parent>
    <artifactId>nroot</artifactId>
    <groupId>com.nankang.cati</groupId>
    <version>${myapp.version}</version>
  </parent>
  <artifactId>nspringboot</artifactId>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>com.nankang.cati</groupId>
      <artifactId>nutil</artifactId>
      <version>${myapp.version}</version>
    </dependency>
    <!-- springboot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <!-- mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
    </dependency>
    <!-- mysql driver -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <scope>runtime</scope>
    </dependency>
    <!-- oracle driver -->
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <scope>runtime</scope>
    </dependency>

    <!-- junit -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
    <!-- druid db pool -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
    </dependency>

  </dependencies>
  <build>
    <finalName>nspringboot</finalName>
  </build>
</project>

4、启动数据库监听 ApplicationStartListener

package com.nankang.cati.nspringboot.listener;


import com.nankang.cati.nutil.context.RunTimeController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

@Component("ApplicationStartListener")
public class ApplicationStartListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        logger.info("Application Starting...");
        try {
            RunTimeController.start();
        } catch (Throwable e) {
            throw new RuntimeException("Application failed to startup.", e);
        }
        logger.info("Application Started [OK].");
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        Logger logger = LoggerFactory.getLogger(this.getClass());
        logger.info("Application Stopping...");
        try {
            RunTimeController.close();
        } catch (Throwable e) {
            throw new RuntimeException("Application failed to stop.", e);
        }
        logger.info("Application Stopped [OK].");
    }
}

5、程序启动文件 Application:

package com.nankang.cati.nspringboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

}

6、其他注意点:

   1)Maven模块灰色显示:删除,重新导入一次即可

 http://blog.csdn.net/Justnow_/article/details/52461197   

   2)springboot启动不了

   原因,发布的不能是自己加进去的,用生成的就好了

 3)Springboot启动不了,Spring版本问题:

   原来版本:3.1.4 升级到 4.3.7 即可。

7、参考文章:

模块中添加Springboot:

Springboot配置:
 
8、模块下载:下载
  
 
原文地址:https://www.cnblogs.com/sshoub/p/6622846.html