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
         http://www.springframework.org/schema/beans/spring-beans.xsd">

<!--配置我们的Dog类  spring 框架在底层 
  通过反射的机制  执行了我们的构造方法-->
<bean id="dog" class="cn.pb.dao.impl.Dog"></bean>


</beans>

直接这样配置一个bean的话,相当于是调用这个Dog类的无参构造器,如果无参构造器不在,Spring上下文创建对象的时候就会报错。

无参构造器加setter方法注入field的值

类:

package com.mc.base.learn.spring.bean;

public class Person {

    private String name;
    private Integer id;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", id=" + id + "]";
    }

}

bean配置:

<?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">

    <bean class="com.mc.base.learn.spring.bean.Person" id="person">
        <property name="name" value="LiuChunfu"></property>
        <property name="id" value="125"></property>
    </bean>
    
</beans>

就bean标签下再用property标签来设置name和value

如果有field是引用型的,就不用value,用ref = "xxx",xxx就是某个bean的id或者name

利用有参数的构造方法:

类代码:

package com.mc.base.learn.spring.bean;

public class Person {

    private String name;
    private Integer id;
    
    public Person(String name, Integer id) {
        super();
        this.name = name;
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person [name=" + name + ", id=" + id + "]";
    }

}
View Code

bean配置:

<?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">

    <bean class="com.mc.base.learn.spring.bean.Person" id="person">
        <constructor-arg name="id" value="123"></constructor-arg>
        <constructor-arg name="name" value="LiuChunfu"></constructor-arg>
    </bean>
    
</beans>
View Code

就是在bean标签下面,有个<constructor-arg>标签来确定这个bean的构造器的参数咯。

二、静态的工厂方法

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory静态工厂类
 */
public class AnimalFactory {
    /**
     * 可以看到程序没有走构造方法
     */
    public AnimalFactory(){
        System.out.println("静态工厂的无参构造====");
    }
//静态工厂,不会走无参构造
    public static Animal getDog(){
        System.out.println("工厂中静态获取Dog实例的方法");
        return new Dog();
    }
}

然后配置bean:

<?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">

   

    <!--02.通过静态工厂 来创建我们对象的实例
     工厂的本身构造不会执行 因为我们的方法是静态的 !类只会被加载,不会被实例化!
     getDog必须是静态的-->
    <bean id="dog" class="cn.pb.util.AnimalFactory" factory-method="getDog"></bean>  

</beans>

三、动态工厂——就非static的getDog方法

package cn.pb.dao;

import cn.pb.dao.impl.Dog;

/**
 * AnimalFactory动态工厂类
 */
public class AnimalFactory {
    /**
     * 程序会先创建工厂实例 再调用getDog()方法
     */
    public AnimalFactory(){
        System.out.println("动态工厂的无参构造====");
    }
//动态工厂 会先走无参构造  创建实例
    public Animal getDog(){
        System.out.println("工厂中动态工厂获取Dog实例的方法");
        return new Dog();
    }
}

bean的配置:

<?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">

   

    <!--03.动态工厂创建 对象的实例-->
    <bean id="factory" class="cn.pb.util.AnimalFactory"></bean><!-- 调用哪个工厂里的哪个方法  来创建对象  对象的id是dog-->
    <bean id="dog" factory-bean="factory" factory-method="getDog"/>

</beans>

这个Dog的对象呢,需要一个实例的一个getDog方法嘛,所以要先有这个实例Factory的bean,然后再在这个factory-bean上填这个factory的bean。

参考文章:

例子分别在这两篇当中搞过来的hh

https://www.cnblogs.com/LiuChunfu/p/5574383.html——《Spring创建对象的方式3种方式》这个是真的三种方法,下面那个emm

https://www.cnblogs.com/lyb0103/p/7611826.html——《Spring笔记03(Spring创建对象的三种方式) 》工厂方法还有静态工厂的用的是这篇的例子

原文地址:https://www.cnblogs.com/wangshen31/p/10533143.html