思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合

pom中的依赖跟着思路一批一批的来

创建项目

1、eclipse中创建一个maven项目,Packing选war,

Project Facts的Dynamic Web Module改成3.0,Java改成1.8。

2、创建后用Java EE Tools -> Generate Deployment Descriptor Stub生成WEB-INF目录。

错误消除。确保web.xml的version是3.0。

jetty插件

3、在src/main/resource下建立jetty目录,把编辑好的webdefault.xml放进去(webdefault.xml的取得和修改)。

在pom.xml里追加<build/>

  1. <build>
  2. <plugins>
  3. <plugin>
  4. <groupId>org.eclipse.jetty</groupId>
  5. <artifactId>jetty-maven-plugin</artifactId>
  6. <version>9.4.6.v20170531</version>
  7. <configuration>
  8. <scanIntervalSeconds>60</scanIntervalSeconds>
  9. <webApp>
  10. <contextPath>/</contextPath>
  11. <defaultsDescriptor>
  12. src/main/resources/jetty/webdefault.xml
  13. </defaultsDescriptor>
  14. </webApp>
  15. <stopKey>shutdown</stopKey>
  16. <stopPort>8085</stopPort>
  17. </configuration>
  18. </plugin>
  19. <plugin>
  20. <groupId>org.apache.maven.plugins</groupId>
  21. <artifactId>maven-compiler-plugin</artifactId>
  22. <version>3.6.1</version>
  23. <configuration>
  24. <source>1.8</source>
  25. <target>1.8</target>
  26. </configuration>
  27. </plugin>
  28. </plugins>
  29. </build>

4、eclipse -> Run创建一个External Tools,

Main选项卡中Location指向maven客户端,

Working Directory指向本项目,

Arguments填jetty:run,

Environment选项卡中追加

  1.   MAVEN_OPTS
  2.   -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=8090,server=y,suspend=y

5、创建一个Remote DEBUG,端口填8090

※ 红蓝先后启动后,服务器就正式启动了,

但是由于jetty默认首页被注释掉了,所以想要测试的话,

需要在webapp目录下写个html做测试(顺便可以试试热修改静态文件)。

6、想要关闭的话,创建一个External Tools即可,其中Arguments填jetty:stop,Environment中不追加参数

※ 默认端口号(8080),DEBUG用端口号(8090),关闭用端口号(8085),三者最好不一致,避免不必要的麻烦。

Junit

7、pom.xml追加

  junit

  spring-test

SpringMVC

8、pom.xml中追加

  spring-webmvc

  servlet-api

9、web.xml追加DispatcherServlet,顺便把编码过滤器也加了

  1. <servlet>
  2. <servlet-name>spring-webmvc</servlet-name>
  3. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  4. <init-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>classpath:springframework/dispatcherservlet-servlet.xml</param-value>
  7. </init-param>
  8. <load-on-startup>1</load-on-startup>
  9. </servlet>
  10. <servlet-mapping>
  11. <servlet-name>spring-webmvc</servlet-name>
  12. <url-pattern>/</url-pattern>
  13. </servlet-mapping>
  14.  
  15. <filter>
  16. <filter-name>encodingFilter</filter-name>
  17. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  18. <init-param>
  19. <param-name>encoding</param-name>
  20. <param-value>UTF-8</param-value>
  21. </init-param>
  22. <init-param>
  23. <param-name>forceEncoding</param-name>
  24. <param-value>true</param-value>
  25. </init-param>
  26. </filter>
  27. <filter-mapping>
  28. <filter-name>encodingFilter</filter-name>
  29. <url-pattern>/*</url-pattern>
  30. </filter-mapping>

10、src/main/resources的springframework中创建配置文件

dispatcherservlet-servlet.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  11.  
  12. <!-- 处理静态资源 -->
  13. <mvc:default-servlet-handler />
  14.  
  15. <!-- 注解驱动 -->
  16. <mvc:annotation-driven />
  17.  
  18. <!-- 扫描controller -->
  19. <context:component-scan base-package="io.deolin.controller" />
  20.  
  21. <!-- 视图解析器 -->
  22. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  23. <property name="prefix" value="/html/" />
  24. <property name="suffix" value=".html" />
  25. </bean>
  26.  
  27. </beans>

※ SpringMVC完成,可以写个controller测试一下

Spring

11、需要spring-context依赖,但在spring-webmvc里面已经有了,所以pom.xml不用追加了

12、src/main/resources的springframework中创建配置文件

application-context.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. <import resource="classpath:springframework/spring-*.xml" />
  8.  
  9. <bean class="io.spldeolin.bestpractice.util.ApplicationContext" />
  10.  
  11. </beans>

13、创建类io.deolin.util.ApplicationContext

  1. package io.deolin.util;
  2.  
  3. import org.springframework.context.ApplicationContextAware;
  4.  
  5. public class ApplicationContext implements ApplicationContextAware {
  6.  
  7. private static org.springframework.context.ApplicationContext applicationContext;
  8.  
  9. @Override
  10. public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) {
  11. ApplicationContext.applicationContext = applicationContext;
  12. }
  13.  
  14. public static org.springframework.context.ApplicationContext getApplicationContext() {
  15. return applicationContext;
  16. }
  17.  
  18. public static Object getBean(String name) {
  19. return applicationContext.getBean(name);
  20. }
  21.  
  22. }

14、web.xml追加Spring上下文监听器和上下文配置

  1. <listener>
  2. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
  4. <context-param>
  5. <param-name>contextConfigLocation</param-name>
  6. <param-value>
  7. classpath:springframework/application-context.xml
  8. </param-value>
  9. </context-param>

※ Spring完成,Spring上下文被映射到了工具类SpringContext,可以写个POJO,注册到application-context.xml中,测试一下

Log4j2

15、pom.xml中追加

log4j-web    (里面包含了log4j2的api依赖 和 实现依赖)

log4j-jcl     (使log4j2的实现依赖能适配到spring-core中commons logging的api依赖)

16、web.xml中追加Log4j2上下文监听器和过滤器

  1. <listener>
  2. <listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
  3. </listener>
  4. <filter>
  5. <filter-name>log4jServletFilter</filter-name>
  6. <filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
  7. </filter>
  8. <filter-mapping>
  9. <filter-name>log4jServletFilter</filter-name>
  10. <url-pattern>/*</url-pattern>
  11. <dispatcher>REQUEST</dispatcher>
  12. <dispatcher>FORWARD</dispatcher>
  13. <dispatcher>INCLUDE</dispatcher>
  14. <dispatcher>ERROR</dispatcher>
  15. </filter-mapping>

17、src/main/resources中新建log4j2.xml

Deolin目前用的是 这套配置

MyBatis

18、pom.xml中追加

mybatis

mybatis-spring

commons-dbcp2

spring-tx

spring-jdbc

mysql-connector-java

19、src/main/resources中新建mybatis/db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/web-integration?characterEncoding=UTF-8&useSSL=false
  3. jdbc.username=root
  4. jdbc.password=root

20、src/main/resources/mybatis中新建mappers.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
  3. <configuration>
  4. <mappers>
  5. <package name="io.deolin.mapper" />
  6. </mappers>
  7. </configuration>

21、在src/main/resources/springframework中新建spring-mybatis.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context.xsd">
  10.  
  11. <context:property-placeholder location="classpath:mybatis/db.properties" />
  12.  
  13. <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
  14. <property name="driverClassName" value="${jdbc.driver}" />
  15. <property name="url" value="${jdbc.url}" />
  16. <property name="username" value="${jdbc.username}" />
  17. <property name="password" value="${jdbc.password}" />
  18. </bean>
  19.  
  20. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  21. <property name="dataSource" ref="dataSource" />
  22. <property name="configLocation" value="classpath:mybatis/mappers.xml" />
  23. </bean>
  24.  
  25. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  26. <property name="basePackage" value="io.deolin.mapper"></property>
  27. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
  28. </bean>
  29.  
  30. </beans>

22、application-context.xml中追加

  1. <import resource="classpath:springframework/spring-mybatis.xml" />

※ Mybatis整合完整,同时启动了事务管理,可以写个Service层和Mapper层测试一下。

详细:Github

maven+SSM+junit+jetty+log4j2环境配置的最佳实践的更多相关文章

  1. Nacos: Namespace 和 Endpoint 在生产环境下的最佳实践

    随着使用 Nacos 的企业越来越多,遇到的最频繁的两个问题就是:如何在我的生产环境正确的来使用 namespace 以及 endpoint.这篇文章主要就是针对这两个问题来聊聊使用 nacos 过程 ...

  2. Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践

    Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...

  3. maven 利用 profile 进行多环境配置

    我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...

  4. ansible安装配置及最佳实践roles

    ansible是什么? ansible是一款轻量级配置管理工具,用于远程批量部署.安装.配置.类似的还有puppet.saltstack,各有所长,任君自选. 官方文档:http://docs.ans ...

  5. 生产环境容器落地最佳实践 --JFrog 内部K8s落地旅程

    引言 Kubernetes已经成为市场上事实上领先的编配工具,不仅对技术公司如此,对所有公司都是如此,因为它允许您快速且可预测地部署应用程序.动态地伸缩应用程序.无缝地推出新特性,同时有效地利用硬件资 ...

  6. Maven之profile实现多环境配置动态切换

            一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...

  7. 使用maven的profile构建不同环境配置

    基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...

  8. maven工程的下载及其环境配置

    Maven是一个项目管理工具,它给我们提供了好多有用的组件和工具. Maven下载: Maven下载载地址:http://maven.apache.org/download.cgi (1)进入下载界面 ...

  9. (十一)Maven之profile实现多环境配置动态切换

    原文链接:https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html 一.多环境配置文件的放置  将不同环境下的配 ...

随机推荐

  1. NeurIPS 2018 中的贝叶斯研究

    NeurIPS 2018 中的贝叶斯研究 WBLUE 2018年12月21日   雷锋网 AI 科技评论按:神经信息处理系统大会(NeurIPS)是人工智能领域最知名的学术会议之一,NeurIPS 2 ...

  2. root用户和sudo使用root权限的区别(转)

    百度百科:https://baike.baidu.com/item/sudo/7337623?fr=aladdin sudo指令 功能: 以root的身分执行命令 语法: sudo 其他指令 用户: ...

  3. 普通表分区改造_rename方式

    一.需求 配合开发人员,对业务临时表进行分区改造(业务认为的临时表,只需要保留近一月数据,并非oracle临时表类型) 二.如下记录完整过程 开发需求 TS_PM 以time_key分区 .沟通明确方 ...

  4. vue实现web登陆权限控制

    实现原理:vueRouter控制前端页面跳转路由,当登录成功后,返回用户登录token信息,将token信息放到store中,router路由跳转取store中状态有token时,当取到token时跳 ...

  5. python实现nc

    #!/usr/bin/python2 import sys import socket import getopt import thread import subprocess listen =Fa ...

  6. 定位之z-index

    我们已经知道固定定位(fixed)和绝对定位(absolute)可以让盒子浮起来 相对定位(relactive)虽然不能让盒子浮起来,但也是可以让图层浮起来 那么既然大家都可以浮起来,就会存在一个问题 ...

  7. ASE19 团队项目 alpha 阶段 Frontend 组 scrum9 记录

    本次会议于11月14日,11:30 在微软北京西二号楼13158,持续15分钟. 与会人员:Jingyi Xie, Jiaqi Xu, Jingwei Yi, Hanyue Tu 请假: Ziwei ...

  8. 【OGG 故障处理】OGG-01028

    通过ATSCN 的方式启动REPLICAT 进程的时候报错 GGSCI> START REPLICAT RP_XXXX1, ATCSN 15572172378 GGSCI> VIEW RE ...

  9. zookeeper:2

    单机环境下安装: 下载地址:http://apache.fayea.com/zookeeper/stable/ 解压zookeeper :tar -zxvf zookeeper-3.4.10.tar. ...

  10. http服务详解(3)

    https https:http over sslSSL会话的简化过程 (1) 客户端发送可供选择的加密方式,并向服务器请求证书 (2) 服务器端发送证书以及选定的加密方式给客户端 (3) 客户端取得 ...