最近花了两天看spring-boot.简单记录下SpringBoot的多模块示例
项目有如下三个模块
dao数据库操作
service项目逻辑
webweb api,与前端交互
项目右键-> 新建module.项目结构look like this
spring-boot-mult-module项目下配置spring-boot的依赖。可以有两种方式来引入spring-boot
直接添加parent,继承spring-boot的pom依赖
<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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>spring-boot-mult-module</artifactId> <version>1.0</version> <modules> <module>mult-module-dao</module> <module>mult-module-service</module> <module>mult-module-web</module> </modules> <packaging>pom</packaging> <name>spring-boot-mult-module</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </project>如果你项目中有其他的父类,就不能用method-1中的继承了,这样你可以这样来管理spring-boot的依赖
<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>org.example</groupId> <artifactId>spring-boot-mult-module</artifactId> <version>1.0</version> <modules> <module>mult-module-dao</module> <module>mult-module-service</module> <module>mult-module-web</module> </modules> <packaging>pom</packaging> <name>spring-boot-mult-module</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencyManagement> <dependencies> <dependency> <!-- Import dependency management from Spring Boot --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>1.5.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> </project>本例中用的是method-1,完整的pom.xml如下:
<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> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>spring-boot-mult-module</artifactId> <version>1.0</version> <modules> <module>mult-module-dao</module> <module>mult-module-service</module> <module>mult-module-web</module> </modules> <packaging>pom</packaging> <name>spring-boot-mult-module</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <!-- mybatis start --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> <!-- mybatis end --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.6</version> <scope>provided</scope> </dependency> </dependencies> <build> <finalName>mult-module-web</finalName> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.*</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.*</include> </includes> <filtering>false</filtering> </resource> </resources> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>这里需要注意的,spring-boot已经添加了很多依赖的包,如果你想使用不同版本的库,你可以用来移除,然后添加自己版本的库
for example:the version of jackson in spring-boot-1.5.4.RELEASE is
<jackson.version>2.8.8</jackson.version>now, you want to use another version of jackson, you can add the follow dependency in you pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> <exclusions> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </exclusion> <exclusion> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency>reference: https://springframework.guru/jackson-dependency-issue-spring-boot-maven/
dao模块是用来操作数据库的,创建包
config包下新建MyBatisConfig.java
mappermybatis xml文件
entity数据库实体类 User
创建接口
UserDao在main文件夹下创建resources文件夹,并添加文件
db.properties数据库配置
mybatis-config.xmlmybatis配置
now, you dao module's structure may look like this:
then, each file's content are follow:
reference: http://www.jianshu.com/p/69b9fbb97574
finally, the structure of dao module may look like this:
then run all the tests and check the database to see whether or not the insert test is successful.
if occur ==IllegalStateException: No supported DataSource type found== error
make sure the dependency of commons-dbcp, or tomcat-jdbc or hikaricp in your pom.xml file
reference: https://stackoverflow.com/questions/34790924/i-am-getting-datasource-not-supported-when-using-datasoucebuilder
the default port of tomcat server is 8080,
if you want to use another port ,
you can create application.properties under resources in web module.
and add the follow property to change server port
server.port=8081if you want to add some filter in you web application
define your filter
public class MyFilter implements Filter{ @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("my filter init"); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { System.out.println("do filter"); filterChain.doFilter(servletRequest, servletResponse); } @Override public void destroy() { System.out.println("destroy"); } }then configure your filter in your ApplicationConfig.java
@Configuration public class ApplicationConfig { @Bean public FilterRegistrationBean registrationBean(){ FilterRegistrationBean registration = new FilterRegistrationBean(); registration.setFilter(new MyFilter()); registration.addUrlPatterns("/*"); registration.setName("MyFilter"); registration.setOrder(1); return registration; } }run your application and you will see the filter is load.
reference: http://www.jianshu.com/p/05c8be17c80a
一个项目中用到了hive和hadoop,在用spring-boot搭个任务导入平台的时候,启动的时候报错了
[ERROR]2017-07-05 16:53:03,820,[SpringApplication], Application startup failed java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDefault(Z)V at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.addDefaultServlet(JettyEmbeddedServletContainerFactory.java:454) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.configureWebAppContext(JettyEmbeddedServletContainerFactory.java:359) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory.getEmbeddedServletContainer(JettyEmbeddedServletContainerFactory.java:174) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] Wrapped by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is java.lang.NoSuchMethodError: org.eclipse.jetty.servlet.ServletMapping.setDefault(Z)V at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) ~[spring-context-4.3.9.RELEASE.jar:4.3.9.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.4.RELEASE.jar:1.5.4.RELEASE]这个是因为hadoop中的servlet-api的版本和spring-boot中的版本冲突了hadoop中的是 2.5,而spring-boot中的是3.1,所以在pom.xml加入如下配置
<dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>2.7.3</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </exclusion> </exclusions> </dependency>reference: https://stackoverflow.com/questions/31942908/spring-boot-application-with-hive-connection-doesnt-start-embedded-tomcat
github: https://github.com/jjzhu-ncu/bg-study/tree/master/spring-boot/spring-boot-mult-module
相关资源:spring-boot示例项目