This is the fourth day of my participation in Gwen Challenge

BeanFactory object registration and dependency binding

Similar to the binding methods of IoC mentioned above, there are also three types: direct coding, external configuration and annotations.

1. Direct coding

public static void main(String[] args)
{
    DefaultListableBeanFactory beanRegistry = new DefaultListableBeanFactory();
    BeanFactory container = (BeanFactory)bindViaCode(beanRegistry);
    FXNewsProvider newsProvider = (FXNewsProvider)container.getBean("djNewsProvider");
    newsProvider.getAndPersistNews();
}

public static BeanFactory bindViaCode(BeanDefinitionRegistry registry)
{
    AbstractBeanDefinition newsProvider = new RootBeanDefinition(FXNewsProvider.class,true);
    AbstractBeanDefinition newsListener = new RootBeanDefinition(DowJonesNewsListener.class,true);
    AbstractBeanDefinition newsPersister = new RootBeanDefinition(DowJonesNewsPersister.class,true);
    // Register the bean definition in the container
    registry.registerBeanDefinition("djNewsProvider", newsProvider);
    registry.registerBeanDefinition("djListener", newsListener);
    registry.registerBeanDefinition("djPersister", newsPersister);
    // Specify dependencies
    //1. Can be injected via constructor
    ConstructorArgumentValues argValues = new ConstructorArgumentValues();
    argValues.addIndexedArgumentValue(0, newsListener);
    argValues.addIndexedArgumentValue(1, newsPersister);
    newsProvider.setConstructorArgumentValues(argValues);
    PropertyValues = new MutablePropertyValues(); /** *2. *propertyValues.addPropertyValues(new PropertyValue("newsListener", newsListener)); *propertyValues.addPropertyValues(new PropertyValue("newPersister", newsPersister)); newsProvider.setPropertyValues(propertyValues); * * /
    // The binding is complete
    return (BeanFactory)registry;
}
Copy the code

The BeanFactory just one interface, and ultimately to the implementation of the interface need a practical bean-managed DefaultListableBeanFactory is such a more general the BeanFactory implementation class. DefaultListableBeanFactory except indirectly realized the BeanFactory interface, also implements the BeanDefinitionRegistry interface, the interface is in the implementation of the BeanFactory Bean registration management roles. Basically, the BeanFactory interface only defines how to access the methods of managed beans within the container, and the concrete implementation classes of each BeanFactory are responsible for registering and managing the concrete beans. The BeanDefinitionRegistry interface definition abstracts the Bean’s registration logic. Typically, the concrete BeanFactory implementation class implements this interface to manage the registration of beans. The relationship between them is as follows:

Now that we look back at the above binding code, we can see that there is a “silver lining”.

1. In the main method, first, to construct a DefaultListableBeanFactory as BeanDefinitionRegistry, then hand it over to the registered bindViaCode method for concrete object, finally perform corresponding logic.

2. In the bindViaCode method, the corresponding BeanDefinition is constructed for the corresponding business object, and RootBeanDefinition is used as the implementation class of BeanDefinition. Once constructed, these BeanDefinitions are registered with the BeanDefinitionRegistry passed in as method parameters. Later, because the constructor injection, so I need ConstructorArgumentValues injection related rely for it.

2. Use an external configuration file

Spring’s IoC container supports two configuration file formats: Properties file format and XML file format.

Spring’s IoC container has a uniform approach when using external configuration files. Usually, depending on the external configuration file format, you need to give the corresponding BeanDefinitionReader implementation class. The corresponding implementation class of BeanDefinitionReader is responsible for reading and mapping the corresponding configuration file content to BeanDefinition. The mapped BeanDefinitionDefinition is then registered with a BeanDefinitionRegistry, which completes the registration and loading of the Bean.

For loading the Properties configuration format, Spring provides org. Springframework. Beans. Factory. Support. PropertiesBeanDefinitionReader classes are used to load Properties configuration format. So, we don’t need to implement BeanDefinitionReader ourselves, just provide the corresponding configuration file according to the class’s read rules.

For loading XML configuration format, before Spring2.x, XML configuration file adopts Document Type Definition (DTD) to implement Document format constraints. After 2.x, the constraint mode of XSD (XML Schema Definition) was introduced.

3. Annotation method

Tag related classes with @autowired and @Component.