Spring+SpringMVC+Hibernate+Shiro框架

首先,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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name />
	
	<!-- 配置首页 -->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
	<!-- 配置监听 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-*</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 编码过滤器 -->
	<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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- Shiro配置 -->
	<filter>
		<filter-name>shiroFilter</filter-name>
		<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
		<init-param>
			<param-name>targetFilterLifecycle</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>shiroFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- Hibernate配置 -->
	<filter>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>OpenSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- SpringMVC配置 -->
	<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:SpringMvc.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>
	
	<!-- 配置日志 -->
  <filter>
    <filter-name>LogFilter</filter-name>
    <filter-class>
		com.medk.filter.LogFilter
	</filter-class>
    <init-param>
      <param-name>LogFilter</param-name>
      <param-value>AUTO</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>LogFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

spring-Common.xml配置:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
	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.2.xsd 
						http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
						http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
						http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring   
     					http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.2.xsd">

	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath:localinfo.properties</value>
			</list>
		</property>
	</bean>
	
</beans>

localinfo.properties配置:

jdbc.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/XXXX
mysql.username=root
mysql.password=xxxxxx

hibernate.base=com/medk/hibernate

spring-Db配置:

<?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:context="http://www.springframework.org/schema/context"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"
	xsi:schemaLocation="http://www.springframework.org/schema/beans	http://www.springframework.org/schema/beans/spring-beans-3.2.xsd	 					
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd							 					
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd	 http://www.springframework.org/schema/tx 						
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd       
http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd">

	<context:component-scan base-package="com.medk.service, com.medk.dao" />

	<!-- 数据源 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
		init-method="init" destroy-method="close">
		<property name="url" value="${mysql.url}" />
		<property name="username" value="${mysql.username}" />
		<property name="password" value="${mysql.password}" />
		<property name="initialSize" value="3" />
		<property name="maxActive" value="30" />
		<property name="minIdle" value="6" />
	</bean>

	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="mappingDirectoryLocations">
			<!-- 在这里添加配置文件 -->
			<list>
				<value>classpath:${hibernate.base}</value>
			</list>
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.use_sql_comments">false</prop>
				<prop key="hibernate.format_sql">false</prop>
				<prop key="hibernate.hbm2ddl.auto">none</prop>
			</props>
		</property>
	</bean>

	<!-- 定义事务 -->
	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	
	<!-- 常量配置文件导入 -->
	<bean id="constKey" class="com.medk.common.Constant">
		<property name="HOME" value="${root}" />
	</bean>
	
</beans>

spring-shiro.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"  
    > 
    
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
        <property name="realm" ref="shiroDbRealm" /> 
        <property name="cacheManager" ref="cacheManager" />  
    </bean>  
  	
  	  <!-- 会话管理器 -->
	<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
  	    <!-- 设置超时时间 -->
	    <property name="globalSessionTimeout" value="28800000"/>
	</bean>
  	
    <!-- 項目自定义的Realm -->  
    <bean id="shiroDbRealm" class="com.medk.common.ShiroRealm">  
        <property name="cacheManager" ref="cacheManager" />  
    </bean>  
  
    <!-- Shiro Filter -->  
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        <property name="securityManager" ref="securityManager" />  
        <property name="loginUrl" value="/front/index.ftl" />  
        <property name="successUrl" value="/main.ftl" />  
        <property name="unauthorizedUrl" value="/error.ftl" />  
        <property name="filterChainDefinitions">  
            <value>  
                /login/* = anon
                /front/* = anon
                /fonts/* = anon
                /css/* = anon
                /js/* = anon
                /images/* = anon
            	/** = authc
            </value>  
        </property>  
    </bean>  
    <!-- 用户授权信息Cache -->  
    <bean id="cacheManager" class="org.apache.shiro.cache.MemoryConstrainedCacheManager" />  
  
    <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->  
    <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />  
  	 <!-- AOP式方法级权限检查 -->  
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"  
        depends-on="lifecycleBeanPostProcessor">  
        <property name="proxyTargetClass" value="true" />  
    </bean>  
  
    <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">  
        <property name="securityManager" ref="securityManager" />  
    </bean>  
   </beans>

SpringMvc.xml配置:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<context:component-scan base-package="com.medk.controller"></context:component-scan>

	<mvc:annotation-driven>
	<mvc:message-converters>
			<bean class="org.springframework.http.converter.StringHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8</value>
						<value>text/plain;charset=UTF-8</value>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>
	
	<!-- 多文件上传配置 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
         <property name="maxUploadSize" value="104857600" />
         <property name="maxInMemorySize" value="4096" />
         <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

	<!-- freemarker视图配置 -->
	<bean
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
		<property name="cache" value="true"/>
		<!--前缀 -->
		<property name="prefix" value="" />
		<!--后缀 -->
		<property name="suffix" value=".ftl" />
		<property name="contentType" value="text/html;charset=UTF-8"></property>  
        <property name="requestContextAttribute" value="request" />  
        <property name="exposeSpringMacroHelpers" value="true" />  
        <property name="exposeRequestAttributes" value="true" />  
        <property name="exposeSessionAttributes" value="true" />
	</bean>
	
	<!-- freemark 配置 -->
	<bean id="freemarkerConfig"
		class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

		<property name="templateLoaderPath" value="/WEB-INF/template"></property>
		<property name="freemarkerSettings">
			<props>
				<prop key="tag_syntax">auto_detect</prop>
				<prop key="template_update_delay">1</prop>
				<prop key="defaultEncoding">UTF-8</prop>
				<prop key="url_escaping_charset">UTF-8</prop>
				<prop key="locale">zh_CN</prop>
				<prop key="boolean_format">是,否</prop>
				<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
				<prop key="date_format">yyyy-MM-dd</prop>
				<prop key="time_format">HH:mm:ss</prop>
				<prop key="number_format">0.######</prop>
				<prop key="whitespace_stripping">true</prop>
				<!--空值处理 -->
				<prop key="classic_compatible">true</prop>
				<prop key="template_exception_handler">com.medk.exception.FreemarkerExceptionHandler</prop>
			</props>
		</property>
	</bean>      
		
		<!-- 其他配置 -->
		<bean id="httpRequestHandlerAdapter"
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter">
		</bean>
	 	<bean id="urlMapping"
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
		<property name="defaultHandler" ref="httpRequestHandlerAdapter"></property>
	 	</bean>
	  	
		<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
		<mvc:resources location="/images/" mapping="/images/**"></mvc:resources>
		<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
		<mvc:resources location="/easyui/" mapping="/easyui/**"></mvc:resources>
		<mvc:resources location="/fonts/" mapping="/fonts/**"></mvc:resources>
		<mvc:resources location="/kindeditor/" mapping="/kindeditor/**"></mvc:resources>
</beans>

环境:JDK1.7

顺便提一下,里面整合了freemarker和kindeditor,原原本本搬上去绝对会报错的,你首先要有jar包,这个问题不大,做过开发的里面的jar包基本都有,其次,配置里面有几个类这里没写上来,比如spring-shiro.xml里面配置的ShiroRealm类等,嘛,授人予鱼不如授人予渔,理解了才有用,照搬没啥意思!自行补齐jar包和类,项目跑起来绝对没问题的!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值