欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring源码-Bean实例化

程序员文章站 2022-05-21 19:18:24
...

Bean实例化

常规方式:

  1. 通过构造器(配置元信息:XML,Java注解和JavaAPI)
  2. 静态工厂方法(XML和JavaAPI)
  3. Bean工厂(XML和JavaAPI)
  4. 通过FactoryBean (XML,Java注解和JavaAPI)

特殊方式:

  1. ServiceLoaderFactoryBean 配置元信息:XML,Java注解和JavaAPI)
  2. AutowireCapableBeanFactory#createBean
  3. BeanDefinitionRegistry#registerBeanDefinition

静态方法XML例子

public static void main(String[] args) {
        ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("bean-instance.xml");
        User bean = applicationContext.getBean(User.class);
        System.out.println(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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="user" name="@3assdf;asd"  class="org.example.beans.domain.User"
     factory-method="createUser"
    />

</beans>

静态工厂XML:

    <?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <bean id="user_by_factory"  factory-bean="userFactory" factory-method="createUser"/>
    <bean id="userFactory"  class="org.example.beans.factory.DefaultUserFactory"/>
</beans>
public interface UserFactory {
    default User createUser (){
        return User.createUser();
    }
}
public class DefaultUserFactory implements UserFactory{
}

FactoryBean 例子

<bean id="userFactoryBean"  class="org.example.beans.factory.UserFactoryBean"/>

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

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

Bean 的特殊实例化方式:

ServiceLoader:
资源目录配置该文件

META-INF/services/org.example.beans.factory.UserFactory

org.example.beans.factory.DefaultUserFactory
<bean id="userFactoryServiceLoader" class="org.springframework.beans.factory.serviceloader.ServiceLoaderFactoryBean">
        <property name="serviceType" value="org.example.beans.factory.UserFactory"/>
    </bean>
public static void main(String[] args) {
    ApplicationContext applicationContext  = new ClassPathXmlApplicationContext("bean-instance.xml");
    ServiceLoader<UserFactory> bean = applicationContext.getBean("userFactoryServiceLoader", ServiceLoader.class);
    displayServiceLoader(bean);
}

public static void displayServiceLoader(ServiceLoader<UserFactory> factories) {
        Iterator<UserFactory> iterator = factories.iterator();
        while (iterator.hasNext()) {
            UserFactory userFactory = iterator.next();
            System.out.println(userFactory.createUser());
        }

    }

AutowireCapableBeanFactory 例子

AutowireCapableBeanFactory autowireCapableBeanFactory = applicationContext.getAutowireCapableBeanFactory();
        autowireCapableBeanFactory.createBean(DefaultUserFactory.class);