Spring之bean的实例化方式

1.使用构造方法实例化bean(利用反射)

import lombok.Data;

@Data
public class People {
    private String name;
    private Integer age;
    private String eat;
}

<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 id="people" class="People">
        <property name="name" value="张三"/>
        <property name="age" value="18"/>
        <property name="eat" value="吃饭"/>
    bean>
beans>

注:无参构造方法如果不存在,将抛出异常BeanCreationException

2.静态工厂实例化对象

package domain;

import lombok.Data;

@Data
public class People {
    private String name;
    private Integer age;
    private String eat;
}

package factory;

import domain.People;

public class PeopleFactory {
    public static People getPeople() {
        return new People();
    }
}

<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 id="people" class="factory.PeopleFactory" factory-method="getPeople">
        <property name="name" value="张三"/>
        <property name="age" value="18"/>
        <property name="eat" value="吃饭"/>
    bean>
beans>

3.实例工厂初始化bean

package domain;

import lombok.Data;

@Data
public class People {
    private String name;
    private Integer age;
    private String eat;
}

package factory;

import domain.People;

public class PeopleFactory {
    public People getPeople() {
        return new People();
    }
}

<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 id="PeopleFactory" class="factory.PeopleFactory"/>
    <bean id="people" factory-bean="PeopleFactory" factory-method="getPeople">
        <property name="name" value="张三"/>
        <property name="age" value="18"/>
        <property name="eat" value="吃饭"/>
    bean>
beans>

4.对实例工厂初始化bean的方式进行优化

package domain;

import lombok.Data;

@Data
public class People {
    private String name;
    private Integer age;
    private String eat;
}

package factory;

import domain.People;
import org.springframework.beans.factory.FactoryBean;

public class PeopleFactoryBean implements FactoryBean<People> {
    @Override
    public People getObject() throws Exception {
        return new People();
    }

    @Override
    public Class<?> getObjectType() {
        return People.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

<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 id="people" class="factory.PeopleFactoryBean" />
beans>

你可能感兴趣的:(Spring,spring,java,后端)