Spring-mvc4 + JPA2 + MySql-5.5 + slf4j + JBoss WildFly-8.1 development environment Setup

Source: Internet
Author: User
Tags jboss wildfly log4j

As a result of the interview was asked Spring4, so intends to the past Spring3 project all upgraded to Spring4. The environmental construction process is now recorded here.


First create the project skeleton using Maven archetype and execute the following command:

MVN archetype:generate-darchetypegroupid=org.apache.maven.archetypes-darchetypeartifactid= maven-archetype-webapp-darchetypeversion=1.0-dgroupid={your GroupID}-dartifactid={your Artifactid}-Dversion=1.0- SNAPSHOT

When finished, it generates the most basic directory structure, and a pom.xml that contains junit. This is obviously not enough. We need to modify pom.xml to add the required dependencies:

<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><!--support File Upload--><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>

Then change the Java version to 1.7: (Spring4 minimum requirement is 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 >


Create the directory structure as shown:



The following is the complete contents of each configuration file.


JBoss does not use the built-in log system via Jboss-deployment-structure.xml command:


To dynamically register data sources with JBoss at deployment time by Shop-ds.xml:


Because we use Servet 3.0, the Web. XML also needs to be upgraded accordingly:

<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>

Configure JPA, Hibernate, data source Jndi lookups, and more via spring configuration file Servlet-context.xml:

<?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.xsdhttp://www.springframework.org/schema/ MVC Http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans/HTTP WWW.SPRINGFRAMEWORK.ORG/SCHEMA/BEANS/SPRING-BEANS.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/HTTP Www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context.xsd "><!--dispatcherservlet Context:defines This Servlets ' 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 D Irectory--><resources mapping= "/resources/**" location= "/resources/"/><!--Resolves views selected for Rendering by @Controllers to. JSP resources in The/web-inf/views directory--><beans:beanclass= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><beans:property name=" prefix "value="/ web-inf/views/"/><beans:property name=" suffix "value=". jsp "/></beans:bean><!--Container Management 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><!--Open 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:beanclass= " Org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor "/><!--transaction--><beans:bean class= "Org.springframework.orm.jpa.JpaTransactionManager" id= "transactiOnmanager "><beans:property name=" entitymanagerfactory "ref=" EMF "/></beans:bean><!--file uploaded-- <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>

The Src/main/resources/log4j.xml configuration is as follows:

<?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 "/&GT;&LT;/LOGGER&GT;&Lt;! --Root Logger--><root><priority value= "Debug"/><appender-ref ref= "Console"/></root> </log4j:configuration>

At this point all OK. Execute the MVN clean install build project and copy the Shop.war to the JBoss deployment directory.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.