maven+SSM+junit+jetty+log4j2环境配置的最佳实践
思路大致是 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/>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
<configuration>
<scanIntervalSeconds>60</scanIntervalSeconds>
<webApp>
<contextPath>/</contextPath>
<defaultsDescriptor>
src/main/resources/jetty/webdefault.xml
</defaultsDescriptor>
</webApp>
<stopKey>shutdown</stopKey>
<stopPort>8085</stopPort>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
4、eclipse -> Run创建一个External Tools,
Main选项卡中Location指向maven客户端,
Working Directory指向本项目,
Arguments填jetty:run,
Environment选项卡中追加
MAVEN_OPTS
-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,顺便把编码过滤器也加了
<servlet>
<servlet-name>spring-webmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springframework/dispatcherservlet-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-webmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> <filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
10、src/main/resources的springframework中创建配置文件
dispatcherservlet-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 处理静态资源 -->
<mvc:default-servlet-handler /> <!-- 注解驱动 -->
<mvc:annotation-driven /> <!-- 扫描controller -->
<context:component-scan base-package="io.deolin.controller" /> <!-- 视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/html/" />
<property name="suffix" value=".html" />
</bean> </beans>
※ SpringMVC完成,可以写个controller测试一下
Spring
11、需要spring-context依赖,但在spring-webmvc里面已经有了,所以pom.xml不用追加了
12、src/main/resources的springframework中创建配置文件
application-context.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:springframework/spring-*.xml" /> <bean class="io.spldeolin.bestpractice.util.ApplicationContext" /> </beans>
13、创建类io.deolin.util.ApplicationContext
package io.deolin.util;
import org.springframework.context.ApplicationContextAware;
public class ApplicationContext implements ApplicationContextAware {
private static org.springframework.context.ApplicationContext applicationContext;
@Override
public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext) {
ApplicationContext.applicationContext = applicationContext;
}
public static org.springframework.context.ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
}
14、web.xml追加Spring上下文监听器和上下文配置
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:springframework/application-context.xml
</param-value>
</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上下文监听器和过滤器
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</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
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/web-integration?characterEncoding=UTF-8&useSSL=false
jdbc.username=root
jdbc.password=root
20、src/main/resources/mybatis中新建mappers.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<package name="io.deolin.mapper" />
</mappers>
</configuration>
21、在src/main/resources/springframework中新建spring-mybatis.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:mybatis/db.properties" /> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis/mappers.xml" />
</bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="io.deolin.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean> </beans>
22、application-context.xml中追加
<import resource="classpath:springframework/spring-mybatis.xml" />
※ Mybatis整合完整,同时启动了事务管理,可以写个Service层和Mapper层测试一下。
详细:Github
maven+SSM+junit+jetty+log4j2环境配置的最佳实践的更多相关文章
- Nacos: Namespace 和 Endpoint 在生产环境下的最佳实践
随着使用 Nacos 的企业越来越多,遇到的最频繁的两个问题就是:如何在我的生产环境正确的来使用 namespace 以及 endpoint.这篇文章主要就是针对这两个问题来聊聊使用 nacos 过程 ...
- Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践
Spring Boot 自定义kafka 消费者配置 ContainerFactory最佳实践 本篇博文主要提供一个在 SpringBoot 中自定义 kafka配置的实践,想象这样一个场景:你的系统 ...
- maven 利用 profile 进行多环境配置
我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml.maven 的 profile 等.这里介绍的就是如何利用 profi ...
- ansible安装配置及最佳实践roles
ansible是什么? ansible是一款轻量级配置管理工具,用于远程批量部署.安装.配置.类似的还有puppet.saltstack,各有所长,任君自选. 官方文档:http://docs.ans ...
- 生产环境容器落地最佳实践 --JFrog 内部K8s落地旅程
引言 Kubernetes已经成为市场上事实上领先的编配工具,不仅对技术公司如此,对所有公司都是如此,因为它允许您快速且可预测地部署应用程序.动态地伸缩应用程序.无缝地推出新特性,同时有效地利用硬件资 ...
- Maven之profile实现多环境配置动态切换
一般的软件项目,在开发.测试及生产等环境下配置文件中参数是不同的.传统的做法是在项目部署的时候,手动修改或者替换这个配置文件.这样太麻烦了,我们可以用Maven的profile来解决这 ...
- 使用maven的profile构建不同环境配置
基本概念说明(resources.filter和profile): 1.profiles定义了各个环境的变量id 2.filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profil ...
- maven工程的下载及其环境配置
Maven是一个项目管理工具,它给我们提供了好多有用的组件和工具. Maven下载: Maven下载载地址:http://maven.apache.org/download.cgi (1)进入下载界面 ...
- (十一)Maven之profile实现多环境配置动态切换
原文链接:https://www.cnblogs.com/zeng1994/p/a442108012ffd6a97b22c63055b48fe9.html 一.多环境配置文件的放置 将不同环境下的配 ...
随机推荐
- PHP关于VC11,VC9,VC6以及Thread Safe和Non Thread Safe版本选择的问题
这里是我在搭建php环境时收集的资料供大家参考: 现在PHP官网上下载PHP安装包都有VC11或VC9的字样,这是什么含义,我们应该下载哪种安装包更好呢?其实PHP官网给出了答案: VC6版本是使用V ...
- 轻松搭建CAS 5.x系列文章
轻松搭建CAS 5.x系列(1)-使用cas overlay搭建SSO SERVER服务端 轻松搭建CAS 5.x系列(2)-搭建HTTPS的SSO SERVER端 轻松搭建CAS 5.x系列(3)- ...
- (错误) Eclipse使用Maven创建Web时错误
转自:http://blog.csdn.net/afgasdg/article/details/12757433 问题描述: 使用Eclipse自带的Maven插件创建Web项目时报错: Could ...
- maven项目打包和编译跳过单元测试和javadoc
代码中可能由于单元测试.注释(方法中的参数)或者maven javadoc插件的问题导致无法打包,影响工作,为避免这两种情况可以在打包时输入命令: mvn clean install -Dmaven. ...
- ArrayList和CopyOnWriteArrayList(转载)
这篇文章的目的如下: 了解一下ArrayList和CopyOnWriteArrayList的增删改查实现原理 看看为什么说ArrayList查询快而增删慢? CopyOnWriteArrayList为 ...
- JAVA基于PDF box将PDF转为图片
在一项目中用到,本身我是.NET的,团队中有用到JAVA,故此我处理这个功能,记录以下备用. 1.引用:fontbox-2.0.16.jar.pdfbox-app-2.0.16.jar 版本一定要正确 ...
- python 链接impala执行SQL
项目中用python来链接impala数据库最常见,一个简单的链接,获取结果. #!/usr/bin/python # -*- coding:utf-8 -*- from impala.dbapi i ...
- 第四章、drf框架 - 三大认证组件 | 认证组件 | 权限组件 | 频率组件
目录 RBAC 基于用户权限访问控制的认证 - Role-Based Access Control 三大认证组件 源码分析 一.认证组件 自定义认证类 使用: 二.权限组件 自定义权限类 三.频率组件 ...
- zookeeper:3
zoo.cfg配置文件 tickTime=2000 :zookeeper中最小的时间单位长度 (ms). initLimit=10 :follower节点启动后与leader节点完成数据同步的时间 ...
- Linux网络管理——ifconfig、route
Linux识别到的网络设备 eth# eth0 eth1 以太网卡 wifi# wifi0 wifi1 无线网卡 ppp# ppp0 ppp1 拨号连接 lo 本地环回网卡 ...