一、Service层使用Spring获取Mapper对象
1.1 问题
目前我们开发功能的流程中,在service层会手动创建SQLSession对象,并使用SQLSession对象获取Mapper接口的实例化对象,但是我们真正使用的是Mapper接口的对象,目前的代码编写方式极大的影响了开发效率,而且mybatis层和service层之间的耦合性非常高
//1.获取SqlSession对象
final InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
final SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
final SqlSession sqlSession = sqlSessionFactory.openSession();
//2.获得mapper对象
final UserMapper mapper = sqlSession.getMapper(UserMapper.class); //依赖性很强,耦合性高不便于维护1.2 解决
使用SpringIOC技术实现service层和mybatis层的解耦:说白了就是让Spring容器帮我们获取Mapper层接口的实例化 对象,我们直接从Spring容器中获取使用即可
//<!--配置Datesource的Bean对象:存储数据库连接参数-->
<bean id="datesource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8"></property>
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="username" value="root"></property>
<property name="password" value="112233"></property>
</bean>
//<!--配置SqlSessionFactory对象:生产Session-->
<bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="datesource"></property>
</bean>
//<!--配置Mapper扫描的Bean对象:使用SqlSession扫描mapper获取Mapper接口的实例化-->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="SqlSessionFactory"></property>
<property name="basePackage" value="com.mapper"></property>
</bean>1.3 获取mapper对象
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
final UserMapper mapper = (UserMapper) ac.getBean("userMapper");1.4 功能说明
返回Spring容器中所有Bean对象的键名
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
final String[] beanDefinitionNames = ac.getBeanDefinitionNames(); //获取Spring容器中所有Bean对象的键名
for(String name:beanDefinitionNames){
System.out.println(name);
}结果
结果分析
detasource、SqlSessionFactory、mapper这三个对象都是我们自己创建的,而userMapper对象是系统自动创建的,也就是说Spring干了一件非常伟大的事情:当我们用数据源创建了Session工厂,用Session工厂生产了Session对象,该Session会自动扫描(name = "basePackage")的包路径,完成整个mapper层的扫描,接着把扫描完的结果自动放入Spring容器中,并自动生成了操作对象的键名:就是mapper层的接口名首字母小写,我们即可根据该键名获取到特定的mapper层对象,实现service层和mybatis层的解耦
二、Controller层使用Spring解耦service层
问题:在业务层使用Spring容器对象获取Mapper接口实例化对象后实现了service层和mybatis层的解耦,但是在controller层我们依然在Servlet 中直接创建Service对象,耦合性过高
解决:将service对象配置为bean对象,Servlet中从Spring容器中获取Service对象,完成功能开发
//<!--配置service对象--> <bean id="us" class="com.service.impl.UserServiceImpl"></bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
final UserServiceImpl userService = (UserServiceImp) ac.getBean("us");三、MVC项目中使用SpringIOC解耦的代码优化
3.1 优化①
假如10个用户发起登录请求,tomcat会创建10个线程处理请求.每个线程都需要调用UserServlet的service方法,而方法的调用依赖UserServlet的实例化对象,Servlet对象只会被创建一次,Servlet对象是多线程共享的.那么Servlet什么时候被创建呢?默认在第一次请求的时候被创建,往后就不会再次重新创建了,如果配置了loadOnStartUp则在服务器启动的时候就会完成初始化创建,当有请求到来时,直接调用即可,省去了一个创建的过程
@WebServlet(value = "/UserServlet" ,loadOnStartup = 1) //启动时就创建
3.2 优化②
问题:在一中我们实现了从Spring容器中获取mapper对象,在二中我们实现了从Spring容器中获取service对象,而获取这两个对象,一次请求会导致Spring容器配置文件加载两次,一旦高并发访问,会造成内存资源的极大的浪费,造成项目崩溃
解决:可以将mapper对象设置为Service层的属性,可以在第一次配置Service层的同时通过构造器方式或者属性注入的方式获取该mapper对象
注意:通过属性注入方式获取对象时需提供该属性的get和set方法
public class UserServiceImpl implements UserService {
UserMapper userMapper; //设置为属性,通过属性注入方式获取
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
@Override
public User userLoginService(String uname, String pwd) throws IOException {
//优化前获取该对象的方式
//ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
//final UserMapper mapper = (UserMapper) ac.getBean("userMapper");
final User user = userMapper.userLoginMapper(uname, pwd);
return user;
}
}//<!--配置service对象-->
<bean id="us" class="com.service.impl.UserServiceImpl">
<property name="userMapper" ref="userMapper"></property> //属性注入
</bean>3.3 优化③ 在Servlet的init方法中完成初始化资源的加载(从Spring容器对象中获取业务层对象)
优化前代码:
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//接收数据
final String uname = req.getParameter("uname");
final String pwd = req.getParameter("pwd");
/*final UserServiceImpl userService = new UserServiceImpl();*/
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml"); //每来一个线程,都会读
final UserServiceImpl userService = (UserServiceImpl) ac.getBean("us"); //读取一遍配置文件,资源浪费
final User user = userService.userLoginService(uname, pwd);
//获得Session对象
final HttpSession session = req.getSession();
if(user != null){
//成功重定向到主页
session.setAttribute("user",user);
resp.sendRedirect(req.getContextPath()+"/main.jsp");
}
else{
//失败返回登录页面
session.setAttribute("flag","loginFail");
resp.sendRedirect(req.getContextPath()+"/login.jsp");
}
}优化后代码:
private UserServiceImpl userService; //将业务层对象设置为属性,通过初始化方法获取
@Override
public void init() throws ServletException {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
userService = (UserServiceImpl) ac.getBean("us");
}3.4 优化④ 在web.xml中加载applicationcontext配置文件
原来加载配置文件代码:
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");加载路径被写死了,不利于后期维护和修改
解决:在web.xml中加载applicationcontext配置文件
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
//<!--配置全局参数:记录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>
</web-app>获取方式:
WebApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

京公网安备 11010502036488号