目录

spring- Bean实例化的姿势有几种

李羽秋
李羽秋 2023年03月19日  ·  阅读 1,106

spring- Bean实例化的姿势有几种

1. 通过静态方法进行构建

User类

public class User {

    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    public static User createUser(){
        User user = new User();
        user.setId(1L);
        user.setName("liyuqiu");
        return user;
    }
}

配置文件

<?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="user-by-static-method" class="org.liyuqiu.spring.ioc.domain.User" factory-method="createUser"/>
</beans>

函数入口

public class BeanInstantinationDemo {
    public static void main(String[] args) {
        //配置XML配置文件
        //启动Spring应用上下文
        BeanFactory beanFactory =  new ClassPathXmlApplicationContext("META-INF/dependency-creation-context.xml");
        User user = beanFactory.getBean("user-by-static-method", User.class);
        System.out.println(user);
    }
}

2.通过抽象方法实例化

配置文件

<?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="user-by-instance-method" factory-bean="UserFactory" factory-method="createUser"/>

    <bean id="UserFactory" class="org.liyuqiu.spring.ioc.factory.DefaultUserFactory"/>


</beans>

接口

public interface UserFactory {
     public default User createUser(){
        return User.createUser();
    }
}

实现类

public class DefaultUserFactory implements UserFactory{

}

函数入口

public class BeanInstantinationDemo {
    public static void main(String[] args) {
        //配置XML配置文件
        //启动Spring应用上下文
        BeanFactory beanFactory =  new ClassPathXmlApplicationContext("META-INF/dependency-creation-context.xml");
        User user = beanFactory.getBean("user-by-instance-method", User.class);
        System.out.println(user);
    }
}

3. 通过factoryBean实例化

UserFactoryBean类

public class UserFactoryBean implements FactoryBean {


    @Override
    public Object getObject() throws Exception {
        return User.createUser();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }
}

配置文件

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


<!--    factoryBean实例化-->
    <bean id="user-by-factory-bean" class="org.liyuqiu.spring.ioc.factory.UserFactoryBean"/>

</beans>

函数入口

public class BeanInstantinationDemo {
    public static void main(String[] args) {
        //配置XML配置文件
        //启动Spring应用上下文
    
BeanFactory beanFactory =  new ClassPathXmlApplicationContext("META-INF/dependency-creation-context.xml");
        User user = beanFactory.getBean("user-by-factory-bean", User.class);
        System.out.println(user);
    }
}
      

分类: Spring
标签: