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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM