spring框架的bean对象生成方式----静态工厂的形式、实例工厂的形式

前期类的准备

  • Maven的pop.xml进行相关包的导入
 <!-- 为 Spring 核心提供了大量扩展。可以找到使用 Spring ApplicationContext 特 性时所需的全部类, 可以进行spring.xml配置文件的读取和使用
         -->
 		<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
         <!-- 使用注解的形式,将来set、get...方法 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
  • dog.java类------>构造器私有,不对外通过构造器
package com.factory;


import lombok.Data;

@Data
public class Dog {
    private String name;
    private int age;

    private Dog(String name, int age) {
        this.name = name;
        this.age = age;
    }
}
  • DogFactory工厂类,通过反射获得Dog.java字节码对象,从而调用其构造器方法,生产dog对象。
package com.factory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public  class DogFactory {
    public static Dog createDog(String name,int age) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //反射获取Dog构造器
        Class<?> aClass = Class.forName("com.factory.Dog");
        //根据字节码对象获取构造器
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class, int.class);
        //更改可以添加
        constructor.setAccessible(true);
        //创建对象
        Object o = constructor.newInstance(name, age);
        return (Dog)o;
    }
}

下面使用spring将dog对象的创建,交给其管理,存在两种方式进行工厂创建对象

普通生成方式-------使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作

  • 格式
<bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
  • 建立spring.xml文件,进行创建bean,将对象的创建,交给spring容器
<?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">
    <bean id="d1" class="com.factory.DogFactory" factory-method="createDog">
        <constructor-arg index="0" value="小明"/>
        <constructor-arg index="1" value="5"/>
    </bean>
</beans>
  • 测试
package com.factory;

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

import java.lang.reflect.InvocationTargetException;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        /*
        * 工厂模式
        * 1.创建Dog类,构造器私有化
        * 2.建立工厂类,通过方式方法创建Dog对象
        *3.测试
        * */

//        //静态工厂创建对象
        //加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Dog d1 = (Dog)applicationContext.getBean("d1");
        System.out.println(d1);
    }
}
  • 结果
    在这里插入图片描述

使用实例工厂的形式创建bean

  • 格式
<bean id="factoryBeanId" class="工厂类"></bean>
    <beanfactory-bean="factoryBeanId" factory-method="factoryMethodName">
    </bean>
  • 将dog的工厂类的createDog()方法,变为非静态方法,即方法前的修饰符static去掉,否则该bean创建成功
package com.factory;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class DogFactory2 {
    private Dog createDog(String name,int age) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //反射获取Dog构造器
        Class<?> aClass = Class.forName("com.factory.Dog");
        //根据字节码对象获取构造器
        Constructor<?> constructor = aClass.getDeclaredConstructor(String.class, int.class);
        //更改可以添加
        constructor.setAccessible(true);
        //创建对象
        Object o = constructor.newInstance(name, age);
        return (Dog)o;
    }
}

  • 建立spring.xml文件,进行创建bean,将对象的创建,交给spring容器
<?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">
    <bean id="d2" class="com.factory.DogFactory2"></bean>
    <bean id="d3" factory-bean="d2" factory-method="createDog">
        <constructor-arg index="0" value="小青"/>
        <constructor-arg index="1" value="10"/>
    </bean>
</beans>
  • 测试
package com.factory;

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

import java.lang.reflect.InvocationTargetException;

public class Main {
    public static void main(String[] args) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
        /*
        * 工厂模式
        * 1.创建Dog类,构造器私有化
        * 2.建立工厂类,通过方式方法创建Dog对象
        *3.测试
        * */
        *         //动态工厂创建对象
        //加载配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        Dog d2 = (Dog)applicationContext.getBean("d3");
        System.out.println(d2);
    }
}
  • 结果
    在这里插入图片描述

普通的创建bean对象------>直接调用类的构造器,创建其对象

  • 格式
bean id="bean对象id" class="对应要创建对象的类名"/>
  • 使用跟上面的测试一样使用

总结

  • 静态创建bean,其工厂方法要有static修饰
  • 实例创建bean,其工厂方法不能是static修饰
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值