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、模塊下載:
下載