最近接手的一个项目,使用的是spring+springmvc+hibernate+jboss这一套框架,以前都是用是mybatis+tomcat,这里提前熟悉下,搭建个简单的脚手架,方便以后做测试,顺便记录下搭建步骤。这里特别说明下,写完这篇文章,只是测试了项目能正常启动,能访问index.jsp,没有测试spring,hibernate这些框架,不过问题应该不大,如果报错了后微调下就好了。
项目github路径:https://github.com/Harry-ZH-Wang/sshDemo
首先创建一个空的maven的架子,后面修改pom.xml文件,也可以建立一个普通的项目,然后关联maven,这里做个maven项目
创建maven项目
因为是web项目,模版的时候可以选择maven-archetype-webapp,也可以选择默认的,常用的包都在里面,不够后期配置pom.xml就可以了。
填写好group id其实就是包名,后面也可以更改的,还有artifiact id就是工程名称,然后next。
这里选择maven的配置,如果没有修改可以使用默认的,如果有修改了,maven home使用自己安装的路径,settings文件这些指向自己的路径。
设置工程名字,项目的保存路径,点击完成。
等待项目创建。
添加jar依赖,调整工程结构
创建完成后根据自己的项目修改pom.xml,这里是我的pom配置,是随便找的一个以前的,有些不是必要的包。
<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>4.0.0</modelVersion>
<groupId>sshDemo</groupId>
<artifactId>sshDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>sshDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- spring版本号 -->
<spring.version>4.2.0.RELEASE</spring.version>
<!-- mybatis版本号 -->
<mybatis.version>3.2.8</mybatis.version>
<!-- log4j日志文件管理包版本 -->
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- spring框架包 start -->
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring框架包 end -->
<!-- mybatis框架包 start -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
<!-- mybatis框架包 end -->
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>
<!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 -->
<!-- <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId>
<version>1.4</version> </dependency> -->
<!-- jstl标签类 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- log END -->
<!-- Json -->
<!-- 格式化对象,方便输出日志 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<!-- 上传组件包 start -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<!-- oracle驱动 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>12.1.0.2.0</version>
</dependency>
<!-- 添加CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.8</version>
</dependency>
<!-- POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15-beta2</version>
</dependency>
<!-- pinyin4j -->
<dependency>
<groupId>com.belerweb</groupId>
<artifactId>pinyin4j</artifactId>
<version>2.5.0</version>
</dependency>
<!-- jpinyin -->
<dependency>
<groupId>com.github.stuxuhai</groupId>
<artifactId>jpinyin</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<!-- FTP -->
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.6</version>
</dependency>
<!-- httpclient -->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<!-- xml -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jr</groupId>
<artifactId>jackson-jr-all</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<!-- rar解压 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>java-unrar</artifactId>
<version>1.7.0-3</version>
</dependency>
<dependency>
<groupId>org.apache.tika</groupId>
<artifactId>tika-core</artifactId>
<version>1.17</version>
</dependency>
<dependency>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
<version>1.6.5</version>
</dependency>
<!--Hibernate依赖-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- ehcache jar package -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
<dependency>
<groupId>org.jboss.snowdrop</groupId>
<artifactId>snowdrop-vfs</artifactId>
<version>3.1.1.Final</version>
</dependency>
</dependencies>
<build>
<finalName>sshDemo</finalName>
<plugins>
<!-- jdk版本号 不指定这个重写方法注解会报错 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit4</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
等maven把依赖下载完成后我们看下项目等结构,是少了存储java类等目录结构,我们需要自己建立一个。
调整下目录结构,配置方式多种多样,这里我是这么配置的
src/main/resources下放各种配置文件
src/main/java下放源代码
webapp/jsp下放jsp页面
以此类推
先建立java,resources,test文件夹
在Project Structure中 java设置为Sources(蓝色),resources设置为Resources,test为Test
配置spring springmvc hibernate jboss
配置的方式多种多样,这里只是简单的搭建脚手架,首先看一下完成了的目录结构
spring 配置文件
applicationContext.xml 配置文件,这里是主的spirng配置文件,配置事务,数据库c3p0驱动,cxf等都在这个配置文件中,是放在resources文件夹下。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!-- 配置扫描规则 -->
<context:component-scan base-package="com.wzh"></context:component-scan>
<cache:annotation-driven cache-manager="cacheManager"></cache:annotation-driven>
<!-- 定义一个缓存管理器 -->
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"></property>
</bean>
<!-- 商业数据源 c3p0,apache提供的DBCP-->
<!-- 商业数据源 c3p0, apache提供的DBCP -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl" value="jdbc:oracle:thin:@192.168.0.114:1521:oracleSID"></property>
<property name="user" value="system"></property>
<property name="password" value="Password01"></property>
<property name="initialPoolSize" value="15"></property><!-- 初始化连接数的数量 -->
<property name="minPoolSize" value="15"></property>
<property name="maxPoolSize" value="100"></property>
<property name="acquireIncrement" value="5"></property>
<property name="maxStatements" value="200"></property>
<property name="maxIdleTime" value="200"></property><!-- 超时时间 -->
<property name="checkoutTimeout" value="2000"></property><!-- 多长时间回收一次超时连接 -->
</bean>
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.wzh.beans</value>
</list>
</property>
</bean>
<!-- 事务 -->
<bean id="txManage" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 第二种事务管理方式:声明式事务管理 -->
<!-- 定义一个通知 -->
<tx:advice id="txAdvice" transaction-manager="txManage">
<tx:attributes>
<tx:method name="*" read-only="true"></tx:method>
<tx:method name="add*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="save*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="insert*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="update*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="modify*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="delete*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="remove*" read-only="false" propagation="REQUIRED" isolation="REPEATABLE_READ" rollback-for="java.lang.Exception"></tx:method>
<tx:method name="select*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="find*" read-only="true" propagation="SUPPORTS"></tx:method>
<tx:method name="get*" read-only="true" propagation="SUPPORTS"></tx:method>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.wzh.service.impl.*.*(..))"></aop:pointcut>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods"></aop:advisor>
</aop:config>
<!-- 配置transactionTemplate -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="txManage"></property>
<!--定义事务隔离级别,-1表示使用数据库默认级别-->
<property name="readOnly" value="false"></property>
<property name="isolationLevelName" value="ISOLATION_DEFAULT"></property>
<property name="propagationBehaviorName" value="PROPAGATION_REQUIRED"></property>
</bean>
<!-- 开启切面功能 -->
<aop:aspectj-autoproxy />
<!--CXF配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:/cxf-beans.xml"/>
</beans>
cxf 配置文件
cxf-beans.xml 配置文件,需要cxf接口才要配置,这里是空的文件,没有发布接口,只是满足项目启动,可以不要此文件,如果不要此配置文件,需要同步在applicationContext.xml 和wel.xml中去掉关于cxf的配置,是放在resources文件夹下。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!--CXF配置 这里因为是3.x的CXF 只需要引用一个文件就好 -->
<import resource="classpath:META-INF/cxf/cxf.xml"></import>
<!-- id是接口名,不重复就好,serviceClass 是接口的路径,address是对外暴露的地址名,bean classs是引用的接口的实现类 -->
</beans>
缓存配置文件
ehcache.xml 配置hibernate的缓存,这里只是简单的配置了下,是放在resources文件夹下。
<ehcache>
<diskStore path="java.io.tmpdir"/><!-- D:/cache -->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
memoryStoreEvictionPolicy="LFU"
/>
<cache
name="users"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="10"
timeToLiveSeconds="20"
overflowToDisk="true"
memoryStoreEvictionPolicy="LFU"
/>
</ehcache>
<!-- maxElementsInMemory 咱们内存中,可以缓存中允许最大的对象数量 -->
<!-- eternal 缓存是否永久有效,false -->
<!-- timeToIdleSeconds 定义的缓存中,从来没有被调用的数据,它的生命周期,单位是秒 -->
<!-- timeToLiveSeconds 调用的数据,在缓存中,可以存活的时间,相当于:满血复活 -->
<!-- overflowToDisk 当内存不足的时候,是否转存到磁盘中去 -->
<!-- memoryStoreEvictionPolicy 缓存的出站策略, 当maxElementsInMemory达到这个峰值的时候,
,Ehcache将如何清理之前的一些不必要的数据,
LFU(最少访问次数来清理) FIFO(先进先出) LRU(最少使用,默认缓存策略)
-->
springmvc 配置文件
Spring-servlet.xml 配置文件,配置springmvc相关功能,放在web-inf文件夹下。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- 开启springmvc的注解支持 -->
<mvc:annotation-driven conversion-service="tc" />
<!-- 定义静态资源 -->
<mvc:resources location="/static/" mapping="/static/**" />
<!-- 可以配置一个全局类型转换器 -->
<!-- 配置spring定义的转换工厂类 -->
<bean id="tc" class="org.springframework.context.support.ConversionServiceFactoryBean">
</bean>
<!-- 制定扫描规则 -->
<context:component-scan base-package="com">
<!-- 制定过滤器 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository" />
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>
<!-- 可以配置一个全局类型转换器 -->
<!-- 配置spring定义的转换工厂类 -->
<!-- 配置http消息体数据与JSON转换 -->
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJackson2HttpMessageConverter"></ref>
</list>
</property>
</bean>
<!-- 配置HTTP消息信息与JSON对象之间的一个转换器 -->
<bean id="mappingJackson2HttpMessageConverter"
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
</bean>
<!-- 视图解析器 JSP -->
<bean id="urlBasedViewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"></property>
<property name="prefix" value="/WEB-INF/JSP/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver 说明: p:defaultEncoding="UTF-8":这里设置默认的文件编码为UTF-8,必须与用户JSP的默认编码一致;
p:maxUploadSize="5000000":指定文件上传大小,单位为字节; p:uploadTempDir="fileUpload/temp":文件上传临时目录,上传完成后,就会将临时文件删除; -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="UTF-8" p:maxUploadSize="5000000" p:uploadTempDir="fileUpload/temp">
</bean>
</beans>
log4j 日志配置文件
因为用的jboss,jboss默认使用的自己的日志框架,如果要使用自己的日志配置,除了log4j.porperties,还需要jboss-deployment-structure.xml 配置下jboss,放在web-inf文件夹下
log4j.properties
### 设置###
log4j.rootLogger = debug,stdout,D,E
### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
### 输出DEBUG 级别以上的日志###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = ${jboss.server.log.dir}/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
### 输出ERROR 级别以上的日志 ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =${jboss.server.log.dir}/error.log
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
jboss-deployment-structure.xml
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
<deployment>
<exclusions>
<module name="org.apache.log4j"/>
</exclusions>
</deployment>
</jboss-deployment-structure>
配置web.xml
最后把配置文件整合到项目工程中
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>mavenDemo</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>sshDemo.root</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.jboss.spring.vfs.context.VFSXmlWebApplicationContext
</param-value>
</context-param>
<!-- spring容器启动 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.properties</param-value>
</context-param>
<!-- 指定日志刷新的时间,单位毫秒 -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置欢迎页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 字符过滤器 -->
<filter>
<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 启动CXF框架 web service-->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<!-- 配置核心控制器 -->
<servlet>
<servlet-name>Spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
添加启动容器
项目整合好后,对jboss进行配置
本地安装jboss
tomcat的例子很多,jboss的稍微要少一些,因为我自己也没怎么用过jboss,这里正好使用jboss,本地是已经安装好了jboss的,mac安装jboss很简单,我这里是直接下载了个zip放在对应的文件夹下,下载的时候需要注意jboss不同版本对应不同版本的jdk,我这里用的1.7的jdk,所有下载的jboss jboss-as-7.1.1.Final。
下载路径:http://jbossas.jboss.org/downloads
解压到对应目录后,启动测试下
如果要停止,控制台界面 control+c
idea 配置jboss
首先添加jboss全局容器,在application server菜单中添加
选择安装好的jboss路径
添加项目启动的jboss
选择之前添加的全局jboss容器
切换到deployment标签页,点击添加按钮,选择war explored选项
切换回server,配置热部署
保存后,选择配置jboss容器启动就好了