maven+SSM+shiro+junit+jetty+log4j环境配置的最佳实践

思路大致是 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中,测试一下

……

时间: 2024-08-06 07:54:15

maven+SSM+shiro+junit+jetty+log4j环境配置的最佳实践的相关文章

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

引言 Kubernetes已经成为市场上事实上领先的编配工具,不仅对技术公司如此,对所有公司都是如此,因为它允许您快速且可预测地部署应用程序.动态地伸缩应用程序.无缝地推出新特性,同时有效地利用硬件资源. 本期我们将回顾采用Kubernetes作为容器编排工具的公司所面临的复杂性和挑战.我们希望我们提供的经验教训.最佳实践和技巧将帮助您在前往K8s旅途中起步并继续前进. 本期将介绍关于在Kubernetes生产环境的最佳实践,包括:: 为上K8s容器云准备好应用程序 在Kubernetes中获得

H3C交换配置PBR最佳实践

简要说明 PBR算是比较常用的功能,需要我们去掌握一下 配置步骤简要 配置BFD 配置track 配置acl 配置policy-based-route 在接口上面应用policy-based-route 配置步骤详解 配置BFD bfd echo-source-ip ip-address 配置track track 10 bfd echo interface Vlan-interface100 remote ip 192.168.10.4 local ip 192.168.10.5 track

Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World(转发)

[JSP]Maven+SSM框架(Spring+SpringMVC+MyBatis) - Hello World 来源:http://blog.csdn.net/zhshulin/article/details/37956105?utm_source=tuicool&utm_medium=referral(以下博文对原博文有改动和补充) 博文里红底白字的,为注意修改的地方,在这里先声明 源码:Maven&SSM框架 - Hello World 开发环境: Eclipse Java EE I

shell 脚本实战笔记(6)--集群环境配置检测

1). 背景: 集群部署的时候, 需要一致的配置和环境设置. 对于虚拟机集群, 可以借助镜像拷贝, 复制和还原集群机器. 对与物理机集群而言, 则不一样, 如果机器一多, 多人去操作和配置, 对于成熟精干的团队还好, 对于不熟悉环境的小团队, 由于水平的参差不齐, 往往会导致不一致的环境. 因此无论如何, 写脚本进行自动化的配置和环境校验总是最佳实践. 2). 假设应用场景:*) 系统环境: 安装CDH5, 集群规模为16台机器, 每台机器16CPU, 内存16G, 2块SATA盘共500G,

Maven管理SSM框架的pom.xml文件配置(自动下载所依赖的jar包)

<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/maven-v4_0_0.xsd"> <modelVersion&

Java-Maven(四):Eclipse集成Maven环境配置

一般maven都需要集成到IDE上使用的,而不是单独的使用,常见的maven可集成IDE:eclipse.IntelliJ IDEA.但这里就只学习eclipse集成maven的基础上,进行maven环境配置. eclipse的maven插件安装: 大多情况来说下载eclipse都默认已经集成了maven插件,验证是否集成eclipse菜单Window->Preferences 如果实在不行,可以在eclipse marketplace中查找maven,找到与当前eclipse版本一致的插件直接

maven(六),外置maven运行环境配置

外置maven eclipse内置的maven插件是固定版本,如果要用其他版本的maven,可以使用外置maven 下载地址: http://maven.apache.org/download.cgi   window系统下载 apache-maven-3.3.9-bin.zip, 解压 进入eclipse首选项--maven--installations--add--选择刚解压的maven目录--完成 如图第一行是eclipse内置maven3.3.3版本,第三行是我们刚刚添加的3.3.9版本

Maven详解及其环境配置

Maven详解 一.前言     以前做过的项目中,没有真正的使用过Maven,只知道其名声很大,其作用是用来管理jar 包的.最近一段时间在项目过程中使用Maven,用Maven构建的web项目,其项目结构只停留在了解阶段,没有深入的使用与理解,刚好最近看了一篇关于Maven的详解:就开始深入学习一下Maven的具体应用. 二.Maven的作用 在开发中,为了保证编译通过,我们会到处去寻找jar包,当编译通过了,运行的时候,却发现"ClassNotFoundException",我们

(二)Maven的安装与环境配置

想要安装 Apache Maven在Windows 系统上, 需要下载 Maven 的 zip 文件,并将其解压到你想安装的目录,并配置 Windows 环境变量. 所需工具 : 1.JDK 2.Maven 3.Windows 7 注 Maven 3.2 要求 JDK 1.6 或以上版本, 而 Maven 3.0/3.1 需要 JDK 1.5 或以上. 1.JDK 和 JAVA_HOME 确保已安装JDK,并 将"JAVA_HOME" 变量已加入到 Windows 环境变量中. JDK