【Spring学习及总结09】创建bean对象的四种方式


目前,我们讲了以下几个知识点:
1.如何获取bean实例,有几种方式
2.如何为bean的属性赋值(通过构造方法,通过set方法)
3.bean的作用域

下面一个知识点主要来讲如何创建bean,java里面创建对象首先想到的就是通过构造方法创建对象,那么现在如何创建bean对象呢?

创建bean对象的四种方式

1.使用默认的构造函数创建bean对象

在Spring的配置文件中使用bean标签,配置id和class属性之后,且没有其他属性和标签时,采用的就是默认构造函数创建bean标签,此时如果类中没有构造函数,就无法创建对象。

首先写一个实体类:

@Data
public class Car {
    private String carName;
    private int price;
    private String color;
}

实体类中有一个默认的空参数构造函数,拿到ClassPath类路径以后,通过反射创建car对象。

<bean id="car" class="com.hh.pojo.Car">
</bean>

2.通过静态工厂方法创建bean

静态工厂:工厂本身 不需要创建对象,通过静态方法调用
对象=工厂类.工厂方法名();
实例工厂:工厂本身需要创建对象
工厂类 工厂对象=new 工厂类();
工厂对象.getAirPlane("张三");

首先写一个实体类AirPlane

@Data
public class AirPlane {
    //飞机名
    private String name;
    //飞机异常
    private String yc;
    //飞机载客量
    private Integer personNum;
    //飞机机长姓名
    private String jzName;
}

写一个静态工厂类 AirPlaneStaticFactory,用来创建飞机对象AirPlane
由于静态工厂类中的方法为静态的,因此不需要创建静态工厂对象,直接通过类名调用方法即可。

//静态工厂
public class AirPlaneStaticFactory {
    //AirPlaneStaticFactory.getAirplane
    //静态工厂类中的获取飞机对象的方法时静态的,可以直接通过类名获取
    public static AirPlane getAirplane(String jzName){
        System.out.println("静态工厂正在造飞机....");
        AirPlane airPlane = new AirPlane();
        airPlane.setName("吉祥");
        airPlane.setJzName(jzName);
        airPlane.setPersonNum(500);
        airPlane.setYc("198.89m");
        return airPlane;
    }
}

写配置配置文件,用来通过静态工厂类返回AirPlane对象
class:指定静态工厂全类名
factory-method:指定工厂方法
constructor-arg:可以为方法传递参数

<bean id="airPlane01" class="com.hh.factory.AirPlaneStaticFactory"
factory-method="getAirplane">
    <constructor-arg value="张三"/>
</bean>

写测试类:

public class MyTest4 {
    //根据spring的配置文件得到ioc容器对象
    ApplicationContext context =
            new ClassPathXmlApplicationContext("spring4.xml");
    @Test
    public void test(){
        System.out.println("容器启动完成....");
        AirPlane airPlane01 =
                context.getBean("airPlane01", AirPlane.class);
        System.out.println(airPlane01);
    }
}

在这里插入图片描述

3.通过实例工厂方法创建bean

实例工厂:工厂本身需要创建对象
工厂类 工厂对象=new 工厂类();
工厂对象.getAirPlane("张三");

先写一个实例工厂类,实例工厂类需要创建工厂对象,因为里面的方法不是静态方法。

//实例工厂
public class AirPlaneInstanceFactory {
    //new AirPlaneInstanceFactory().getAirplane
    public AirPlane getAirplane(String jzName){
        System.out.println("静态工厂正在造飞机....");
        AirPlane airPlane = new AirPlane();
        airPlane.setName("吉祥");
        airPlane.setJzName(jzName);
        airPlane.setPersonNum(500);
        airPlane.setYc("198.89m");
        return airPlane;
    }
}

配置文件:
1.先配置出实例工厂对象
2.factory-bean:指定使用哪个工厂实例
3.factory-method:使用哪个工厂方法

<!--实例工厂的创建-->
<bean id="airPlaneInstanceFactory" 
     class="com.hh.factory.AirPlaneInstanceFactory">
</bean>
<bean id="airplane02" class="com.hh.pojo.AirPlane"
factory-method="getAirplane" factory-bean="airPlaneInstanceFactory">
   <constructor-arg value="李四"/>
</bean>

写一个测试类:

public class MyTest4 {
    //根据spring的配置文件得到ioc容器对象
    ApplicationContext context =
            new ClassPathXmlApplicationContext("spring4.xml");
    @Test
    public void test1(){
        System.out.println("容器启动完成....");
        AirPlane airPlane02 =
                context.getBean("airPlane02", AirPlane.class);
        System.out.println(airPlane02);
    }
}

在这里插入图片描述

4.实现FactoryBean接口

实现了FactoryBean接口的类是Spring可以认识的工厂类,Spring会自动调用工厂方法创建实例

首先写一个类实现FactoryBean接口

public class MyFactoryBean implements FactoryBean<Book>{
    //getObject:工厂方法,返回创建的对象
    public Book getObject() throws Exception {
        Book book = new Book();
        book.setBookName(UUID.randomUUID().toString());
        return book;
    }
    // 返回创建对象的类型
    public Class<?> getObjectType() {
        return null;
    }
    //isSingleton:是单例吗?
    public boolean isSingleton() {
        return false;
    }
}

在配置文件中配置这个类:

<!--FactoryBean:Spring规定的一个接口-->
<bean id="myFactoryBean" class="com.hh.controller.MyFactoryBean">
</bean>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我一直在流浪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值