您的位置:首页 > 编程语言 > Java开发

springmvc注解开发

2016-06-03 16:08 531 查看

关于SpringMvc+spring+hibernate 注解开发的配置文件

web.xml中的配置


<!--放置顺序 (有时候会导致有点小错误但不影响运行,我这人有强迫症所以都是按顺序放置的)
(icon?,display-name?,description?,distributable?,context-param*,filter*,filter-mapping*,
listener*,servlet*,servlet-mapping*,session-config?,mime-mapping*,welcome-file-list?,
error-page*,taglib*,resource-env-ref*, resource-ref*,security-constraint*,login-config?,
security-role*, env-entry*,ejb-ref*,ejb-local-ref*)-->
<web-app>

<display-name>Archetype Created Web Application</displayname>

<!-- Spring config文件 注册多个配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring.xml,
classpath:spring-hibernate.xml
</param-value>
</context-param>

<!--字符编码设置-->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 使用监听器引入spring框架 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- spring mvc config文件 -->
<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-mvc.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>

<!--指明下面文件不需要springMVC过滤-->
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.gif</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.ico</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.png</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>logion.jsp</welcome-file>
</welcome-file-list>

<error-page>
<error-code>500</error-code>
<location>/view/error.jsp</location>
</error-page>

<error-page>
<error-code>404</error-code>
<location>/view/error.jsp</location>
</error-page>

</web-app>


springMvc配置文件中的配置


<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 
<!-- spring mvc 自动扫描 -->
<context:component-scan base-package="com.men" />
<!--aop注解配置-->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!--视图解析器-->
<bean id="viewResoler" class="org.springframework.web.servlet.view.UrlBasedViewResolver" >
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<!--视图解析器标识。如果有多个的话表示此解析器-->
<!--<property name="viewNames" value=""/>-->
<property name="prefix" value="/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--json转换配置-->
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"/></bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!--自定义拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/"/>
<bean class="com.men.fillter.MyFillter"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>


spring-hibernate.xml

<!--这里面配置了与持久层dao相关的-->
<?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:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"> <!--读取配置文件-->
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="root" />
<!--驱动会根据url自动识别-->
<property name="password" value="123456" />
<property name="initialSize" value="0" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
</bean>
<!-- hibernate session -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<!--缓存配置-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
</props>
</property>
<!-- autoscan annotation for hiberante config -->
<property name="packagesToScan">
<list>
<value>com.men.model</value>
</list>
</property>
</bean>
<!--开启注解缓存-->
<cache:annotation-driven/>
<!--缓存的bean-->
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager"><ref local="ehcache"/></property>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" p:configLocation="classpath:ehcache.xml"/>
</beans>


spring.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.xsd"> <!--验证码配置-->
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha" >
<property name="config">
<bean class="com.google.code.kaptcha.util.Config">
<constructor-arg>
<props>
<prop key="kaptcha.border">yes</prop>
<prop key="kaptcha.border.color">105,179,90</prop>
<prop key="kaptcha.textproducer.font.color">blue</prop>
<prop key="kaptcha.image.width">100</prop>
<prop key="kaptcha.image.height">40</prop>
<prop key="kaptcha.textproducer.font.size">30</prop>
<prop key="kaptcha.session.key">code</prop>
<prop key="kaptcha.textproducer.char.length">4</prop>
<prop key="kaptcha.textproducer.font.names">宋体,楷体</prop>
</props>
</constructor-arg>
</bean>
</property>
</bean>
</beans>


ehcache.xml hibernate二级缓存需要的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<ehcache>
<diskStore path="c:\\ehcache\"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!-- 设置Category类的缓存的数据过期策略 -->
<cache name="org.qiujy.domain.cachedemo.Category"
maxElementsInMemory="100"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/>
<!-- 设置Category类的products集合的缓存的数据过期策略 -->
<cache name="org.qiujy.domain.cachedemo.Category.products"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="org.qiujy.domain.cachedemo.Product"
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<cache name="workUnitHisCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="2400"
timeToLiveSeconds="2400"
overflowToDisk="false"/>

</ehcache>


config.properties

hibernate.dialect=org.hibernate.dialect.MySQLDialect
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://127.0.0.1:3306/schoolmanagementsystem
jdbc_username=root
jdbc_password=123456
hibernate.hbm2ddl.auto=update
hibernate.show_sql=true
hibernate.format_sql=false
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息