1. SPRING aop入门

Aop  面向切面编程

在一个大型的系统中,会写很多的业务类--业务方法

同时,一个大型的系统中,还有很多公共的功能:比如事务管理、日志处理、缓存处理.....

1.1. 动态代理机制复习

1.1.1. 动态代理编程实例

工程结构如下:

1.1.1.1. 原业务接口TestService

  1. public interface TestService {
  2.  
  3. public String lababa(String babaleixing);
  4. public String chifan(String shenmefan);
  5. }

1.1.1.2. 原业务类TestServiceImpl

  1. public class TestServiceImpl implements TestService{
  2.  
  3. @Override
  4. public String lababa(String babaleixing) {
  5.  
  6. System.out.println("脱虎皮裙了。。。。");
  7. System.out.println("撅屁股了。。。。");
  8. System.out.println("开拉了。。。。" +babaleixing);
  9. System.out.println("擦屁屁了");
  10.  
  11. return "拉完了";
  12. }
  13. @Override
  14. public String chifan(String shenmefan) {
  15.  
  16. System.out.println("上桌。。。。。");
  17. System.out.println("抓饭。。。。。");
  18. System.out.println("开吃。。。。。" +shenmefan);
  19. System.out.println("擦嘴嘴。。。。。。");
  20.  
  21. return "吃完了";
  22. }
  23.  
  24. }

1.1.1.3. 动态代理的测试类

ProxyTest

  1. public class ProxyTest {
  2.  
  3. public static void main(String[] args) {
  4. // 首先获取原业务对象的动态代理对象,并且定义动态代理对象中的增强处理逻辑
  5. TestService testServiceImpl = (TestService) Proxy.newProxyInstance(TestServiceImpl.class.getClassLoader(), TestServiceImpl.class.getInterfaces(), new InvocationHandler() {
  6. /**
  7. * 代理对象被调用时的真正处理逻辑所在
  8. */
  9. @Override
  10. public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
  11.  
  12. //前置增强
  13. System.out.println("开启事务...........");
  14.  
  15. //调用原业务对象的原方法
  16. TestServiceImpl testService = new TestServiceImpl();
  17. // 根据代理对象被调用的方法,去调用相应的原业务对象的原方法
  18. String name = method.getName();
  19. System.out.println("当前被调用的是这个方法: " + name);
  20. //调用一下原业务对象的该方法
  21. Object invokeResult = method.invoke(testService, args);
  22.  
  23. //后置增强
  24. System.out.println("提交事务...........");
  25.  
  26. return invokeResult;
  27. }
  28. });
  29.  
  30. // 调用一下动态代理对象的业务方法
  31. String lababa = testServiceImpl.lababa("稀粑粑");
  32. System.out.println("动态代理对象invoke完毕,结果为:" + lababa);
  33. String chifan = testServiceImpl.chifan("满汉全席");
  34. System.out.println("动态代理对象invoke完毕,结果为:" + chifan);
  35.  
  36. }
  37. }

1.2. aspectJ面向切面编程(模拟实现)

1.2.1. AOP编程框架简介

aspectJ是一个AOP组织提供的面向切面编程框架

它的使用方法是:

1/ 用户自己开发自己的业务类和业务方法

2/ 用户自己开发自己的增强逻辑,增强逻辑可以写在一个普通Advice类中

3/ 配置一个配置文件,告诉aspectJ,为哪些业务方法增加哪些增强逻辑

1.2.2. AOP框架编程示例

示例如下:

1.2.2.1. 用户自己的原业务接口

  1. public interface UserService {
  2. User findUserById(int id);
  3. User findUserByName(String name);
  4. }

1.2.2.2. 用户自己的原业务类

  1. public class UserServiceImpl implements UserService {
  2.  
  3. @Override
  4. public User findUserById(int id){
  5. System.out.println("执行原方法findUserById.......");
  6. User user = new User();
  7. user.setId(id);
  8. return user;
  9. }
  10.  
  11. @Override
  12. public User findUserByName(String name){
  13. System.out.println("执行原方法findUserByName.......");
  14. User user = new User();
  15. user.setUsername(name);
  16. return user;
  17. }
  18. }

1.2.2.3. 用户提供的增强逻辑类

  1. public class MyAdvice {
  2.  
  3. public void before(JoinPoint joinPoint){
  4. System.out.println("前增强处理。。。。。。");
  5. }
  6.  
  7. public void after(JoinPoint joinPoint){
  8. System.out.println("后增强处理。。。。。。");
  9. }
  10. }

1.2.2.4. AOP切面配置文件

  1. <bean id="userService" class="cn.dohit.ssm.aopservice.service.impl.UserServiceImpl" />
  2. <bean id="myadvice" class="cn.dohit.ssm.aop.MyAdvice"></bean>
  3. <aop:config>
  4. <aop:aspect ref="myadvice">
  5. <aop:pointcut expression="execution(* cn.dohit.ssm.aopservice.service.*.*(..))"
  6. id="mypoint" />
  7. <aop:before method="before" pointcut-ref="mypoint" />
  8. </aop:aspect>
  9.  
  10. </aop:config>

1.2.2.5. 测试类

  1. public class test {
  2. public static void main(String[] args) {
  3. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-aop.xml");
  4. // 拿到的这个bean不是原业务类的对象,而是一个动态代理对象
  5. UserService bean = (UserService) context.getBean("userService");
  6. // 调用业务方法时,走的是代理对象中的处理逻辑
  7. User findUserById = bean.findUserById(100);
  8. System.out.println(findUserById.getId());
  9.  
  10. User findUserByName = bean.findUserByName("张三");
  11. System.out.println(findUserByName.getUsername());
  12.  
  13. }
  14.  
  15. }

2. SSM整合

整合目标:控制层采用springmvc、持久层使用mybatis实现。

各层的BEAN都交给spring管理

本质: 让mybatis层的Mapper类的对象交给spring来构造,在service层的类中需要Mapper对象时,直接从spring注入即可

整个项目的各种业务逻辑的数据库事务管理,也交给spring来处理(aop)

2.1. 需求

实现商品查询列表,从mysql数据库查询商品信息。

2.2. jar包

包括:spring(包括springmvc)、mybatis、mybatis-spring整合包、数据库驱动、第三方连接池。

参考:“mybatis与springmvc整合全部jar包”目录

2.3. 工程搭建

2.3.1. 整合思路

Dao层:

1、SqlMapConfig.xml,空文件即可。需要文件头。

2、applicationContext-dao.xml。

a) 数据库连接池

b) SqlSessionFactory对象,需要spring和mybatis整合包下的。

c) 配置mapper文件扫描器。

Service层:

1、applicationContext-service.xml包扫描器,扫描@service注解的类。

2、applicationContext-trans.xml配置事务。

表现层:

Springmvc.xml

1、包扫描器,扫描@Controller注解的类。

2、配置注解驱动。

3、视图解析器

Web.xml

配置前端控制器。

2.3.2. 整合步骤

2.3.2.1. 导入三大框架的所有jar包

2.3.2.2. 建各类配置文件

2.3.2.3. 配置spring应用容器的方式

1、修改web.xml——关键点:在web项目中如何启动spring的applicationContext容器

思路:

由于整个工程中所有的controller对象,service对象,dao对象都交给spring框架来构造

但是web工程中,并没有一个main方法来启动spring的applicationContext容器

所以,我们需要让tomcat在启动的时候去启动spring的applicationContext容器

而spring中有两个类(DispatcherServlet和ContextListener)可以被tomcat所加载,并在初始化时创建spring的应用容器

从而,具体做法上有两种:

第一种:用两个类去创建两个应用容器来构造项目中的类

把controller对象交给DispatcherServlet去构造

把service和dao层的对象交给ContextListener去构造

DispatcherServlet加载springmvc.xml文件,该文件中只扫描controller层的包

ContextListener加载applicationContext-service.xml,applicationContext-dao.xml两个配置文件,两个配置文件中分别配置service层和dao层的对象构造

在这种方法中,整个项目运行时,会存在两个spring的容器,这两个容器有一个父子关系:

DispatcherServlet的容器是子容器

ContextListener的容器是父容器

子容器可以获取父容器中的对象

第二种:用一个容器去加载项目中类

把controller对象、service对象、dao对象全都交给DispatcherServlet去构造

具体做法就是让DispatcherServlet加载所有的bean配置文件

2.3.2.4. 配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>ssm-integration</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. </welcome-file-list>
  10.  
  11. <!-- 加载spring容器 -->
  12. <!--
  13. <context-param>
  14. <param-name>contextConfigLocation</param-name>
  15. <param-value>classpath:spring/applicationContext-service.xml</param-value>
  16. </context-param>
  17. <listener>
  18. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  19. </listener>
  20. -->
  21. <servlet>
  22. <servlet-name>springmvc</servlet-name>
  23. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  24. <init-param>
  25. <param-name>contextConfigLocation</param-name>
  26. <param-value>classpath:springmvc/springmvc.xml</param-value>
  27. </init-param>
  28. </servlet>
  29. <servlet-mapping>
  30. <servlet-name>springmvc</servlet-name>
  31. <url-pattern>*.action</url-pattern>
  32. </servlet-mapping>
  33. </web-app>

2.3.2.5. 整合service层

A、可以让dispatcherServlet创造的容器来构建service层对象

B、也可以让ContextListener创造的容器来构建service层对象

C、修改applicationContext-service.xml文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10.  
  11. <!-- 组件扫描父包路径 -->
  12. <context:component-scan base-package="com.dohit.ssm.service" />
  13.  
  14. </beans>

2.3.2.6. 整合测试controller+service

1、开发一个service层的测试类:

TestServiceImple.java

  1. @Service
  2. public class TestServiceImpl implements TestService {
  3.  
  4. @Override
  5. public String helloService(String name) {
  6.  
  7. return name+" sb";
  8. }
  9.  
  10. }

2、开发表现层controller类:

写一个controller方法,在方法中调用TestServiceImpl中的一个方法

然后在页面上请求一下这个controller的方法

TestController.java

  1. @Controller
  2. public class TestController {
  3.  
  4. @Autowired
  5. private TestService testService;
  6.  
  7. @RequestMapping("/hello")
  8. @ResponseBody
  9. public String hello(String name){
  10. return "你好:"+name;
  11. }
  12.  
  13. @RequestMapping("/hello2")
  14. @ResponseBody
  15. public String hello2(String name){
  16. String res = testService.helloService(name);
  17. return "hello: "+res;
  18. }
  19. }

2.3.2.7. 整合dao层

核心思想:让spring去帮我们构造一个sqlsessionfactory,并且让它去自动扫描mapper接口和xml文件,生成mapper接口的实例对象

做法:

A、在applicationContext-dao.xml中,配置sqlsessionfactory的bean

B、先配置一个连接池bean

classpath:mybatis/db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root

2.3.2.7.1. applicationContext-dao.xml中

  1. <!-- 加载properties配置文件 -->
  2. <context:property-placeholder location="classpath:mybatis/db.properties" />
  3. <!-- 数据库连接池 -->
  4. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  5. destroy-method="close">
  6. <property name="driverClassName" value="${jdbc.driver}" />
  7. <property name="url" value="${jdbc.url}" />
  8. <property name="username" value="${jdbc.username}" />
  9. <property name="password" value="${jdbc.password}" />
  10. <property name="maxActive" value="10" />
  11. <property name="maxIdle" value="5" />
  12. </bean>

2.3.2.7.2. 配置SqlSessionFactoryBean

  1. <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
  2. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  3. <!-- 数据库连接池 -->
  4. <property name="dataSource" ref="dataSource" />
  5. <!-- 加载mybatis的全局配置文件 -->
  6. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
  7. </bean>

并且准备一个"classpath:mybatis/SqlMapConfig.xml"文件

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.  
  7. </configuration>
2.3.2.7.3. 配置mapper扫描器
  1. <!-- 配置Mapper扫描器,mapper扫描器会自动扫描mapper包,生成各种mapper的实例对象,就可以在service中注入这些mapper实例对象了-->
  2. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  3. <property name="basePackage" value="com.dohit.ssm.mapper"/>
  4. </bean>
2.3.2.7.4. 测试controller层+service层+mybatis层

在testservice中,注入一个mapper对象

并且,写一个方法来调用testMapper的方法

在controller中,写一个方法来调service的这个方法

在页面上请求/hello3.action?id=1

2.3.2.8. Spring声明式事务配置

Spring声明式事务管理配置很简单,只要加一个配置文件到工程中,并且让spring容器去加载该文件即可,配置文件如下

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- 事务管理器 -->
  11. <bean id="transactionManager"
  12. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  13. <!-- 数据源 -->
  14. <property name="dataSource" ref="dataSource" />
  15. </bean>
  16. <!-- 通知:增强 -->
  17. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  18. <tx:attributes>
  19. <!-- 传播行为 -->
  20. <tx:method name="save*" propagation="REQUIRED" />
  21. <tx:method name="insert*" propagation="REQUIRED" />
  22. <tx:method name="delete*" propagation="REQUIRED" />
  23. <tx:method name="update*" propagation="REQUIRED" />
  24. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  25. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  26. </tx:attributes>
  27. </tx:advice>
  28. <!-- 切面 -->
  29. <aop:config>
  30. <aop:advisor advice-ref="txAdvice"
  31. pointcut="execution(* com.dohit.ssm.service.*.*(..))" />
  32. </aop:config>
  33. </beans>

注:文件中的datasource对象引用的是在application-dao.xml中配置好的datasource对象

事务配置文件的加载应该在web.xml中由contextLoaderListener去读取加载:

2.4. 整合完成后的所有配置文件及工程结构

2.4.1. 工程结构示意图:

2.4.2. 各配置文件

2.4.2.1. sqlMapConfig.xml

在classpath下创建mybatis/sqlMapConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. </configuration>

2.4.2.2. applicationContext-dao.xml

配置数据源、配置SqlSessionFactory、mapper扫描器。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10.  
  11. <!-- 加载properties配置文件 -->
  12. <context:property-placeholder location="classpath:mybatis/db.properties" />
  13. <!-- 数据库连接池 -->
  14. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
  15. destroy-method="close">
  16. <property name="driverClassName" value="${jdbc.driver}" />
  17. <property name="url" value="${jdbc.url}" />
  18. <property name="username" value="${jdbc.username}" />
  19. <property name="password" value="${jdbc.password}" />
  20. <property name="maxActive" value="10" />
  21. <property name="maxIdle" value="5" />
  22. </bean>
  23. <!-- mapper配置 -->
  24. <!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
  25. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <!-- 数据库连接池 -->
  27. <property name="dataSource" ref="dataSource" />
  28. <!-- 加载mybatis的全局配置文件 -->
  29. <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
  30. </bean>
  31.  
  32. <!-- 配置Mapper扫描器,mapper扫描器会自动扫描mapper包,生成各种mapper的实例对象,就可以在service中注入这些mapper实例对象了-->
  33. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  34. <property name="basePackage" value="com.dohit.ssm.mapper"/>
  35. </bean>
  36.  
  37. </beans>

2.4.2.3. db.properties

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
  3. jdbc.username=root
  4. jdbc.password=root

2.4.2.4. applicationContext-service.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10.  
  11. <!-- 组件扫描父包路径 -->
  12. <context:component-scan base-package="com.dohit.ssm.service" />
  13.  
  14. </beans>

2.4.2.5. applicationContext-transaction.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
  10. <!-- 事务管理器 -->
  11. <bean id="transactionManager"
  12. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  13. <!-- 数据源 -->
  14. <property name="dataSource" ref="dataSource" />
  15. </bean>
  16. <!-- 通知 -->
  17. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  18. <tx:attributes>
  19. <!-- 传播行为 -->
  20. <tx:method name="save*" propagation="REQUIRED" />
  21. <tx:method name="insert*" propagation="REQUIRED" />
  22. <tx:method name="delete*" propagation="REQUIRED" />
  23. <tx:method name="update*" propagation="REQUIRED" />
  24. <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
  25. <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
  26. </tx:attributes>
  27. </tx:advice>
  28. <!-- 切面 -->
  29. <aop:config>
  30. <aop:advisor advice-ref="txAdvice"
  31. pointcut="execution(* cn.dohit.springmvc.service.*.*(..))" />
  32. </aop:config>
  33. </beans>

2.4.2.6. springmvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:mvc="http://www.springframework.org/schema/mvc"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
  8. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
  9.  
  10. <!-- 加载注解驱动 -->
  11. <mvc:annotation-driven />
  12.  
  13. <!-- 指定需要扫描controller类的包 -->
  14. <context:component-scan base-package="com.dohit.ssm.controller" />
  15.  
  16. <!-- 视图解析器配置:前缀和后缀 -->
  17. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  18. <property name="viewClass"
  19. value="org.springframework.web.servlet.view.JstlView" />
  20. <property name="prefix" value="/WEB-INF/jsp/" />
  21. <property name="suffix" value=".jsp" />
  22. </bean>
  23.  
  24. <!-- 静态资源的自动映射 -->
  25. <mvc:resources location="/js/" mapping="/js/**" />
  26. <mvc:resources location="/img/" mapping="/img/**" />
  27. <mvc:resources location="/css/" mapping="/css/**" />
  28.  
  29. </beans>

2.4.2.7. web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
  5. id="WebApp_ID" version="2.5">
  6. <display-name>ssm-integration</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index.html</welcome-file>
  9. </welcome-file-list>
  10.  
  11. <!-- 加载spring容器 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:spring/applicationContext-*.xml</param-value>
  15. </context-param>
  16. <listener>
  17. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  18. </listener>
  19.  
  20. <servlet>
  21. <servlet-name>springmvc</servlet-name>
  22. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  23. <init-param>
  24. <param-name>contextConfigLocation</param-name>
  25. <param-value>classpath:springmvc/springmvc.xml</param-value>
  26. </init-param>
  27. </servlet>
  28. <servlet-mapping>
  29. <servlet-name>springmvc</servlet-name>
  30. <url-pattern>*.action</url-pattern>
  31. </servlet-mapping>
  32. </web-app>

springmvc代码执行流程

Spring笔记(二)的更多相关文章

  1. Spring笔记(二)Core层

    Spring用一种非入侵的方式来管理程序,模块结构图如下:   .Core层 IOC(控制反转):应用本身程序不负责依赖对象的创建及维护,依赖对象的创建及维护有外设容器负责,即:IOC: DI(依赖注 ...

  2. spring笔记(二)

    共性问题: 1. 服务器启动报错,什么原因? * jar包缺少.jar包冲突 1) 先检查项目中是否缺少jar包引用 2) 服务器: 检查jar包有没有发布到服务器下: 用户库jar包,需要手动发布到 ...

  3. spring笔记二

    DI—Dependency Injection,即“依赖注入”:组件之间依赖关系由容器在运行期决定,形象的说,即由容器动态的将某个依赖关系注入到组件之中.依赖注入的目的并非为软件系统带来更多功能,而是 ...

  4. Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)

    Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...

  5. JDBC学习笔记二

    JDBC学习笔记二 4.execute()方法执行SQL语句 execute几乎可以执行任何SQL语句,当execute执行过SQL语句之后会返回一个布尔类型的值,代表是否返回了ResultSet对象 ...

  6. MyBatis笔记二:配置

    MyBatis笔记二:配置 1.全局配置 1.properites 这个配置主要是引入我们的 properites 配置文件的: <properties resource="db.pr ...

  7. Spring笔记(6) - Spring的BeanFactoryPostProcessor探究

    一.背景 在说BeanFactoryPostProcessor之前,先来说下BeanPostProcessor,在前文Spring笔记(2) - 生命周期/属性赋值/自动装配及部分源码解析中讲解了Be ...

  8. 《CMake实践》笔记二:INSTALL/CMAKE_INSTALL_PREFIX

    <CMake实践>笔记一:PROJECT/MESSAGE/ADD_EXECUTABLE <CMake实践>笔记二:INSTALL/CMAKE_INSTALL_PREFIX &l ...

  9. jQuery源码笔记(二):定义了一些变量和函数 jQuery = function(){}

    笔记(二)也分为三部分: 一. 介绍: 注释说明:v2.0.3版本.Sizzle选择器.MIT软件许可注释中的#的信息索引.查询地址(英文版)匿名函数自执行:window参数及undefined参数意 ...

  10. Mastering Web Application Development with AngularJS 读书笔记(二)

    第一章笔记 (二) 一.scopes的层级和事件系统(the eventing system) 在层级中管理的scopes可以被用做事件总线.AngularJS 允许我们去传播已经命名的事件用一种有效 ...

随机推荐

  1. Ubuntu 只能用guest登录的问题修复

    方式1(推荐): 1.先进入guest登入 2.通过Ctrl+Alt+F1进入命令行窗口(注释:Ctrl+Alt+F1~F6进入终端命令行,Ctrl+Alt+F7是退出终端) 3.登录root,然后进 ...

  2. 巨蟒python全栈开发flask14项目开始6

    1.App未读消息显示 2.发起好友请求 3.同意拒绝好友请求 4.玩具社交圈 1.App未读消息显示 2.发起好友请求 3.同意拒绝好友请求 4.玩具社交圈

  3. 百度jQuery库

    <script src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.js"></script>

  4. Limits on Table Column Count and Row Size Databases and Tables Table Size 最大行数

    MySQL :: MySQL 8.0 Reference Manual :: C.10.4 Limits on Table Column Count and Row Size https://dev. ...

  5. linux查看硬件信息的方法

    目前会Linux的人不少,但是精通的不多,怎样才能做一个符合企业需求的Linux人才,首先要有良好的Linux基础知识.本文为你讲解Linux的知识,今天所讲的是 Linux硬件信息怎样查看,希望你能 ...

  6. BitTrex行情查看与技术指标系统

    上个月的时候,向TradingView申请K线图行情插件,填了各种资料,被问了N多问题,结果却仍是不愿意提供插件给我们. 于是,我们自己开发了一个BitTre行情查看与技术指标系统, 这套系统被国内多 ...

  7. Python标准库 之 turtle(海龟绘图)

    turtle库介绍 首先,turtle库是一个点线面的简单图像库(也被人们成为海龟绘图),在Python2.6之后被引入进来,能够完成一些比较简单的几何图像可视化.它就像一个小乌龟,在一个横轴为x.纵 ...

  8. 【Android】自己定义相机的实现(支持连续拍照、前后摄像头切换、连续对焦)

    ~转载请注明http://blog.csdn.net/u013015161/article/details/46921257 介绍 这几天.写了一个自己定义照相机的demo.支持连续拍照和摄像头切换. ...

  9. sql server常用性能计数器

    https://blog.csdn.net/kk185800961/article/details/52462913?utm_source=blogxgwz5 https://blog.csdn.ne ...

  10. SQL事务回滚 写法(转)

    以下是SQL 回滚的语句:方案一:SET   XACT_ABORT   ON--如果产生错误自动回滚GOBEGIN   TRANINSERT   INTO   A   VALUES   (4)INSE ...