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>

创建如下图所示的目录结构:

下面是各个配置文件的完整内容。

通过 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 构建工程,将 shop.war 复制到 JBoss 部署目录下即可。

Spring-MVC4 + JPA2 + MySql-5.5 + SLF4J + JBoss WildFly-8.1开发环境搭建

时间: 2024-10-17 13:16:47

Spring-MVC4 + JPA2 + MySql-5.5 + SLF4J + JBoss WildFly-8.1开发环境搭建的相关文章

spring mvc4.1.6 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明

一.准备工作 开始之前,先参考上一篇: struts2.3.24 + spring4.1.6 + hibernate4.3.11 + mysql5.5.25 开发环境搭建及相关说明 思路都是一样的,只不过把struts2替换成了spring mvc 二.不同的地方 工程目录及jar包: action包改成controller: 删除struts2 jar包,添加spring mvc包(已有的话,不需添加):     web.xml配置: 跟之前不同的地方是把struts2的过滤器替换成了一个se

使用 maven:archetype 创建JSF2 + EJB3.1 + JPA2项目骨架并在JBoss WildFly 8.1上部署

执行以下命令创建项目骨架: mvn archetype:generate -DarchetypeGroupId=org.jboss.spec.archetypes -DarchetypeArtifactId=jboss-javaee6-webapp-ear-archetype -DarchetypeVersion=7.1.1.Final -DgroupId={你的groupId} -DartifactId={你的artifactId} -Dversion=1.0-SNAPSHOT 项目生成完毕后

spring boot 开发环境搭建(Eclipse)

Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 spring boot 连接Mysql spring boot配置druid连接池连接mysql spring boot集成mybatis(1) spring boot集成mybatis(2) – 使用pagehelper实现分页 spring boot集成mybatis(3) – mybatis ge

spring mvc(一)开发环境搭建和HelloWorld程序

Spring MVC 3提供了基于注解.REST风格等特性,有些方面比Struts 2方便一些. 这里进行Spring MVC 3的开发环境搭建,即开发Hello World程序. 1,拷贝Spring MVC 3类库到WEB-INF/lib下,经测试至少需要如下几个,版本为Spring 3.1.1: org.springframework.asm-3.1.1.RELEASE.jar org.springframework.beans-3.1.1.RELEASE.jar org.springfr

spring mvc(三)开发环境搭建和HelloWorld程序

Spring MVC响应中返回JSON数据的方法: 配置与以前相同使用<mvc:annotation-driven/>的注解配置, 但WEB-INF/lib的类路径里面要有jackson-all-1.6.9.jar这个库文件, 然后在controller里面这样写: @Controller @RequestMapping("/user" ) public class UserController { @RequestMapping("/ajax2" )

spring mvc(二)开发环境搭建和HelloWorld程序

Spring MVC3在controller和视图之间传递参数的方法: 一, 从controller往视图传递值, controller---->视图 1)简单类型,如int, String,直接写在controller方法的参数里,是无法传递到视图页面上的(经测试). (而用@RequestParam("name")注解,可以从视图上,或地址中加?name=***传递到controller方法里) 2)可以用Map<String, Object>,其键值可以在页面上

Spring开发环境搭建教程

Spring开发环境搭建 JDK7以上版本 eclispe for j2ee 4.0以上版本 Spring frameWorks 3.0以上版本 至于前两个我们就不介绍,直接百度就可以了,对于Spring FrameWork的下载链接比较难找. Spring frameWorks 3.0以上版本下载步骤 1.首先打开链接Spring官方网站 2.然后 点击最新版本号的Referrence链接进入 3. 选择Distribution Zip Files这一项. 4. 点击这个链接进入,进入真正的下

Spring 开发环境搭建

为了方面,直接使用eclipse,创建maven工程,创建成功之后 一.修改pom.xml,为了方面我就把Spring相关的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

JavaEE开发环境搭建(2)---(MySQL的安装)

上次写了JavaEE开发环境搭建之jdk的安装...然后自己看的时候感觉有时候加载图片不是很流畅,所以这次就少添加点图片了~~ JavaEE开发环境搭建之MySQL的安装 1:mysql 的下载 在百度里搜mysql 进入官网 (http://www.mysql.com/) 然后在网址后面加入--> downloads/mysql 然后网页就会跳转到mysql下载页面(http://dev.mysql.com/downloads/mysql/)(你直接复制这里也可以~~前面只是告诉你怎么找到下载