美文网首页
2018-09-17

2018-09-17

作者: Mango_lxh | 来源:发表于2018-09-17 17:18 被阅读0次

    业务层轻量级框架
    无任何侵入性
    代码量少,配置量少
    spring代替struts1和struts2

    image.png
    作用:
    1. IOC容器
      英语原文:Inversion of Control   中文翻译:控制反转
      (1)利用Spring来创建对象并按依赖关系组装(JavaBean工厂)
      (2)利用Spring构建业务逻辑层
      管理依赖关系
      适应需求变更(单例或多例)
      (3)利用Spring创建数据访问对象(DAO)

    spring在业务层管理对象,自动创建对象,给对象之间装配依赖关系
    手工装配的话,业务层会依赖持久层
    使用单例带来的问题:并发性差

    1. AOP采用动态代理技术(面向切面 如过滤器Filter)
      在原有的代码前后增加功能
      管理事务
    2. 和WEB框架的集成 如:Struts、ORM、Hibernate 等
      例如在和Hibernate集成后,Hibernate把Session和事务都交给Spring进行管理 ,业务层 把对象的装配、对象之间的依赖关系、对象的创建都交给Spring进行管理
    业务层如何调用持久层对象的方法?

    1、工厂
    2、set
    3、有参的构造方法

    UserManager usermanager=new UserManagerImpl();  
    usermanager.setUserDao(new UserDao4OracleImpl());//装配
    usermanager.save("zhansan", "123");
    

    把持久对象装配给业务对象,让业务层和持久层建立关系

    搭建spring的测试环境

    1、创建一个java项目
    2、引入user 库

    • 主jar包
      SPRING_HOME/dist/spring.jar
    • 记录日志
      SPRING_HOME/lib/jakarta-commons/commons-logging.jar
      SPRING_HOME/lib/log4j/log4j-1.2.14.jar

    3、创建spring配置文件 applicationContext.xml ,为了便于调试最好加入log4j配置文件
    从安装包spring-framework-2.0.8\samples\jpetstore\war\WEB-INF下把applicationContext.xml、log4j.properties文件复制到src目录下.
    配置applicationContext.xml文件如下:

      <?xml version="1.0" encoding="UTF-8"?>
    <beans   xmlns="http://www.springframework.org/schema/beans"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:aop="http://www.springframework.org/schema/aop"
                 xmlns:tx="http://www.springframework.org/schema/tx"
                 xsi:schemaLocation="http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
    
    </bean>
    

    4、创建类及Spring编辑配置文件 applicationContext.xml
    Dao类和业务类见spring_whyspring项目
    在本项目中编辑配置文件 applicationContext.xml的前提条件:
    在UserManagerImpl中提供构造函数或setter方法,
    让spring管理我们的对象创建和依赖, 这样spring将实例化好的UserDao实现注入给我们。
    编辑applicationContext.xml配置文件如下:

    <bean id="userDao4MySqlImpl" class="com.spring.dao.UserDao4MySqlImpl"/>
    <bean id="userDao4OracleImpl" class="com.spring.dao.UserDao4OracleImpl"/>
    <bean id="userManager" class="com.spring.manager.UserManagerImpl">
    <!-- 构造方法注入
    <constructor-arg ref="userDao4OracleImpl"/>
    -->
    <property name="userDao" ref="userDao4OracleImpl"/>
    </bean>
    

    id:对象的标识
    class:包名

    5、 创建测试程序Client.java

    public class Client {
    public static void main(String[] args) {
        //获取BeanFactory、读取applicationContext.xml配置文件
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml"); 
        //从IOC容器中获取userManager对象
        UserManager userManager = (UserManager)factory.getBean("userManager");
            userManager.save("张三", "123");
    }
    }
    
    执行过程

    1、首先创建bean工厂,通过读取src下的applicationContext.xml
    2、创建持久层对象
    3、

    相关文章

      网友评论

          本文标题:2018-09-17

          本文链接:https://www.haomeiwen.com/subject/epurnftx.html