maven+SSM+shiro+junit+jetty+log4j环境配置的最佳实践
Posted Deolin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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中,测试一下
……
以上是关于maven+SSM+shiro+junit+jetty+log4j环境配置的最佳实践的主要内容,如果未能解决你的问题,请参考以下文章
SSM+Redis+Shiro+Maven框架搭建及集成应用
java大数据 高并发 系统框架 springmvc mybatis Bootstrap html5 shiro maven SSM SSH
java 整合redis缓存 SSM 后台框架 rest接口 shiro druid maven bootstrap html5