1. 程式人生 > >maven+SSM+shiro+junit+jetty+log4j環境配置的最佳實踐

maven+SSM+shiro+junit+jetty+log4j環境配置的最佳實踐

一次 bject catch mapping getbean tco 客戶 包名 目錄

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

pom中的依賴跟著思路一批一批的來

創建項目

1、eclipse中創建一個maven項目,Packing選war,

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

2、創建後用Java EE Tools -> Generate Deployment Descriptor Stub生成WEB-INF目錄。

錯誤消除。

3、Build Path的JRE改成workspace默認。

jetty插件

4、本地倉庫找到\.m2\repository\org\mortbay\jetty\jetty-maven-plugin\裏面找到jar包,

提取出/src/main/resources/jetty/webdefault.xml,useFileMappedBuffer改成false,

拷貝進項目src/main/resources的jetty中,pom.xml裏追加<build/>

        <build>
        <plugins>
            <plugin>
                <groupId
>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.16.v20140903</version> <configuration> <webDefaultXml>/src/main/resources/jetty/webdefault.xml</webDefaultXml>
<scanIntervalSeconds>2</scanIntervalSeconds> <webApp> <contextPath>/</contextPath> </webApp> <stopKey>shutdown</stopKey> <stopPort>8085</stopPort> </configuration> </plugin> </plugins> </build>

5、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

6、創建一個Remote DEBUG,端口填8090

※ 紅藍先後啟動後,瀏覽器輸入http://localhost:8080/已經能看到效果了,同時這個模式也是debug模式了

Directory: /

WEB-INF/     0 bytes     2017-7-10 16:09:14

7、想要關閉的話,創建一個External Tools即可,其中Arguments填jetty:stop,Environment中不追加參數

※ 默認端口號(8080),DEBUG用端口號(8090),關閉用端口號(8085),三者最好不一致,避免不必要的麻煩。

junit

8、pom.xml中追加junit

SpringMVC

9、pom.xml中追加

  spring-webmvc

  servlet-api

10、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>

11、src/main/resources的springframework中創建配置文件

dispatcherservlet-servlet.xml

<!--
    以下幾個第一次就可以直接配置了
        命名空間:直接拷貝
        註解驅動:直接拷貝
        掃描controller:改包名
        視圖解析器:改位置
        靜態資源:一般需要css、js、img等
-->
<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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <!-- 註解驅動 --> <mvc:annotation-driven /> <!-- 掃描controller --> <context:component-scan base-package="io.deolin.controller" /> <!-- 視圖解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 靜態資源 --> <mvc:resources mapping="/js/**" location="/js/" /> <mvc:resources mapping="/html/**" location="/html/" /> </beans>

※ SpringMVC完成,可以寫個controller測試一下

Spring

12、需要spring-context依賴,但在spring-webmvc裏面已經有了,所以pom.xml不用追加了

13、src/main/resources的springframework中創建配置文件

application-context.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-4.1.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-4.1.xsd
                http://www.springframework.org/schema/tx
                http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
                http://www.springframework.org/schema/util
                http://www.springframework.org/schema/util/spring-util-2.0.xsd">

    <bean class="io.deolin.util.SpringContext" />

</beans>

14、創建類io.deolin.util.SpringContext

package io.deolin.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

public class SpringContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContext.applicationContext = applicationContext;
    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(String name) throws BeansException {
        try {
            return applicationContext.getBean(name);
        } catch (Exception e) {
            throw new RuntimeException("Bean不存在!");
        }
    }

}

※ Spring完成,Spring上下文被映射到了工具類SpringContext,可以寫個POJO,註冊到application-context.xml中,測試一下

……

maven+SSM+shiro+junit+jetty+log4j環境配置的最佳實踐