SSM框架搭建 --- 超详细 (2)

Spring AOP 日志管理

<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.18.RELEASE</version> </dependency> 以上便是搭建SSM所需要的基本依赖

编写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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee " version="3.0"> <display-name>Archetype Created Web Application</display-name> <!-- 定义Spring监听器,加载spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 编码过滤器 --> <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>forceRequestEncoding</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>forceResponseEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- springMVC核心配置 --- 注意:servlet-name里面填写的如果是dispatcherServlet 就要在同级目录创建dispatcherServlet-servlet.xml作为Spring-mvc的配置文件 --> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- 拦截设置 --> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

编写Spring-MVC配置文件 --->dispatcherServlet-servlet.xml

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" 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" xsi:schemaLocation=" "> <!--SpringMVC的配置文件 ---- 包含网站跳转逻辑的控制,配置--> <!-- 扫描所有包 --> <context:component-scan base-package="cbuc.ssm" use-default-filters="false"> <!--只扫描控制器controller--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 能支持SpringMVC更高级的一些功能, JSR303校验,快捷的ajax请求...来映射动态请求 --> <mvc:annotation-driven/> <!-- 将springMVC不能处理的资源交给Tomcat --> <mvc:default-servlet-handler/> <!-- 对模型视图添加前后缀 --> <bean p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/> <!-- 配置json转换器 --> <bean> <property> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </beans>

编写Spring配置文件,在src/main/resource目录下创建applicationContext.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/aop "> <!--Spring的配置文件,这里主要配置和业务逻辑有关的--> <!--Spring配置文件的核心点(数据源,与mybatis的整合,事务控制)--> <!--扫描包下的所有组件除了controller--> <context:component-scan base-package="cbuc.ssm"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 配置数据库相关参数properties的属性:${url} --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- ===================================数据库连接池=============================== --> <!-- ===================在src/main/resource目录下创建jdbc.properties====================== --> <bean> <property value="${jdbc.driver}"/> <property value="${jdbc.url}"/> <property value="${jdbc.username}"/> <property value="${jdbc.password}"/> <property value="${c3p0.maxPoolSize}"/> <property value="${c3p0.minPoolSize}"/> <property value="${c3p0.autoCommitOnClose}"/> <property value="${c3p0.checkoutTimeout}"/> <property value="${c3p0.acquireRetryAttempts}"/> </bean> <!--====================================================================================--> <!-- ==================================配置和mybatis的整合============================= --> <bean> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property value="classpath:mybatis-config.xml" /> <!-- 注入数据库连接池 --> <property ref="dataSource"/> <!-- 扫描sql配置文件:mapper需要的xml文件 --> <property value="classpath:mapper/*.xml"/> </bean> <!--配置扫描器,将mybatis接口的实现加入到ioc容器中--> <bean> <!-- 给出需要扫描Mapper接口包 --> <property value="cbuc.ssm.mapper" /> <!-- 注入sqlSessionFactory --> <property value="sqlSessionFactory"/> </bean> <!--====================================================================================--> <!-- =====================================事务控制的配置================================= --> <bean> <!-- 控制住数据源 --> <property ref="dataSource"/> </bean> <aop:config> <!--切入点表达式--> <aop:pointcut expression="execution(* cbuc.ssm.service..*(..))"></aop:pointcut> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/> </aop:config> <!--配置事务增强,事务如何切入--> <tx:advice> <tx:attributes> <!--所有方法都是事务方法--> <tx:method/> <!--以get开始的所有方法--> <tx:method read-only="true"/> </tx:attributes> </tx:advice> <!--====================================================================================--> </beans>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwjydg.html