关于老式的spring+mybatis整合,使用了druid连接池,还使用了mybatis-spring依赖(用于整合的),但是这个依赖本身就使用了spring-jdbc的某些类来处理事务方面的内容,所以还需要额外再添加一个spring-jdbc依赖。

以前的mybatis是通过SqlSessionFactoryBuilder().build()创建出SqlSessionFactory,再调用openSession(true)创建出一个能够自动提交事务的sqlSession,再调用getMapper()得到一个dao类。而现在主要是通过spring的xml文件配置注入依赖。

1.原本的sqlSessionFactoryBuilder().build()在xml文件中改变成配置SqlSessionFactoryBean类的<bean>,有用到dataSource的set方法,获得了sqlSessionFactory对象(在spring容器加载的时候).

2.通过MapperFacotryBean类,这个类本身有一个setMapperInterface,其继承的SqlSessionDaoSupport类有一个setSessionFactory方法,通过在<bean>中配置这两个属性就可以得到一个dao类的对象(向上转型为接口的xxDao)

3.第二步已经把dao类得到了,再就是配置service,使用dao。

4.基本上的<bean>已经配置好了,但是为了方便获取ApplicationContext,也不是说方便,只是ApplicationContext如果通过每次在每个Servlet都new出来,然后又被垃圾回收销毁,而其又管理了多个<bean>,这样子耗时效率低下,也违背了被管理的<bean>的单例,通过监听器就在整个进程中获取一个ApplicationContext,当然你也可以不通过监听器而是直接写一个单例模式也是可以的。在Servlet下创建了一个上下文监听器,在tomcat启动时调用contextInitialized方法创建出一个ApplicationContexxt,具体代码如下:

public class InitializationListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
// 这里是在web.xml文件下写了一个<context-param>设置了spring.xml的文件名,方便不过过java代 码改动
String configFile = servletContext.getInitParameter("configFile");
ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
// 最好还是不要使用魔法值吧,context
servletContext.setAttribute("context", context);
}
// 下方的销毁方法没有内容,无视
}

  同时为了获取到这个context,还写了一个类实现spring的ApplicationContextAware接口(实现了Aware的接口,只要在spring配置好<bean>,其中的set方法都会自动装配,应该是,我不是很确定),set方法中设置了ApplicationContext的赋值,同时额外写一个了方法getBean,直接通过该类调用,就不需要ApplicationContext.getBean

public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
} public static <T> T getBean(String name, Class<T> clz) {
return context.getBean(name, clz);
}
}

  5.但是监听器的启动需要在web.xml文件中配置

<listener>
<listener-class>com.web.listener.InitializationListener</listener-class>
</listener>

  而实现了ApplicationContextAware接口的类要被自动装配还需要配置一个<bean>,以自动装配调用set方法,设置了ApplicationContext对象,才能用到getBean,而不会报空指针异常。

applicationConfig.xml如下,最后一个<bean>是自动装配它自己的类的set方法,不用写id

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/demo"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.dao.DeptDao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> <bean id="service" class="com.service.impl.DeptServiceImpl">
<property name="deptDao" ref="dao"></property>
</bean> <bean class="com.web.listener.ApplicationContextHolder"></bean>

  6.如此,dao模块就只需要写一个接口就行了,在方法上通过注解使用sql语句,service模块在impl类上设置一个dao的字段和对应的set方法。而Servlet中通过代码ApplicationHolder.getBean("service", XXX.class);就可以直接获取到service对象。

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
DeptService service = ApplicationContextHolder.getBean("service", DeptService.class);
List<Dept> depts = service.queryAll();
req.setAttribute("depts", depts);
req.getRequestDispatcher("WEB-INF/home.jsp").forward(req, resp);
}

  

学习spring的第4天的更多相关文章

  1. 学习Spring——依赖注入

    前言: 又开始动笔开了“学习Spring”系列的头…… 其实一开始写“学习SpringMVC”的几篇文章是出于想系统的了解下Spring以及SpringMVC,因为平时在公司中虽然每天都在使用Spri ...

  2. 菜鸟学习Spring——60s配置XML方法实现简单AOP

    一.概述. 上一篇博客讲述了用注解的形式实现AOP现在讲述另外一种AOP实现的方式利用XML来实现AOP. 二.代码演示. 准备工作参照上一篇博客<菜鸟学习Spring--60s使用annota ...

  3. 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置

    在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...

  4. 跟着刚哥学习Spring框架--创建HelloWorld项目(一)

    1.Spring框架简介 Spring是一个开源框架,Spring是在2003年兴起的一个轻量级的开源框架,由Rod johnson创建.主要对JavaBean的生命周期进行管理的轻量级框架,Spri ...

  5. 学习Spring中遇到关于BeanFactory及测试类的问题

    最近在学习Spring,使用的是Spring 5.0.1 学习书本中使用的是4.0 学习书本中使用以下来加载配置文件及设置 Resource resource = new ClassPathResou ...

  6. 学习Spring Boot:(一)入门

    微服务 现在微服务越来越火了,Spring Boot热度蹭蹭直升,自学下. 微服务其实是服务化思路的一种最佳实践方向,遵循SOA(面向服务的架构)的思路,各个企业在服务化治理上面的道路已经走得很远了, ...

  7. 学习Spring Boot:(二十六)使用 RabbitMQ 消息队列

    前言 前面学习了 RabbitMQ 基础,现在主要记录下学习 Spring Boot 整合 RabbitMQ ,调用它的 API ,以及中间使用的相关功能的记录. 相关的可以去我的博客/RabbitM ...

  8. 学习Spring Boot:(二十五)使用 Redis 实现数据缓存

    前言 由于 Ehcache 存在于单个 java 程序的进程中,无法满足多个程序分布式的情况,需要将多个服务器的缓存集中起来进行管理,需要一个缓存的寄存器,这里使用的是 Redis. 正文 当应用程序 ...

  9. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  10. 跟着刚哥学习Spring框架--通过注解方式配置Bean(四)

    组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 特定组件包括: 1.@Component:基本注解,识别一个受Spring管理的组件 2.@Resposit ...

随机推荐

  1. KDE Plasma 5.17 即将发布

    导读 Plasma 5.17上个月达到了beta版本,而下周将发布Plasma 5.17.0版本!KDE桌面的大更新只有几天了.因此,开发人员一直在整理它,同时也集思广益讨论Plasma 5.18应该 ...

  2. CH8 课后习题

    8.1和8.2 #include <iostream> using namespace std; istream& f(istream& in) { int v; in & ...

  3. xcode 6 如何将 模拟器(simulator) for iphone/ipad 转变成 simulator for iphone

    xcode 6默认模拟器是iphone/ipad通用的,如果想只针对iphone或者ipad可以进行如下设置: 1.修改模拟器大小(非必须) 模拟器->WIndow->scale-> ...

  4. 1-8SpringBoot之切面AOP

    SpringBoot提供了强大AOP支持,我们前面讲解过AOP面向切面,所以这里具体AOP原理就补具体介绍: AOP切面主要是切方法,我们一般搞一些日志分析和事务操作,要用到切面,类似拦截器: @As ...

  5. 算法:辗转相除法求最大公约数(C语言实现)

    辗转相除法,一种求最大公约数的算法 已知:A / B = C ······ R  (A.B.C.R皆是整数) 假设:D是A的余数,D也是B的余数,那么D就是A和B的公约数 D是A和B的约数,则A和B是 ...

  6. docker-compose 修改zabbix images 添加微信报警插件 时间同步 中文乱码 添加grafana美化zabbix

    我们先来看一下我们要修改得  zabbix.yaml           github   https://github.com/bboysoulcn/awesome-dockercompose ve ...

  7. 视频编解码 基本概念:GOP

    前言 产品开发要求添加视频剪辑功能,翻阅有关的文档,查到了GOP(group of pictures)这个概念. 解析 GOP说白了就是两个I帧之间的间隔.比较说GOP为120,如果是720p60的话 ...

  8. html5,css3炫酷实例-元素

    自动完成输入框下拉列表 使用的插件:jquery-ui 使用数据源实现文本框的自动完成功能 <link href="https://cdn.bootcss.com/jqueryui/1 ...

  9. mysql 索引入门

    创建索引的语法结构:

  10. makecert 制作数字证书 给DLL加一个数字签名

    声明:文章整理自互联网 我仅需要给dll添加(替换)一个签名,所以我只看了第一步和第三步,其余的部分我没有测试,不能保证内容的是否正确. 看了很多关于DLL加签名的教程 大多是错误的 完全无法正常走下 ...