程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

手写实现 IOC 和 AOP

发布于2021-05-29 23:10     阅读(1211)     评论(0)     点赞(3)     收藏(0)


此处准备了⼀个『银⾏转账』的案例,请分析该案例在代码层次有什么问题 ?分析之后使⽤我们已有知识解决这些问题(痛点)。其实这个过程我们就是在⼀步步分析并⼿写实现 IoC 和 AOP。(文章转载自乐字节)

0.png

第1节 银行转账案例界面

1.png

第2节 银行转账案例表结构

2.png

第3节 银行转账案例代码调用关系第4节 银行转账案例关键代码

3.png

第4节 银⾏转账案例关键代码

  • TransferServlet
import com.lagou.edu.service.impl.TransferServiceImpl;
import com.lagou.edu.utils.JsonUtils;
import com.lagou.edu.pojo.Result;
import com.lagou.edu.service.TransferService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**TransferService接⼝及实现类
* @author 应癫
*/
@WebServlet(name="transferServlet",urlPatterns = "/transferServlet")
public class TransferServlet extends HttpServlet {
 // 1. 实例化service层对象
 private TransferService transferService = new TransferServiceImpl();
 @Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
 doPost(req,resp);
 }
 @Override
 protected void doPost(HttpServletRequest req, HttpServletResponse
resp) throws ServletException, IOException {
 // 设置请求体的字符编码
 req.setCharacterEncoding("UTF-8");
 String fromCardNo = req.getParameter("fromCardNo");
 String toCardNo = req.getParameter("toCardNo");
 String moneyStr = req.getParameter("money");
 int money = Integer.parseInt(moneyStr);
 Result result = new Result();
 try {
 // 2. 调⽤service层⽅法
 transferService.transfer(fromCardNo,toCardNo,money);
 result.setStatus("200");
 } catch (Exception e) {
 e.printStackTrace();
 result.setStatus("201");
 result.setMessage(e.toString());
 }
 // 响应
 resp.setContentType("application/json;charset=utf-8");
 resp.getWriter().print(JsonUtils.object2Json(result));
 }
}

  • TransferService接口及实现类
/**
* @author 应癫
*/
public interface TransferService {
 void transfer(String fromCardNo,String toCardNo,int money) throws
Exception;
}
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.dao.impl.JdbcAccountDaoImpl;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
/**
* @author 应癫
*/
public class TransferServiceImpl implements TransferService {
 private AccountDao accountDao = new JdbcAccountDaoImpl();
 @Override
 public void transfer(String fromCardNo, String toCardNo, int money)
throws Exception {
 Account from = accountDao.queryAccountByCardNo(fromCardNo);
 Account to = accountDao.queryAccountByCardNo(toCardNo);
 from.setMoney(from.getMoney()-money);
 to.setMoney(to.getMoney()+money);
 accountDao.updateAccountByCardNo(from);
 accountDao.updateAccountByCardNo(to);
 }
}

  • AccountDao层接⼝及基于Jdbc的实现类
import com.lagou.edu.pojo.Account;
/**
* @author 应癫
*/JdbcAccountDaoImpl(Jdbc技术实现Dao层接⼝)
public interface AccountDao {
 Account queryAccountByCardNo(String cardNo) throws Exception;
 int updateAccountByCardNo(Account account) throws Exception;
}
  • JdbcAccountDaoImpl(Jdbc技术实现Dao层接口)
import com.lagou.edu.pojo.Account;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.utils.DruidUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* @author 应癫
*/
public class JdbcAccountDaoImpl implements AccountDao {
 @Override
 public Account queryAccountByCardNo(String cardNo) throws Exception {
 //从连接池获取连接
 Connection con = DruidUtils.getInstance().getConnection();
 String sql = "select * from account where cardNo=?";
 PreparedStatement preparedStatement = con.prepareStatement(sql);
 preparedStatement.setString(1,cardNo);
 ResultSet resultSet = preparedStatement.executeQuery();
 Account account = new Account();
 while(resultSet.next()) {
 account.setCardNo(resultSet.getString("cardNo"));
 account.setName(resultSet.getString("name"));
 account.setMoney(resultSet.getInt("money"));
 }
 resultSet.close();
 preparedStatement.close();
 con.close();
 return account;
 }
}

第5节 银⾏转账案例代码问题分析

4.png

(1)问题⼀:在上述案例实现中,service 层实现类在使⽤ dao 层对象时,直接在TransferServiceImpl 中通过 AccountDao accountDao = new JdbcAccountDaoImpl() 获得了 dao层对象,然而⼀个 new 关键字却将 TransferServiceImpl 和 dao 层具体的⼀个实现类JdbcAccountDaoImpl 耦合在了⼀起,

如果说技术架构发⽣⼀些变动,dao 层的实现要使⽤其它技术,比如 Mybatis,思考切换起来的成本?每⼀个 new 的地⽅都需要修改源代码,重新编译,⾯向接⼝开发的意义将⼤打折扣?

(2)问题⼆:service 层代码没有竟然还没有进⾏事务控制 ?!如果转账过程中出现异常,将可能导致数据库数据错乱,后果可能会很严重,尤其在⾦融业务。(文章转载自乐字节)

最后知识点实在是太多,小编也无力继续写下去啦,建议自己获取课件慢慢学!

该文章是spring高级框架课程笔记里面(手写实现 IOC 和 AOP)的知识点!

需要spring高级框架笔记的可点击下方跳转链接

提取码;f8wo

001.png

原文链接:https://blog.csdn.net/weixin_55765992/article/details/117363312



所属网站分类: 技术文章 > 博客

作者:听说你没有见过我

链接:http://www.javaheidong.com/blog/article/207879/6e428e177123dd60bcfa/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)