Solo  当前访客:3 登录 注册

喧哗博客-http://blog.xuahua.com

繁华过后的沉寂--技术经验分享
浏览次数: 95,037    文章总数: 91    评论总数: 3
标签:

spring实战汇总

1,
    spring实战系列一MAVEN工程建立
2,
    spring实战之二maven文件配置


3,

    spring实战之三web配置

4,



    spring实战之四run



完整工程源码下载

如果你也需要jar包,此处也有lib打包下载



spring实战之四run 有更新!

前面讲了这么多,到了最关键的时候了,让他跑起来吧。

1,生成文件路径,java ,source,webapp各目录下的文件。




文件目录路径必须与applicationContext.xml ,spring-webmvc.xml中配置的一致。



至此,已经编译成功。可以在工程target目录下的 把war复制到运行容器中即可运行。

效果图如下

spring实战之三web配置 有更新!

1,除了需要配置pom.xml需要引用的jar包以外,还需要配置其他的配置文件;如web.xml,spring-mvc.xml,applicationContext.xml等。

现在一一详细解说

web.xml



<!-- spring初始化时,容器调用监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
 <!--  spring初始化时,引用文件工程目录下所有以applicationContext_开头的.xml,包括jar包中也会搜索进来。
    	classpath:applicationContext.xml 仅指工程classes目录下的applicationContext.xml;
    	classpath:applicationContext_*.xml 仅指工程classes目录下的applicationContext_开头的.xml文件;
    	classpath*:applicationContext_.xml 指工程classes目录下包括lib包中,包含有application_开头的.xml文件。	
    	 文件以及全局参数,此处默认了一个参数product来控制是否使用生产联接串或开发联接串-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>spring.profiles.default</param-name>
        <param-value>product</param-value>
    </context-param>
  <!-- 不多说了,编码过滤器 -->
    <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>
    </filter>

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 <!-- spring mvc的初始化,文件为classes目录下的 spring-webmvc.xml web请求处理参数,以及 -->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-webmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
  <!--MySQL数据库JNDI数据  可在程序手动lookup 到数据源-->
    <resource-ref>
       <description>xuahua</description>
       <res-ref-name>jdbc/xuahua</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
   </resource-ref>
 <!-- druid web监控使用,如果不需要监控,可不用 -->
   <servlet>  
        <servlet-name>DruidStatView</servlet-name>  
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>  
    </servlet>    
    <servlet-mapping>  
        <servlet-name>DruidStatView</servlet-name>  
        <url-pattern>/druid/*</url-pattern>  
    </servlet-mapping> 
        
以上为一般工程所需要的基本文件.


其中文件提到了  applicationContext.xml  spring-webmvc.xml

applicationContext.xml 内容如下

<!-- 加载属性文件,如果没有,可不使用 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>classpath:parameter.properties</value>
		</property>
	</bean>


<!-- Hibernate 联接配置 -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		
		<!-- 指定数据源 -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		
		<!-- 指定实体映射文件 -->
		<property name="packagesToScan">
			<list>
				<value>com.xuahua.model</value>
			</list>
		</property>
		
		<!-- 属性文件 -->
		<property name="hibernateProperties">
			<value>
				hibernate.dialect=org.hibernate.dialect.MySQLDialect
				hibernate.show_sql=true
				hibernate.format_sql=true
				hibernate.cache.use_second_level_cache=false
				hibernate.use_sql_comments=false
				hibernate.jdbc.batch_size=30
				hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
			</value>
		</property>
	</bean>
 <!-- hibernate事务管理 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
<!-- 事务切入点 -->
	<aop:config>
		<aop:pointcut id="crudMethodsCoreMaster"
			expression="execution(* com.xuahua.service..*.*(..))" />
		<aop:advisor advice-ref="txAdviceCoreMaster" pointcut-ref="crudMethodsCoreMaster" />
	</aop:config>
	
<!-- hibernate 代理事务,定义哪些service 开头的应该需要事务。哪些只需要只读事务 -->
	<tx:advice id="txAdviceCoreMaster" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="mng*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="create*" propagation="REQUIRED"
				rollback-for="Exception" />
			<tx:method name="save*" propagation="REQUIRED"
				rollback-for="Exception" />
			<tx:method name="update*" propagation="REQUIRED"
				rollback-for="Exception" />
			<tx:method name="insert*" propagation="REQUIRED"
				rollback-for="Exception" />
			<tx:method name="delete*" propagation="REQUIRED"
				rollback-for="Exception" />
			<tx:method name="do*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="load*" read-only="true" />
			<tx:method name="get*" read-only="true" />
			<tx:method name="list*" read-only="true" />
			<tx:method name="find*" read-only="true" />
			<tx:method name="view*" read-only="true" />
			<tx:method name="show*" read-only="true" />
			<tx:method name="valid*" read-only="true" />
			<tx:method name="query*" read-only="true" />
		</tx:attributes>
	</tx:advice>

		<!-- 生产数据源(JNDI)(生产环境、UAT环境、测试环境用) -->
	<beans profile="product">
		<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
			<property name="jndiName">
				<value>${jndiName}</value>
			</property>
		</bean>
	</beans>
<!-- druid web监控使用。 -->
	<beans>
		<bean id="druid-stat-interceptor"
			class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
		</bean>
	
	
		<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
			scope="prototype">
			<property name="patterns">
				<list>
					<value>com.xuahua.service.*</value>
				</list>
			</property>
		</bean>
	
		<aop:config>
			<aop:advisor advice-ref="druid-stat-interceptor"
				pointcut-ref="druid-stat-pointcut" />
		</aop:config>
 	</beans>


spring-mvc.xml文件内容如下

<!-- 定义自动代理 -->
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<!-- spring 定义扫描的包,对应工程路径.com.xuahua.controller;dao;service,意味着这三个包下面的所有类,spring会把注解的类名,方法名都会扫描进来 -->
	<context:component-scan base-package="com.xuahua">
		<context:include-filter type="regex"
			expression=".controller..*" />
		<context:include-filter type="regex"
			expression=".dao..*" />
		<context:include-filter type="regex"
			expression=".service..*" />
	</context:component-scan>
    
	<!-- 启动注解的方式 -->
	<context:annotation-config />  
	
 <!-- spring mvc 请求映射处理默认-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
    
    <!-- spring mvc Adapter 请求适配 转换json使用-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!-- 处理JSON数据转换的-->
                <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                    <property name="supportedMediaTypes">
                        <list>
                            <value>text/html;charset=UTF-8</value>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

	<!-- 使用默认的Servlet来响应静态文件-->
	<mvc:default-servlet-handler />
<!-- 视图解释类,定义文件路径,以及文件后缀。 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/" />
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 上传文件支持 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

完整文件下载地址  web.xml http://file.xuahua.com/images/blog/2016/0728/web.xml

                          applicationContext.xml  http://file.xuahua.com/images/blog/2016/0728/applicationContext.xml

                         spring-webmvc.xml   http://file.xuahua.com/images/blog/2016/0728/spring-webmvc.xml


spring实战之二maven文件配置

1,建立工程以后,就可以先运行pom.xml可以看到成功,并产生一个空的war包。 如何建工程?请见 spring实战之一maven工程建立

2, 打开编辑pom.xml

在根节点project下,dependencies 节点前添加以下内容

<modelVersion>4.0.0</modelVersion>
  <groupId>blog</groupId>
  <artifactId>com.xuahua</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>com.xuahua Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
  <!-- 定义本pom.xml引用版本属性, org.springframework-version,org.hibernate-version,project.build.sourceEncodeing 此名可随便取,必须与下面引用相匹配。 -->
  <properties>
		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
		<org.hibernate-version>4.3.1.Final</org.hibernate-version>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 </properties>
dependencies 节点内,可添加相关联的包

<!-- 下面是springframwork 4.xx 引用的包 -->
    <!-- spring 4 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${org.springframework-version}</version>
			<exclusions>
				<!-- Exclude Commons Logging in favor of SLF4j -->
				<exclusion>
					<groupId>commons-logging</groupId>
					<artifactId>commons-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-expression</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>
   	 <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${org.springframework-version}</version>
		</dependency>

Apache Commons 基础组件


<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.1</version>
		</dependency>
		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>1.8.3</version>
		</dependency>

		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-codec</groupId>
			<artifactId>commons-codec</artifactId>
			<version>1.8</version>
		</dependency>
		<dependency>
			<groupId>commons-configuration</groupId>
			<artifactId>commons-configuration</artifactId>
			<version>1.10</version>
		</dependency>
		
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.3.6</version>
		</dependency>
返回json串时,使用到的处理相关包 Jackson 

<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.5.1</version>
		</dependency>
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>1.9.13</version>
		</dependency>
slf4j 日志文件框架使用的jar包

<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-log4j12</artifactId>
			<version>1.7.2</version>
		</dependency>
mysql driver 数据库驱动包

	<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.15</version>
		</dependency>
 数据库联接池使用的

<!-- c3p0 datasource  数据库联接池使用的,此处是c3p0-->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<!-- druid 联接池,同时也提供内部默认的监控,个人推荐 -->
		 <dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.23</version>
		</dependency>
		<!-- dbcp使用的默认联接池  配合 commons-pool 使用-->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.5.4</version>
		</dependency>
		
		
		<!-- 中间件 ibatis -->
		<dependency>
			<groupId>org.apache.ibatis</groupId>
			<artifactId>ibatis-core</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.3</version>
		</dependency>
 hiberante 4

<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-core</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.hibernate</groupId>
			<artifactId>hibernate-validator</artifactId>
			<version>${org.hibernate-version}</version>
		</dependency>
		<dependency>
			<groupId>org.javassist</groupId>
			<artifactId>javassist</artifactId>
			<version>3.18.1-GA</version>
		</dependency>
		<!-- http-client -->
		<dependency>
			<groupId>commons-httpclient</groupId>
			<artifactId>commons-httpclient</artifactId>
			<version>3.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.3.3</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.3.3</version>
		</dependency>
		
其他可以用到的

<!-- AspectJ -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.7.4</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.7.4</version>
		</dependency>
		<!-- json-lib -->
		<dependency>
			<groupId>net.sf.json-lib</groupId>
			<artifactId>json-lib</artifactId>
			<version>2.4</version>
			<classifier>jdk15</classifier>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.7</version>
		</dependency>
		<!-- memcached -->
		<dependency>
			<groupId>com.whalin</groupId>
			<artifactId>Memcached-Java-Client</artifactId>
			<version>3.0.2</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.maven-play-plugin.spy</groupId>
			<artifactId>spymemcached</artifactId>
			<version>2.6</version>
		</dependency>
		
		<!-- servlet3 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.0.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>servlet-api</artifactId>
			<version>2.3</version>
			<scope>provided</scope>
		</dependency>

完整的文件可以下载地址 http://file.xuahua.com/images/blog/2016/0728/pom.xml

spring实战系列一MAVEN工程建立 有更新!

1,前提,maven工程需要的nexus 另外自己搭建;

2,eclipse开发工具流程


2.1 选择maven project

maven-喧哗博客


 指定工程目录


maven-喧哗博客

 建立maven工程对应配置

maven-喧哗博客

maven pom.xml文件指定的groupid


点 Fisnish 。至此 maven工程已经建立起来了。。如果需要运行,请见spring实战系列二配置

liunx 下maven的安装布署

1.首先到Maven官网下载安装文件,目前最新版本为3.0.5,下载文件为apache-maven-3.0.5-bin.tar.gz

下载可以使用wget命令;

wget http://mirror.bit.edu.cn/apache/maven/binaries/apache-maven-3.0.3-bin.tar.gz 


2.进入下载文件夹,找到下载的文件,运行如下命令解压

tar -xvf  apache-maven-3.0.5-bin.tar.gz


解压后的文件夹名为apache-maven-3.0.5

3.使用mv命令将apache-maven-3.0.5文件夹拷贝到自己指定的文件夹,比如/usr/local/下

mv -rf apache-maven-3.0.5 /usr/local/


4.配置环境变量,编辑/etc/profile文件,添加如下代码

MAVEN_HOME=/usr/local/apache-maven-3.0.5
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin

【注:此处需要使用root权限】


5.保存文件,并运行如下命令使环境变量生效
source /etc/profile

6.在控制台输入如下命令,如果能看到Maven相关版本信息,则说明Maven已经安装成功
mvn -v

公告

喧哗博客--繁华过后的沉寂--技术经验分享^-^
Copyright (c) 2009-2019, b3log.org & hacpai.com