zoukankan      html  css  js  c++  java
  • spring_1_工厂与第一个 Spring 程序

    1. 引言

    1.1 EJB存在的问题

    1. 运行环境苛刻
    2. 代码移植性差

    1.2 什么是Spring

    Spring是一个轻量级的JavaEE解决方案,整合众多优秀的设计模式。

    • 轻量级
      1. 对于运行环境没有特殊要求
        开源: Tomcat resion jetty
        收费: weblogic webaphere
      2. 代码移植性高: 不需要实现额外接口
    • JavaEE的解决方案
      img1
    • 整合设计模式
      1. 工厂
      2. 代理
      3. 模板
      4. 策略
    • 设计模式
      • ⼴义概念:⾯向对象设计中,解决特定问题的经典代码。
      • 狭义概念:GOF4⼈帮定义的23种设计模式:⼯⼚、适配器、装饰器、⻔⾯、代理、模板…

    1.3 ⼯⼚设计模式

    1. 概念:通过⼯⼚类,创建对象;

      User user = new User();
      UserDAO userDAO = new UserDAOImpl();
      
    2. 好处:解耦合
      • 耦合:指定是代码间的强关联关系,⼀⽅的改变会影响到另⼀⽅;
      • 问题:不利于代码维护;
      • 简单:把接⼝的实现类,硬编码在程序中;

        UserService userService = new UserServiceImpl();
        

    1.4 简单工厂的设计

    package com.baizhiedu.basic;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class BeanFactory {
        private static Properties env = new Properties();
    
        static{
            try {
                //第一步 获得IO输入流
                InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
                //第二步 文件内容 封装 Properties集合中 key = userService value = com.baizhixx.UserServiceImpl
                env.load(inputStream);
    
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    
        /*
               对象的创建方式:
                   1. 直接调用构造方法 创建对象  UserService userService = new UserServiceImpl();
                   2. 通过反射的形式 创建对象 解耦合
                   Class clazz = Class.forName("com.baizhiedu.basic.UserServiceImpl");
                   UserService userService = (UserService)clazz.newInstance();
         */
    
        public static UserService getUserService() {
            UserService userService = null;
            try {
                //com.baizhiedu.basic.UserServiceImpl
                Class clazz = Class.forName(env.getProperty("userService"));
                userService = (UserService) clazz.newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return userService;
        }
    
        public static UserDAO getUserDAO(){
            UserDAO userDAO = null;
            try {
                Class clazz = Class.forName(env.getProperty("userDAO"));
                userDAO = (UserDAO) clazz.newInstance();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return userDAO;
        }
    
    }
    

    配置文件 applicationContext.properties:

    # Properties 集合 存储 Properties文件的内容
    # 特殊Map key=String value=String
    # Properties [userService = com.baizhiedu.xxx.UserServiceImpl]
    # Properties.getProperty("userService")
    
    userService = com.baizhiedu.basic.UserServiceImpl
    userDAO = com.baizhiedu.basic.UserDAOImpl
    

    1.5 通用工厂的设计

    问题:简单⼯⼚会存在⼤量的代码冗余。
    img2

    通⽤⼯⼚的代码:

    package com.baizhiedu.basic;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    public class BeanFactory {
        private static Properties env = new Properties();
        static{
            try {
                //第一步 获得IO输入流
                InputStream inputStream = BeanFactory.class.getResourceAsStream("/applicationContext.properties");
                //第二步 文件内容 封装 Properties集合中 key = userService value = com.baizhixx.UserServiceImpl
                env.load(inputStream);
    
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
         /*
          key 小配置文件中的key [userDAO,userService]
          */
         public static Object getBean(String key){
             Object ret = null;
             try {
                 Class clazz = Class.forName(env.getProperty(key));
                 ret = clazz.newInstance();
             } catch (Exception e) {
                e.printStackTrace();
             }
             return ret;
         }
    
    }
    

    1.6 通⽤⼯⼚的使⽤⽅式

    1. 定义类型 (类)
    2. 通过配置⽂件的配置告知⼯⼚ applicationContext.properties 中 key = value;
    3. 通过⼯⼚获得类的对象 Object ret = BeanFactory.getBean("key");

    1.7 总结:

    Spring本质: ⼯⼚ ApplicationContext (applicationContext.xml)

    2. 第一个 Spring 程序

    使用工具idea

    2.1 环境搭建

    依赖查询网站:https://mvnrepository.com/;

    • 配置 Spring 的 jar 包:

      <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.1.4.RELEASE</version>
      </dependency>
      
    • Spring 的配置文件:
      1. 配置⽂件的放置位置:任意位置,没有硬性要求;
      2. 配置⽂件的命名 :没有硬性要求,建议: applicationContext.xml
    • 思考:⽇后应⽤ Spring 框架时,需要进⾏配置⽂件路径的设置。

    2.2 Spring 的核⼼API

    ApplicationContext

    • 作⽤:Spring 提供的 ApplicationContext 这个⼯⼚,⽤于对象的创建; 好处:解耦合
    • ApplicationContext 是接⼝类型; 接⼝:屏蔽实现的差异 ⾮web环境 (main junit): ClassPathXmlApplicationContext web环境: XmlWebApplicationContext img3

    • 重量级资源: ApplicationContext ⼯⼚的对象占⽤⼤量内存。 不会频繁的创建对象, ⼀个应⽤只会创建⼀个⼯⼚对象。 ApplicationContext ⼯⼚:⼀定是线程安全的(多线程并发访问)。

    2.3 程序开发

    1. 创建类型:Person.java

      public class Person {}
      
    2. 配置文件的配置:

      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      
        <bean id="person" class="com.yusael.basic.Person"/>
      
      </beans>
      
    3. 通过⼯⼚类,获得对象

      /**
       * 用于测试Spring的第一个程序
       */
      @Test
      public void test() {
          // 1、获取spring的工厂
          ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
          // 2、通过工厂类获得对象
          Person person = (Person)ctx.getBean("person");
          System.out.println(person);
      }
      

    2.4 细节分析

    名词解释:Spring ⼯⼚创建的对象,叫做 bean 或者 组件(componet);

    2.5 Spring ⼯⼚的相关的⽅法

    1. getBean :传入 id值 和 类名 获取对象,不需要强制类型转换。

      // 通过这种⽅式获得对象,就不需要强制类型转换
      Person person = ctx.getBean("person", Person.class);
      System.out.println("person = " + person);
      
    2. getBean :只指定类名,Spring 的配置文件中只能有一个 bean 是这个类型。

      Person person = ctx.getBean(Person.class);
      System.out.println("person = " + person);
      
    3. getBeanDefinitionNames :获取 Spring 配置文件中所有的 bean 标签的 id 值。

      // 获取的是Spring工厂配置文件中所有bean标签的id值  person person1
      String[] beanDefinitionNames = ctx.getBeanDefinitionNames();
      for (String beanDefinitionName : beanDefinitionNames) {
          System.out.println("beanDefinitionName = " + beanDefinitionName);
      }
      
    4. getBeanNamesForType :根据类型获得 Spring 配置文件中对应的 id 值。

      // 根据类型获得Spring配置文件中对应的id值
      String[] beanNamesForType = ctx.getBeanNamesForType(Person.class);
      for (String id : beanNamesForType) {
          System.out.println("id = " + id);
      }
      
    5. containsBeanDefinition :用于判断是否存在指定 id 值的 bean, 不能判断 name 值

      // 用于判断是否存在指定id值的bean,不能判断name值
      if (ctxn.containsBeanDefinition("person")) {
          System.out.println(true);
      } else {
          System.out.println(false);
      }
      
    6. containsBean :用于判断是否存在指定 id 值的 bean, 也可以判断 name 值

      // 用于判断是否存在指定id值的bean,也可以判断name值
      if (ctx.containsBean("p")) {
          System.out.println(true);
      } else {
          System.out.println(false);
      }
      

    2.6 配置文件中的细节

    如果 bean 只配置 class 属性:

    <bean class="com.yusael.basic.Person"></bean>
    
    • 自动 生成一个 id,com.yusael.basic.Person#1 可以使用 getBeanNamesForType 验证.
    • 应⽤场景: 如果这个 bean 只需要使⽤⼀次 ,那么就可以省略 id 值; 如果这个 bean 会使⽤多次,或者被其他 bean 引⽤则需要设置 id 值;

    name 属性:

    • 作⽤:⽤于在 Spring 的配置⽂件中,为 bean 对象定义别名(小名)
    • name 与 id 的相同点:
      • ctx.getBean("id") 或 ctx.getBean("name") 都可以创建对象;
      • <bean id="person" class="Person"/> 与 <bean name="person" class="Person"/> 等效;
    • name 与 id 的区别:
      • 别名可以定义多个,但是 id 属性只能有⼀个值;
      • XML 的 id 属性的值,命名要求:必须以字⺟开头,可以包含 字⺟、数字、下划线、连字符;不能以特殊字符开头 /person; XML 的 name 属性的值,命名没有要求,/person 可以。 但其实 XML 发展到了今天:ID属性的限制已经不存在,/person也可以。

    2.7 Spring⼯⼚的底层实现原理(简易版)

    img4

    3. 思考

    问题:未来在开发过程中,是不是所有的对象,都会交给 Spring ⼯⼚来创建呢? 回答:理论上是的,但是有特例 :实体对象(entity) 是不会交给Spring创建,它由持久层框架进⾏创建。

    4. Spring5.x 与 日志框架 的整合

    Spring 与⽇志框架进⾏整合,⽇志框架就可以在控制台中,输出Spring框架运⾏过程中的⼀些重要的信息。 好处:便于了解Spring框架的运⾏过程,利于程序的调试。

    默认日志框架
    Spring 1.x、2.x、3.x 早期都是基于commonslogging.jar
    Spring 5.x 默认整合的⽇志框架 logback、log4j2
    

    Spring 如何整合⽇志框架? Spring5.x 整合 log4j:

    1. 引⼊ log4j.jar 包;

      <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
      </dependency>
      <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
      </dependency>
      
    2. 引⼊ log4j.properties 配置⽂件;

      # resources文件夹根目录下
      ### 配置根
      log4j.rootLogger = debug,console
      
      ### 日志输出到控制台显示
      log4j.appender.console=org.apache.log4j.ConsoleAppender
      log4j.appender.console.Target=System.out
      log4j.appender.console.layout=org.apache.log4j.PatternLayout
      log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
      
  • 相关阅读:
    线性时间排序算法
    【MSSQL】MDF、NDF、LDF文件的含义
    SQL表变量与临时表区别 + 非游标临时表遍历
    SQL Server 备份和还原全攻略
    S​Q​L​_​S​e​r​v​e​r​_​2​0​0​8​定​期​自​动​备​份​详​细​图​解
    Global.asax 文件是什么
    SQL 视图 局部变量 全局变量 条件语句 事务 触发器
    XP禁用了U盘和移动硬盘方法
    java实现 数据结构:链表、 栈、 队列、优先级队列、哈希表
    Mysql 存储过程和函数区别
  • 原文地址:https://www.cnblogs.com/instinct-em/p/13254181.html
Copyright © 2011-2022 走看看