Spring-MVC4 + JPA2 + MySql-5.5 + SLF4J + JBoss WildFly-8.1开发环境的搭建
面试被问Spring4,它的目的是把过去Spring3所有升级项目Spring4。现在将记录在此环境搭建过程。
第一次使用Maven Archetype创建一个项目框架,运行以下命令:
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=1.0 -DgroupId={你的groupId} -DartifactId={你的artifactId} -Dversion=1.0-SNAPSHOT
完毕后会生成最主要的文件夹结构,和一个包括了JUnit的pom.xml。
这显然是不够的。我们须要自行改动 pom.xml 加入所需依赖:
<properties>
<java-version>1.7</java-version>
<org.aspectj-version>1.7.3</org.aspectj-version>
<spring.version>4.0.1.RELEASE</spring.version>
<slf4jVersion>1.7.7</slf4jVersion>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency> <!-- Spring dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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> <!-- 支持文件上传 -->
<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> <!-- JPA -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.8</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency> <!-- MySql JDBC Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency> <!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${org.aspectj-version}</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency> <!-- @Inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency> <!-- JSON -->
<dependency>
<groupId>javax.json</groupId>
<artifactId>javax.json-api</artifactId>
<version>1.0</version>
</dependency> <!-- SLF4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4jVersion}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency> </dependencies>
然后把 java版本号改为1.7:(Spring4最低要求是1.6)
<build>
<finalName>shop</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArgument>-Xlint:all</compilerArgument>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
</configuration>
</plugin>
</plugins>
</build>
创建例如以下图所看到的的文件夹结构:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbmVvc21pdGg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
以下是各个配置文件的完整内容。
通过 jboss-deployment-structure.xml 命令JBoss不要使用内置的Log系统:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<deployment>
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>
通过 shop-ds.xml 在部署时动态向JBoss注冊数据源:
<? xml version="1.0" encoding="UTF-8"?>
<datasources xmlns="http://www.jboss.org/ironjacamar/schema">
<datasource jndi-name="java:jboss/datasources/shop"
enabled="true" use-java-context="true" pool-name="codeschooldb">
<connection-url>jdbc:mysql://localhost:3306/shop? characterEncoding=utf-8</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql</driver>
<security>
<user-name>root</user-name>
<password>111111</password>
</security>
</datasource>
</datasources>
因为我们使用 Servet 3.0。所以web.xml也要对应的升级:
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param> <!-- spring -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <session-config>
<session-timeout>15</session-timeout>
</session-config> </web-app>
通过Spring配置文件 servlet-context.xml 配置JPA, Hibernate。数据源JNDI查找等内容:
<?xml version="1.0" encoding="UTF-8"? >
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure --> <!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean> <!-- 容器管理的JPA -->
<beans:bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<beans:property name="dataSource" ref="dataSource" />
<beans:property name="jpaVendorAdapter" ref="jpaAdapter" />
<beans:property name="packagesToScan" value="cn.fh.shop.model" />
<beans:property name="jpaPropertyMap">
<beans:props>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
<beans:prop key="hibernate.format_sql">true</beans:prop>
<beans:prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</beans:prop>
<!-- 开启L2 cache -->
<beans:prop key="hibernate.cache.use_second_level_cache">true</beans:prop>
</beans:props>
</beans:property> </beans:bean>
<beans:bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<beans:property name="database" value="MYSQL" />
</beans:bean>
<jee:jndi-lookup id="dataSource" jndi-name="java:jboss/datasources/shop"
resource-ref="true" />
<beans:bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <!-- 事务 -->
<beans:bean class="org.springframework.orm.jpa.JpaTransactionManager"
id="transactionManager">
<beans:property name="entityManagerFactory" ref="emf" />
</beans:bean> <!-- 文件上传 -->
<beans:bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<beans:property name="maxUploadSize" value="100000" />
</beans:bean> <tx:annotation-driven /> <context:component-scan base-package="cn.fh.shop" /> </beans:beans>
src/main/resources/log4j.xml 配置例如以下:
<? xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p: %c - %m%n" />
</layout>
</appender> <!-- Application Loggers -->
<logger name="cn.fh.spring">
<level value="info" />
</logger> <!-- 3rdparty Loggers -->
<logger name="org.springframework.core">
<level value="info" />
</logger> <logger name="org.springframework.beans">
<level value="info" />
</logger> <logger name="org.springframework.context">
<level value="info" />
</logger> <logger name="org.springframework.web">
<level value="info" />
</logger> <!-- Root Logger -->
<root>
<priority value="debug" />
<appender-ref ref="console" />
</root> </log4j:configuration>
至此一切OK。运行 mvn clean install 构建project。将 shop.war 复制到 JBoss 其下部署文件夹。
版权声明:本文博客原创文章,博客,未经同意,不得转载。
Spring-MVC4 + JPA2 + MySql-5.5 + SLF4J + JBoss WildFly-8.1开发环境的搭建的更多相关文章
- Apache+MySQL+PHP开发环境的搭建(二)
通过自主选择相应的apache,mysql,php等软件,根据自己的应用开发需求进行安装.此方法搭建的环境自主性较强,搭建过程较为复杂,繁琐. 1.所需软件: Apache: http-2.2.22- ...
- Apache+MySQL+PHP开发环境的搭建(一)
通过套件来安装和配置php开发环境. 1.所需软件:AppServ(因为是开源,任何网站都能下载) 安装本软件基本上就是下一步. 2.进行安装 选择一个盘安装该软件点击next继续: 根据自己的实际情 ...
- Ubuntu 14.04下java开发环境的搭建--3--Tomcat及MySQL的安装
前面两篇文章,已经说明了JDK和Eclipse 的安装方法,下面简单说一下,Tomcat及MySQL的安装方法. Tomcat的安装. 在合适的地方解压apache-tomcat-6.0.39.tar ...
- Windows下Apache+PHP+MySQL开发环境的搭建(WAMP)
准备工作: 1.下载apache服务器安装包,官网http://www.apache.org/,下载地址:http://httpd.apache.org/download.cgi 2.下载MySQL, ...
- 使用 maven:archetype 创建JSF2 + EJB3.1 + JPA2项目骨架并在JBoss WildFly 8.1上部署
运行下面命令创建项目骨架: mvn archetype:generate -DarchetypeGroupId=org.jboss.spec.archetypes -DarchetypeArtifac ...
- Spring MVC开发环境的搭建和实例
一.安装jdk 二.安装tomcat 三.安装maven 新增环境变量MAVEN-HOME,并在path变量中添加bin路径 四.安装IntelliJ IDEA 五.创建maven web项目选择jd ...
- Ubuntu在下面LAMP(Linux+Apache+MySQL+PHP) 开发环境的搭建
LAMP在行业是一个非常流行的词.此4字母代表Linux.Apache,MySQL和PHP. LAMP其高效.灵活的特点已经成为中小企业的首选. 它已经推出了快速构建LAMP道路. 1 在Ubuntu ...
- PHP 开发环境的搭建和使用03-- 安装mySql
1/ 安装的MySQL版本是5.6.10版本的,直接点击Install 2/ 选择 Execute 3/ 更新最新版本成功后,选择 "next" 4/ 自定义安装方式,选择C ...
- 基于eclipse的java与mysql开发环境的搭建
本文主要介绍 Java与MySQL的连接 1.安装jdk 略~ 园子里有很多 2.安装mysql 略~ 3.下载并安装JDBC,通过mysql官网下载 http://dev.mysql.com/d ...
随机推荐
- php 禁止 URL 直接访问 php文件
通过判断访问来源来实现. $fromurl="http://www.111.com/index.php"; //只能从这个地址访问 if( $_SERVER['HTTP_REFER ...
- u-boot: Error: Can't overwrite "ethaddr"
When try to execute following command, It reports error as following: --->8--- U-Boot> setenv ...
- 潜水JVM
原文地址:http://blog.jamesdbloom.com/JVMInternals.html(转载请注明出处和本文地址英文原文) 本文简要解析JVM的内部结构.下图显示了一个典型的一块JVM( ...
- SharePoint Server 2010 删除Web应用
SharePoint Server 2010 删除Web应用 因为之前的测试.在SharePointserver创建于非常多Web应用(我是在本机Win7系统上安装的SharePoin ...
- Bulk Insert Data
Bulk Insert Data 命名空间:Oracle.DataAccess.Client 组件:Oracle.DataAccess.dll(2.112.1.0) ODP.NET 版本:ODP.NE ...
- Robotium原则的实施源代码分析
从前面的章节<Robotium源代码分析之Instrumentation进阶>中我们了解到了Robotium所基于的Instrumentation的一些进阶基础.比方它注入事件的原理等,但 ...
- Androidclient和server第一种方法端数据交换
网上有一个非常不同的情况来证明Android客户端和server如何结束的数据,但这些实施例大多相互作用更复杂,对于那些谁刚开始学习它是有害的,现在介绍一些简单的代码.逻辑清晰的互动样本,首先介绍本博 ...
- mysqldump: Couldn't execute 'show events': Cannot proceed because system tables used by Event Schedu
最近将老版本的mysql 实例倒入 percona 5.5.30,使用的是线上的全备,结果将mysql 库下的表也倒入了,这下可悲剧了,备份报错. 没办法,将mysql库下的数据倒出来,清空,再倒入p ...
- oracle错误之 ora-01017
ora-01017 现象描述: scott用户和其它建立的用户,都登的上.但sys和system用户登录不上 方案一(试过,不行): 1,打开目录:F:\app\Administrator\produ ...
- 牟大哥:《App自我促销》连载2
直立人迁移走
[谋哥每天一干货,第六十九篇] 前篇说到声音在远古时代.是一个奇妙的东西,它可以非常快地把信息传播到其它地方,突破了短距离. 然而能人的后代直立人学会了直立行走,他们開始走出非洲,到达遥远的中东.中国 ...