Spring之bean的实例化方式

2024-01-03 13:11:49

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

import lombok.Data;

@Data
public class People {
    private String name;
    private Integer age;
    private String eat;
}
<?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 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();
    }
}
<?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 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();
    }
}
<?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 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;
    }
}
<?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 id="people" class="factory.PeopleFactoryBean" />
</beans>

文章来源:https://blog.csdn.net/Lyhdreamer/article/details/135357575
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。