ssm 常用配置文件
SpringMVCIOC容器和SpringIOC容器关系SpringMVCIOC容器和SpringIOC容器作为父子关系容器,SpringMVC容器内的Bean可以访问父容器的Bean,反之Spring 中的Bean不能访问SpringMVC 子容器的Bean。1.Spring 通常管理除去@Controller 之外的Bean,SpringMVC 管理@Controller ...
·
SpringMVC IOC容器和Spring IOC容器关系
SpringMVC IOC容器和Spring IOC容器作为父子关系容器,SpringMVC容器内的Bean可以访问父容器的Bean,反之Spring 中的Bean不能访问SpringMVC 子容器的Bean。
1.Spring 通常管理除去@Controller 之外的Bean,SpringMVC 管理@Controller 的Bean,SpringMVC内的Bean可以使用@Autowire,@Resource注入Spring容器管理的Bean,反之不行。
2.要是为了快速方便开发,完全可以用SpringMVC框架
web.xml 配置SpringMVC servlet
<!-- 配置spring 容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- springMVC 前端控制器 -->
<servlet>
<servlet-name>spring-mvc</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>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 编码过滤器 解决post 乱码-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
配置Spring 容器
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:component-scan base-package="com.vincent.dao"></context:component-scan>
<context:component-scan base-package="com.vincent.service.impl"></context:component-scan>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/db_test" />
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-conf.xml" />
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置mybatis 包扫描接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.vincent.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
</bean>
<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
Mybatis配置文件
mybatis.xml mybatis配置文档
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 使用log4j作为日志-->
<setting name="logImpl" value="LOG4J"/>
</settings>
<typeAliases>
<package name="po" />
</typeAliases>
</configuration>
SpringMVC
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.vincent.controller"></context:component-scan>
<mvc:annotation-driven />
<!-- 使用默认的servlet 相应静态资源 -->
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp" />
</bean>
<!--全局请求异常处理 定义一个类实现HandlerExceptionResolver接口-->
<bean class="com.vincent.exception.handler.ExceptionHandler" />
<!-- 配置多媒体处理器 如上传图片 id必须是multipartResolver-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 限制上传图片大小 8M -->
<property name="maxUploadSize" value="8388608" />
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- /**拦截所有请求 -->
<!-- /* : 匹配一级,即 /add , /query 等
/** : 匹配多级,即 /add , /add/user, /add/user/user… 等
-->
<mvc:mapping path="/**" />
<bean class="com.vincent.filter.ResponseFilter" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
配置Log4j日志
log4j.properties
log4j.rootLogger = DEBUG,stdout
#console
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
#file
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File =logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} - [ %p ] %l%n%m%n
ssm 项目配置文件相对使用频繁,为提高开发效率做一个简单的记录方便复制粘贴。
更多推荐
已为社区贡献1条内容
所有评论(0)