zxpnet网站 zxpnet网站
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

zxpnet

一个爱学习的java开发攻城狮
首页
前端
后端服务器
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 大后端课程视频归档
  • 南航面试题
  • 并发编程

  • 性能调优

  • java8语法

  • lombok

  • 日志

  • 工具类

  • spring

    • Spring源码整体脉络介绍及源码编译
    • Ioc容器加载过程-Bean的生命周期源码深度剖析
    • 内置后置PostProcess处理器深度讲解
    • Untitled
    • IOC-Endgame
    • Spring-AOP源码探究
    • 基于注解构建项目
    • springmvc原理深度解析
    • SpringMVC常见面试题总结
    • Spring常见面试题总结
    • spring5基础
    • spring注解驱动开发笔记
    • webflux基础
    • spring工厂
      • Spring AOP 编程
      • Spring 持久层
      • Spring 注解编程
      • spring整合springmvc
    • mybatis

    • springboot

    • redis

    • zookeeper

    • springcloud

    • dubbo

    • netty

    • springsecurity

    • mq消息中间件

    • shiro

    • beetle

    • 模板引擎

    • jpa

    • 数据结构与算法

    • 数据库知识与设计

    • gradle

    • maven

    • bus

    • 定时任务

    • docker

    • centos

    • 加解密

    • biz业务

    • pigx项目

    • 开源项目

    • 品达通用权限项目-黑马

    • 货币交易项目coin-尚学堂

    • php

    • backend
    • spring
    shollin
    2021-08-03
    目录

    spring工厂

    • 配置根
    • 日志输出到控制台显示

    引言 EJB(Enterprise Java Bean)存在的问题:EJB 是重量级的框架。

    运行环境苛刻 代码移植性差 什么是 Spring? Spring是⼀个轻量级的 JavaEE 解决方案,整合众多优秀的设计模式。

    什么是轻量级?

    对于运行环境是没有额外要求的; 开源:tomcat、resion、jetty 收费:weblogic、websphere 代码移植性高:不需要实现额外接口。 JavaEE 的解决方案:

    整合设计模式:

    工厂 代理 模板 策略 什么是设计模式?

    ⼴义概念:面向对象设计中,解决特定问题的经典代码。 狭义概念:GOF4人帮定义的23种设计模式:工厂、适配器、装饰器、门面、代理、模板… 工厂设计模式 什么是工厂设计模式?

    概念:通过工厂类,创建对象; User user = new User(); UserDAO userDAO = new UserDAOImpl(); 1 2 好处:解耦合。 耦合:指定是代码间的强关联关系,⼀方的改变会影响到另⼀方; 问题:不利于代码维护; 简单:把接口的实现类,硬编码在程序中; UserService userService = new UserServiceImpl(); 1 简单工厂的设计 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;
    }
    

    } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 配置文件 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 2 3 4 5 6 7 通用工厂的设计 问题:简单工厂会存在大量的代码冗余。

    通用工厂的代码:

    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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 配置文件 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 2 3 4 5 6 7 通用工厂的使用方式 定义类型 (类) 通过配置文件的配置告知工厂 applicationContext.properties 中 key = value; 通过工厂获得类的对象 Object ret = BeanFactory.getBean("key"); 总结: Spring本质:工厂 ApplicationContext (applicationContext.xml)

    第一个 Spring 程序 环境搭建 依赖查询网站:https://mvnrepository.com/;

    配置 Spring 的 jar 包:

    org.springframework spring-context 5.1.4.RELEASE 1 2 3 4 5 6 Spring 的配置文件:

    配置文件的放置位置:任意位置,没有硬性要求; 配置文件的命名 :没有硬性要求,建议:applicationContext.xml; 思考:日后应用 Spring 框架时,需要进行配置文件路径的设置。

    Spring 的核心API ApplicationContext

    作用:Spring 提供的 ApplicationContext 这个工厂,用于对象的创建; 好处:解耦合 ApplicationContext 是接口类型; 接口:屏蔽实现的差异 非 web 环境 (main junit) :ClassPathXmlApplicationContext web 环境 :XmlWebApplicationContext

    重量级资源: ApplicationContext 工厂的对象占用大量内存。 不会频繁的创建对象 ,⼀个应用只会创建⼀个工厂对象。 A pplicationContext 工厂:⼀定是线程安全的(多线程并发访问)。 程序开发 创建类型:Person.java public class Person {} 1 配置文件的配置: <?xml version="1.0" encoding="UTF-8"?>

        <bean id="person" class="com.yusael.basic.Person"/>
    
    1 2 3 4 5 6 7 8 通过工厂类,获得对象 /** * 用于测试Spring的第一个程序 */ @Test public void test() { // 1、获取spring的工厂 ApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml"); // 2、通过工厂类获得对象 Person person = (Person)ctx.getBean("person"); System.out.println(person); } 1 2 3 4 5 6 7 8 9 10 11 细节分析 名词解释:Spring 工厂创建的对象,叫做 bean 或者 组件 (componet);

    Spring 工厂的相关的方法 getBean:传入 id值 和 类名 获取对象,不需要强制类型转换。

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

    // 使用这种方式的话, 当前Spring的配置文件中 只能有一个bean class是Person类型 Person person = ctx.getBean(Person.class); System.out.println("person = " + person); 1 2 3 getBeanDefinitionNames:获取 Spring 配置文件中所有的 bean 标签的 id 值。

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

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

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

    // 用于判断是否存在指定id值的bean,也可以判断name值 if (ctx.containsBean("p")) { System.out.println(true); } else { System.out.println(false); } 1 2 3 4 5 6 配置文件中的细节 如果 bean 只配置 class 属性:

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

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

    思考 问题:未来在开发过程中,是不是所有的对象,都会交给 Spring 工厂来创建呢?

    回答:理论上是的,但是有特例 :实体对象(entity) 是不会交给Spring创建,它由持久层框架进行创建。

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

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

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

    引⼊ log4j.jar 包; org.slf4j slf4j-log4j12 1.7.21 log4j log4j 1.2.17

    引⼊ log4.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

    参考文章:

    《孙哥说Spring5》学习笔记_代码改变世界-CSDN博客 (opens new window)

    Spring 工厂设计模式、第一个Spring程序细节分析、整合日志框架_代码改变世界-CSDN博客 (opens new window)

    webflux基础
    Spring AOP 编程

    ← webflux基础 Spring AOP 编程→

    最近更新
    01
    国际象棋
    09-15
    02
    成语
    09-15
    03
    自然拼读
    09-15
    更多文章>
    Theme by Vdoing | Copyright © 2019-2023 zxpnet | 粤ICP备14079330号-1
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式