SSM集成
Spring + Mybatis
Spring 负责整个项目的IoC和AOP等等,是整个项目的大管家,Mybatis 负责持久层。对于 Spring + Mybatis 的集成,整个配置的解析从 Spring 开始,通过解析 Spring 的xml配置文件或配置类来获得 ApplicationContext 对象(即IoC容器对象),MyBatis 需要的所有配置信息都可以通过配置 MyBatis 提供的集成Spring的类——SqlSessionFactoryBean 来完成(如插件、settings),通过 MapperScannerConfigurer 类来配置Mapper包扫描,生成Mapper接口的代理类。
MyBatis 需要的 DataSource 则直接从IoC注入即可,SqlSessionFactoryBean、MapperScannerConfigurer 也加入IoC即可完成MyBatis的集成。
- MyBatis 的注解会早于Spring解析,需要把DataSource的配置单独放在一个配置类中。
DataSource 加入 IoC:
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.driver}")
private String driver;
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(this.username);
dataSource.setPassword(this.password);
dataSource.setUrl(this.url);
dataSource.setDriverClassName(this.driver);
return dataSource;
}
}MyBatis 其他组件加入IoC:
@Configuration
@ComponentScan("com.guitar.chat.*") // Spring包扫描
@EnableAspectJAutoProxy // 开启AOP
@EnableTransactionManagement // 开启Spring事务管理
public class MapperConfig {
@Bean // sqlSession工厂Bean 加入IoC
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource){
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
// 设置别名
sqlSessionFactoryBean.setTypeAliasesPackage("com.guitar.chat.pojo");
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
// 开启驼峰映射
configuration.setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setConfiguration(configuration);
return sqlSessionFactoryBean;
}
@Bean // Mapper包扫描组件 加入IoC
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
// 配置Mapper包扫描
mapperScannerConfigurer.setBasePackage("com.guitar.chat.mapper");
return mapperScannerConfigurer;
}
@Bean // Spring事务管理器加入IoC
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}xml 配置见:Spring6-集成MyBatis
使用 @Service 将 Service 实现类加入IoC,在Service类中使用 @Autowired 即可自动注入Mapper代理类(MyBatis存入IoC)。
执行 ApplicationContext context = new AnnotationConfigApplicationContext(MapperConfig.class); 即可让配置生效。
解析配置类/xml配置文件是整个项目框架的起点。
SSM
见:五、框架实战:SSM整合原理和实战 (wolai.com)
SSM 是在 Spring+MyBatis 的基础上,引入 SpringMVC 作为Web的框架。Web 项目提供了 WebApplicationInitializer 接口来进行项目的初始化,SpringMVC 封装了这个接口,提供了 AbstractAnnotationConfigDispatcherServletInitializer 抽象类来完成配置解析和初始化,提供了 WebMvcConfigurer 接口来快速配置 SpringMVC 相关组件:
SpringMVC框架负责控制层
Spring 框架负责整体和业务层的声明式事务管理(tx)
MyBatis框架负责数据库访问层(持久层)
IoC 容器
本质上说,整合就是将三层架构和框架核心API组件交给SpringIoC容器管理!根据三层架构,通常使用两个IoC容器(web容器和root容器):
- 分离关注点:通过初始化两个容器,可以将各个层次的关注点进行分离。这种分离使得各个层次的组件能够更好地聚焦于各自的责任和功能。
- 解耦合:各个层次组件分离装配不同的IoC容器,这样可以进行解耦。这种解耦合使得各个模块可以独立操作和测试,提高了代码的可维护性和可测试性。
- 灵活配置:通过使用两个容器,可以为每个容器提供各自的配置,以满足不同层次和组件的特定需求。每个配置文件也更加清晰和灵活。
IoC 容器可以设置父容器,当自身没有相应的组件时,会从父容器中找。
源码:FrameworkServlet 655行!
protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) { Class<?> contextClass = getContextClass(); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException( "Fatal initialization error in servlet with name '" + getServletName() + "': custom WebApplicationContext class [" + contextClass.getName() + "] is not of type ConfigurableWebApplicationContext"); } ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); wac.setEnvironment(getEnvironment()); //wac 就是web ioc容器 //parent 就是root ioc容器 //web容器设置root容器为父容器,所以web容器可以引用root容器 wac.setParent(parent); String configLocation = getContextConfigLocation(); if (configLocation != null) { wac.setConfigLocation(configLocation); } configureAndRefreshWebApplicationContext(wac); return wac; }
SpringMVC 作为整个Web项目的框架,web容器装web相关组件,而设置root容器为父容器,需要父容器内的组件时,也可以无感get。

两个容器的内容:
- web IoC:web相关组件(controller,springmvc核心组件)
- root IoC:业务和持久层相关组件(service,aop,tx,dataSource,mybatis,mapper等)
配置文件关系
MyBatis + Spring(同上述):
DataSourceConfig.java — 由于注解解析顺序而需要单独提供 DataSource 的配置类。
MapperConfig.java — MyBatis 组件配置 + Spring功能配置(如AOP、tx)
SpringMVC:
SpringMvcConfig.java — SpringMVC 相关组件配置
@Configuration @ComponentScan("com.guitar.mvc.controller") @EnableWebMvc public class SpringMvcConfig implements WebMvcConfigurer { }WebInitializer.java — web 初始化类:加载 root容器、web容器,配置Servlet路径
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override // root 容器内容 protected Class<?>[] getRootConfigClasses() { return new Class[]{DataSourceConfig.class, MapperConfig.class}; } @Override // web容器内容 protected Class<?>[] getServletConfigClasses() { return new Class[]{SpringMvcConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }
