Java学习笔记-spring-Bean实例化

负一、目录


零、bean 实例化方式

1. 构造器实例化 (常用)

1. 静态工厂实例化

2. 实例工厂实例化

一、构造器实例化

Spring 容器通过Bean 对应类中默认无参构造方法来实例化Bean。

//Bean1.java
package com.itheima.instance.constructor;
public class Bean1 {
}
<!--beans1.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
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
      id: Bean唯一标识符;
      class:Bean实例具体实现类,必须是一个完整类名,使用类的全限定名
    -->
    <bean id="bean1" class="com.itheima.instance.constructor.Bean1"/>
</beans>
//InstanceTest1
package com.itheima.instance.constructor;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InstanceTest1 {
    public static void main(String[] args) {
      //配置文件路径
      String xmlPath = "com/itheima/instance/constructor/beans1.xml";
      //加载配置文件
      ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
      //实例化Bean
      Bean1 bean = (Bean1) applicationContext.getBean("bean1");
      System.out.println(bean);
    }
}

二、静态工厂实例化

创建一个静态工厂类来创建Bean的实例,Bean配置中的class属性所指定的不再是Bean实例实现的类,而是静态工厂类,同时还需要使用 factory-method 属性指定所创建的静态工厂方法。

//bean2.java
package com.itheima.instance.static_factory;

public class Bean2 {
}
//MyBean2Factory.java
package com.itheima.instance.static_factory;

public class MyBean2Factory {
    public static Bean2 createBean() {
        return new Bean2();
    }
}
<!--bean2.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
    http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--
      class:静态工厂类完全限定名
      factory-method:定义所需的工厂方法
    -->
    <bean id="bean2"
          class="com.itheima.instance.static_factory.MyBean2Factory"
          factory-method="createBean"/>
</beans>
//InstanceTest2
package com.itheima.instance.static_factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InstanceTest2 {
    public static void main(String[] args) {
        String xmlPath = "com/itheima/instance/static_factory/beans2.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("bean2"));
    }
}

三、实例化工厂实例化

配置文件中需要实例化的Bean通过 factory-bean 属性指向配置的实例工厂,然后使用 factory-method 确定使用的是工厂中的哪个方法。

//Bean3.java
package com.itheima.instance.factory;

public class Bean3 {
}
//MyBean3Factory.java
package com.itheima.instance.factory;

public class MyBean3Factory {
    public MyBean3Factory() {
        System.out.println("bean3 工厂实例化中");
    }

    public Bean3 createBean() {
        return new Bean3();
    }
}
<!--bean3.xml-->
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xis="http://www.w3.org/2001/XMLSchema-instance"
       xis:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置工厂-->
    <bean id="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"/>
    <!--配置实例工厂和使用的方法-->
    <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"/>
</beans>
//InstanceTest3.java
package com.itheima.instance.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class InstanceTest3 {
    public static void main(String[] args) {
        String xmlPath = "com/itheima/instance/factory/beans3.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        System.out.println(applicationContext.getBean("bean3"));
    }
}

本节源码下载地址:

https://pan.baidu.com/s/1gbHdTRGozS4MHMFivaO9jw

提取码:gr6m

https://download.csdn.net/download/gangzhucoll/11014530

上一篇:【转载】花20分钟写的-大白话讲解如何给github上项目贡献代码


下一篇:开发者学堂课程干货总结——Dubbo 分布式服务治理实践(一)