• 03-spring bean


    一、spring 基于 xml 的 IOC 环境搭建和入门

    1.pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>spring02</groupId>
        <artifactId>spring02</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <packaging>jar</packaging>
    
        <dependencies>
            <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.1.9.RELEASE</version>
            </dependency>
    
    
        </dependencies>
    
    </project>
    

    2.类

    将上次的耦合和解耦的笔记放进去

    3. bean.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--把对象的创建交给 spring 来管理-->
        <bean id="accountService" class="com.service.impl.AccountServiceImpl"/>
        <bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
    </beans>
    

    4.主函数

    public class Client {
        /**
         * 获取 spring 的 ioc 核心容器,并根据 id 获取对象
         * @param args
         */
        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
    
            IAccountService accountService=(IAccountService)ac.getBean("accountService");
    
            IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);
    
            System.out.println(accountService);
            System.out.println(accountDao);
        }
    }
    

    5.总结

    ApplicationContext的三个常用实现类:

    1. ClassPathXmlApplicationContext:他可以加载类路径下的配置文件,要求配置文件必须在类路径下,不在的话,加载不了。

    2. FileSystemXmlApplicationContext:他可以加载磁盘任意路劲下的配置文件(必须有访问权限)

    3. AnnotationConfigApplicationContext:它是用于读取注解创建容器的。

    获取配置文件对应实现类有两种方法:

    1. 强制转换
      • IAccountService accountService=(IAccountService)ac.getBean("accountService");
    2. IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);

    二、BeanFactory和ApplicationContext的区别

        public static void main(String[] args) {
            ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
    
            IAccountService accountService=(IAccountService)ac.getBean("accountService");
    
            IAccountDao accountDao=ac.getBean("accountDao",IAccountDao.class);
    
            System.out.println(accountService);
            System.out.println(accountDao);
    
            /*-------BeanFactory-------------*/
            Resource resource=new ClassPathResource("bean.xml");
            BeanFactory beanFactory=new XmlBeanFactory(resource);
            IAccountService as=(IAccountService)beanFactory.getBean("accountService");
            System.out.println(as);
        }
    }
    

    核心容器接口两个引发出的问题:

    1. ApplicationContext: (单例对象适用)

      他在创建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读完配置文件马上就创建配置文件的对象。

    2. BeanFactory: (多例对象适用)

      他在创建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据 id 获取对象了,什么时候才真正的创建对象。

    三、spring 中 bean 的细节之三种创建 Bean 对象的方式

    1. 第一种方式:使用默认构造函数创建。

    • 在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。 采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建
    <bean id="accountService" class="com.service.impl.AccountServiceImpl"/>
    <bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
    

    2.第二种方式:

    • 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
    package com.factory;
    
    import com.service.IAccountService;
    import com.service.impl.AccountServiceImpl;
    
    /*
    模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
    */
    public class InstanceFactory {
        public IAccountService getAccountService(){
            return new AccountServiceImpl();
        }
    }
    
    <bean id="instanceFactory" class="com.factory.InstanceFactory"/>
    <bean id="accountService2" factory-bean="instanceFactory" factory-method="getAccountService"/>
    

    3.第三种方式:

    • 使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
    package com.factory;
    
    import com.service.IAccountService;
    import com.service.impl.AccountServiceImpl;
    
    /*
    模拟一个工厂类(该类可能存在于jar包中,我们无法通过修改源码的方式来提供默认构造函数)
    */
    public class StaticFactory {
        public static IAccountService getAccountService(){
            return new AccountServiceImpl();
        }
    }
    
    
    <bean id="accountService3" class="com.factory.StaticFactory" factory-method="getAccountService"/>
    

    四、spring中bean的细节之作用范围

    bean 的作用范围调整

    1.bean标签订单scope属性:

    作用:用于指定bean作用范围 取值:常用的就是单例的于多例的

    1. singleton:单例的(默认值)

    2. prototype:多例的

    3. request:作用与web应用的请求范围

    4. session:作用于web应用的会话范围

    5. global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session

    对于 global-session 的理解

    global-session

    global session 可以理解为全局 session

    2.bean 对象的生命周期

    Ⅰ单例对象

    1. 出生:当容器创建时对象出生

    2. 活着:只要容器还在,对象一直活着

    3. 死亡:单例对象的生命周期和容器相同多例对象

    实现类

    public class AccountServiceImpl implements IAccountService {
    
        private IAccountDao accountDao=new AccountDaoImpl();
    
        public void saveAccount() {
            accountDao.saveAccount();
        }
    
        public void init(){
            System.out.println("对象初始化");
        }
    
        public void destroy(){
            System.out.println("对象销毁");
        }
    }
    

    xml 文件

        <bean id="accountService" class="com.service.impl.AccountServiceImpl" scope="singleton"
         init-method="init" destroy-method="destroy"/>
        <bean id="accountDao" class="com.dao.impl.AccountDaoImpl"/>
    

    主函数

    public class Client {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
            IAccountService as=ac.getBean("accountService",IAccountService.class);
    
            as.saveAccount();
            ac.close();
        }
    }
    

    运行结果

    对象初始化
    保存成功
    对象销毁
    

    Ⅱ多例对象

    1. 出生:当我们使用时 spring 框架为我们创建

    2. 活着:对象只要是在使用过程中就一直活着

    3. 死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收

  • 相关阅读:
    Java的家庭记账本程序(K)
    构建之法阅读笔记01
    Java的家庭记账本程序(J)
    iOS-CALayer实现简单进度条
    iOS-CALayer
    iOS-CAEmitterLayer(粒子效果)
    iOS-OpenGLES 简单渲染
    iOS-CoreImage简单使用
    iOS富文本-NSAttributedString简单封装
    iOS后台运行
  • 原文地址:https://www.cnblogs.com/zuiren/p/11415413.html
Copyright © 2020-2023  润新知