其他分享
首页 > 其他分享> > Spring回顾

Spring回顾

作者:互联网

Spring回顾

Spring两个核心思想:IOC和AOP

  1. 什么是ioc:

    • Inversion Of Control(简称IOC),控制反转;它是一个技术思想,不是一个技术实现;

    • 开发模式:我们不用自己去new对象,是通过从IOC容器去帮助我们实例化对象并且管理它,我们需要使用哪个对象,去问IOC容器拿即可;此外,我们失去了创建对象的权力,得到了不用考虑对象的创建和管理等一系列事情;

    • 为什么叫做控制反转?

      • 控制: 指的是对象创建(实例化,管理)的权利

      • 反转:控制反转交给外部环境(如spring.ioc容器)

    • IOC和DI的区别:

      • DI:Dependancy Injection(依赖注入) ,IOC和DI描述的是同一件事情(对象实例化及依赖关系维护这件事情),只不过角度不一样罢了,

        IOC是站在对象的角度,对象实例化及管理的权利交给了反转容器;DI是站在容器的角度;

  2. 什么是AOP

    • AOP: Aspect o'riented Programming 面向切面/方面变成,AOP是OOP的延续,从OOP说起:

      • OOP三大特征:封装,继承,多态;OOP思想是一种垂直纵向的继承体系(联想extends),OOP编程思想可以解决大多数的代码重复问题,但是在顶级父类中的多个方法中相同位置出现了重复代码,OOP就解决不了;这时候AOP就出场了...

    • 为什么叫做面向切面编程?

      • 在多个纵向(顺序)流程中,出现的相同子流程代码,我们称之为横切逻辑代码,横切逻辑代码的使用场景很有限,一般有:事务控制,权限校验,日志等


spring框架的ioc实现三种方式:

  1. 纯xml(bean信息定义全部配置在xml中)

    1. javaSE应用:ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml")或者new FileSystemXmlApplicationContext("c:/beans.xml")

    2. javaWeb应用:ContextLoaderListener(监听器去加载xml)

  2. xml+注解:部分bean使用注解定义(实现同上)

  3. 纯注解模式,所有的bean都是用注解来定义

    1. javaSE应用:applicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);

    2. javaWeb应用:ContextLoaderListener(监听器去加载注解配置类)

                   (springf'ramework.beans关系图)

BeanFactory与ApplicationContext区别

BeanFactory 是Spring框架中Ioc容器的顶层接口,它只是用来定义一些基础功能,定义一些基础规范,而 ApplicationContext 是它的一个子接口,所以ApplicationContext 是具备 BeanFactory 提供的全部的功能的;通常我们称 BeanFactory 为SpringIOC的基础容器,ApplicationContext 是容器的高级接口,比 BeanFactory 拥有更多的功能;


手写实现IOC和AOP

上⼀部分我们理解了 IoC 和 AOP 思想,我们先不考虑 Spring 是如何实现这两个思想的,此处准备了⼀ 个『银⾏转账』的案例,请分析该案例在代码层次有什么问题 ?分析之后使⽤我们已有知识解决这些问 题(痛点)。其实这个过程我们就是在⼀步步分析并⼿写实现 IoC 和 AOP。

银行转帐案例界面

表结构

代码调用关系

TransferServlet
@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接⼝及实现类
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的实现类
public interface AccountDao {
 Account queryAccountByCardNo(String cardNo) throws Exception;
 int updateAccountByCardNo(Account account) throws Exception;
}
JdbcAccountDaoImpl(Jdbc技术实现Dao层接⼝)
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;
 }
    
 @Override
 public int updateAccountByCardNo(Account account) throws Exception {
 //从连接池获取连接
 Connection con = DruidUtils.getInstance().getConnection();
 String sql = "update account set money=? where cardNo=?";
 PreparedStatement preparedStatement = con.prepareStatement(sql);
 preparedStatement.setInt(1,account.getMoney());
 preparedStatement.setString(2,account.getCardNo());
 int i = preparedStatement.executeUpdate();
 preparedStatement.close();
 con.close();
 return i;
 }
}
​
代码问题分析

(1)问题⼀:在上述案例实现中,service 层实现类在使⽤ dao 层对象时,直接在 TransferServiceImpl 中通过 AccountDao accountDao = new JdbcAccountDaoImpl() 获得了 dao层对 象,然⽽⼀个 new 关键字却将 TransferServiceImpl 和 dao 层具体的⼀个实现类 JdbcAccountDaoImpl 耦合在了⼀起,如果说技术架构发⽣⼀些变动,dao 层的实现要使⽤其它技术, ⽐如 Mybatis,思考切换起来的成本?每⼀个 new 的地⽅都需要修改源代码,重新编译,⾯向接⼝开发 的意义将⼤打折扣?

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

问题解决思路

下面我们进行代码改造:
-- beans.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans>
 <bean id="transferService"
class="com.yun.test.service.impl.TransferServiceImpl">
 <property name="AccountDao" ref="accountDao"></property>
 </bean>
 <bean id="accountDao"
class="com.yun.test.dao.impl.JdbcAccountDaoImpl">
 </bean>
</beans>
public class BeanFactory {
 /**
 * ⼯⼚类的两个任务
 * 任务⼀:加载解析xml,读取xml中的bean信息,通过反射技术实例化bean对象,然后放⼊
map待⽤
 * 任务⼆:提供接⼝⽅法根据id从map中获取bean(静态⽅法)
 */
 private static Map<String,Object> map = new HashMap<>();
 
 static {
     InputStream resourceAsStream = BeanFactory.class.getClassLoader().getResourceAsStream("beans.xml");
     SAXReader saxReader = new SAXReader();
     try {
     Document document = saxReader.read(resourceAsStream);
     Element rootElement = document.getRootElement();
     List<Element> list = rootElement.selectNodes("//bean");
     // 实例化bean对象
     for (int i = 0; i < list.size(); i++) {
         Element element = list.get(i);
         String id = element.attributeValue("id");
         String clazz = element.attributeValue("class");
         Class<?> aClass = Class.forName(clazz);
         Object o = aClass.newInstance();
         map.put(id,o);
     }
 // 维护bean之间的依赖关系
 List<Element> propertyNodes = rootElement.selectNodes("//property");
 for (int i = 0; i < propertyNodes.size(); i++) {
     Element element = propertyNodes.get(i);
    // 处理property元素
    String name = element.attributeValue("name");
    String ref = element.attributeValue("ref");
 
    String parentId = element.getParent().attributeValue("id");
    Object parentObject = map.get(parentId);
    Method[] methods = parentObject.getClass().getMethods();
    for (int j = 0; j < methods.length; j++) {
     Method method = methods[j];
    if(("set" + name).equalsIgnoreCase(method.getName())){
        // bean之间的依赖关系(注⼊bean)
        Object propertyObject = map.get(ref);
         method.invoke(parentObject,propertyObject);
        }
 }
 // 维护依赖关系后重新将bean放⼊map中
 map.put(parentId,parentObject);
 }
 } catch (DocumentException e) {
 e.printStackTrace();
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 } catch (IllegalAccessException e) {
 e.printStackTrace();
 } catch (InstantiationException e) {
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 e.printStackTrace();
 }
 }
 public static Object getBean(String id) {
 return map.get(id);
 }
}
​

针对问题⼆的改造
public class TransactionManager {
 private ConnectionUtils connectionUtils;
 public void setConnectionUtils(ConnectionUtils connectionUtils) {
 this.connectionUtils = connectionUtils;
 }
 // 开启事务
 public void beginTransaction() throws SQLException {
 connectionUtils.getCurrentThreadConn().setAutoCommit(false);
 }
 // 提交事务
 public void commit() throws SQLException {
 connectionUtils.getCurrentThreadConn().commit();
 }
 // 回滚事务
 public void rollback() throws SQLException {
 connectionUtils.getCurrentThreadConn().rollback();
 }
}
​
public class ProxyFactory {
     private TransactionManager transactionManager;
     public void setTransactionManager(TransactionManagertransactionManager) {
     this.transactionManager = transactionManager;
 }
 public Object getProxy(Object target) {
 return Proxy.newProxyInstance(this.getClass().getClassLoader(),
target.getClass().getInterfaces(), new InvocationHandler() {
 @Override
 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
 Object result = null;
 try{
 // 开启事务
 transactionManager.beginTransaction();
 // 调⽤原有业务逻辑
 result = method.invoke(target,args);
 // 提交事务
 transactionManager.commit();
 }catch(Exception e) {
 e.printStackTrace();
 // 回滚事务
 transactionManager.rollback();
 // 异常向上抛出,便于servlet中捕获
 throw e.getCause();
 }
 return result;
 }
 });
 }
}
​
<?xml version="1.0" encoding="UTF-8" ?>
<!--跟标签beans,⾥⾯配置⼀个⼜⼀个的bean⼦标签,每⼀个bean⼦标签都代表⼀个类的配置--
>
<beans>
 <!--id标识对象,class是类的全限定类名-->
 <bean id="accountDao"class="com.wsy.test.dao.impl.JdbcAccountDaoImpl">
    <property name="ConnectionUtils" ref="connectionUtils"/>
 </bean>
 <bean id="transferService"class="com.wsy.test.service.impl.TransferServiceImpl">
 <!--set+ name 之后锁定到传值的set⽅法了,通过反射技术可以调⽤该⽅法传⼊对应的值-->
    <property name="AccountDao" ref="accountDao"></property>
 </bean>
 <!--配置新增的三个Bean-->
 <bean id="connectionUtils"class="com.wsy.test.utils.ConnectionUtils"></bean>
 <!--事务管理器-->
 <bean id="transactionManager"class="com.wsy.test.utils.TransactionManager">
    <property name="ConnectionUtils" ref="connectionUtils"/>
 </bean>
 <!--代理对象⼯⼚-->
 <bean id="proxyFactory" class="com.wsy.test.factory.ProxyFactory">
    <property name="TransactionManager" ref="transactionManager"/>
 </bean>
</beans>
public class JdbcAccountDaoImpl implements AccountDao {
 private ConnectionUtils connectionUtils;
 public void setConnectionUtils(ConnectionUtils connectionUtils) {
 this.connectionUtils = connectionUtils;
 }
 @Override
 public Account queryAccountByCardNo(String cardNo) throws Exception {
 //从连接池获取连接
 // Connection con = DruidUtils.getInstance().getConnection();
 Connection con = connectionUtils.getCurrentThreadConn();
 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;
 }
 @Override
 public int updateAccountByCardNo(Account account) throws Exception {
 // 从连接池获取连接
 // 改造为:从当前线程当中获取绑定的connection连接
 //Connection con = DruidUtils.getInstance().getConnection();
 Connection con = connectionUtils.getCurrentThreadConn();
 String sql = "update account set money=? where cardNo=?";
 PreparedStatement preparedStatement = con.prepareStatement(sql);
 preparedStatement.setInt(1,account.getMoney());
 preparedStatement.setString(2,account.getCardNo());
 int i = preparedStatement.executeUpdate();
 preparedStatement.close();
 //con.close();
 return i;
 }
}
@WebServlet(name="transferServlet",urlPatterns = "/transferServlet")
public class TransferServlet extends HttpServlet {
 // 1. 实例化service层对象
 //private TransferService transferService = new TransferServiceImpl();
 //private TransferService transferService = (TransferService)BeanFactory.getBean("transferService");
 // 从⼯⼚获取委托对象(委托对象是增强了事务控制的功能)
 // ⾸先从BeanFactory获取到proxyFactory代理⼯⼚的实例化对象
 private ProxyFactory proxyFactory = (ProxyFactory)BeanFactory.getBean("proxyFactory");
 private TransferService transferService = (TransferService)proxyFactory.getJdkProxy(BeanFactory.getBean("transferService")) ;
 @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));
 }
}
​

纯xml模式 (实际企业开发,纯XML模式几乎已经没有了,学习的目的是为了和下面注解开发相对应,便于理解及巩固)
实例化Bean的三种⽅式
<!--配置service对象-->
<bean id="userService" class="com.yun.service.impl.TransferServiceImpl">
</bean
<!--使⽤静态⽅法创建对象的配置⽅式-->
<bean id="userService" class="com.yun.factory.BeanFactory"
 factory-method="getTransferService"></bean>
<!--使⽤实例⽅法创建对象的配置⽅式-->
<bean id="beanFactory"
class="com.yun.factory.instancemethod.BeanFactory"></bean>
<bean id="transferService" factory-bean="beanFactory" factory•method="getTransferService"></bean>

XML+注解相结合模式
xml形式 对应的注解形式
<bean>标签 @Component("accountDao"),注解加在类上 bean的id属性内容直接配置在注解后⾯如果不配置,默认定义个这个bean的id为类 的类名⾸字⺟⼩写; 另外,针对分层代码开发提供了@Componenet的三种别名@Controller、 @Service、@Repository分别⽤于控制层类、服务层类、dao层类的bean定义,这 四个注解的⽤法完全⼀样,只是为了更清晰的区分⽽已
<bean>标签的 scope属 性 @Scope("prototype"),默认单例,注解加在类上
<bean>标签的 init-method 属性 @PostConstruct,注解加在⽅法上,该⽅法就是初始化后调⽤的⽅法
<bean>标签的 destory-method 属性 @PreDestory,注解加在⽅法上,该⽅法就是销毁前调⽤的⽅法

纯注解模式

改造xm+注解模式,将xml中遗留的内容全部以注解的形式迁移出去,最终删除xml,从Java配置类启动

对应注解

@Configuration 注解,表名当前类是⼀个配置类

@ComponentScan 注解,替代 context:component-scan

@PropertySource,引⼊外部属性配置⽂件

@Import 引⼊其他配置类

@Value 对变量赋值,可以直接赋值,也可以使⽤ ${} 读取资源配置⽂件中的信息

@Bean 将⽅法返回对象加⼊ SpringIOC 容器

// @Configuration 注解表明当前类是一个配置类
@Configuration
@ComponentScan({"com.yun.test"})
@PropertySource({"classpath:jdbc.properties"})
public class SpringConfig {
​
    @Value("${jdbc.driver}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
​
​
    @Bean("dataSource")
    public DataSource createDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driverClassName);
        druidDataSource.setUrl(url);
        druidDataSource.setUsername(username);
        druidDataSource.setPassword(password);
        return  druidDataSource;
    }
}

标签:String,回顾,对象,Spring,private,bean,new,public
来源: https://www.cnblogs.com/wangshaoyun/p/16643508.html