Study Guide Spring bean configuration Chapter III (unfinished)

The third chapter bean configuration

In this chapter, we will cover the following:

  1. bean definition inheritance:
  2. How to solve the argument to the constructor bean class:
  3. How to configure the primitive type (e.g., int, float, etc.), the type of collection (e.g. java.util.List, java.util.Map) and other custom types (e.g., Address) such as bean properties and constructor arguments;
  4. How to specify bean properties and constructor parameters by using the p namespace and c respectively namespace to use application context XML file becomes simple;
  5. Write your own Spring's factory classes FactoryBean interfaces, run to create the bean instance;
  6. Modular bean configuration.
bean definition inheritance

We saw in Chapter 1 and Chapter 2, bean defined application context XML file specifies the bean class and its dependencies fully qualified name. In some scenarios, in order to make the bean definition is not so long, you might want to bean definition inherits from another bean definition configuration information. Such a scenario described below MyBank application.

MyBank - bean definition inherits examples

In the previous chapter, we learned MyBank application to access the database through DAO. Suppose MyBank DatabaseOperations application defines a class can interact with the database, all the DAO MyBank applications rely on DatabaseOperations class to perform database operations, as shown below.

image

The figure shows the FixedDepositDao DatabaseOperations and PersonalBankingDao class depends on the class. The following application context XML file shows bean definitions of these classes.

<bean id="databaseOperations"
    class="sample.spring.chapter01.bankapp.utils.DatabaseOperations"></bean>

<bean id="personalBankingDao" class="sample.spring.chapter01.bankapp.dao.PersonalBankingDaoImpl">
    <property name="databaseOperations" ref="databaseOperations" />
</bean>

<bean id="fixedDepositDao" class="sample.spring.chapter01.bankapp.dao.FixedDepositDaoImpl">
    <property name="databaseOperations" ref="databaseOperations" />
</bean>

In the above xml, personalBankingDao and definitions used databaseOperations fixedDepositDao bean property to reference DatabaseOperations instance. This means PersonalBankingDaoImpl and FixedDepositDaoImpl setDatabaseOperations class defines a method to allow the container Spring injection DatabaseOperations instance.

If a plurality of bean applications share a common set of configuration (attribute constructor parameters, etc.), you can create a bean definition, as another bean definition parent definition. In personalBankingDao fixedDepositDao bean and definitions, the common configuration is databaseOperations properties. The following shows personalBankingDao fixedDepositDao bean definitions and how to define inherited from the parent bean databaseOperations property.

<bean id="databaseOperations" class="sample.spring.chapter03.bankapp.utils.DatabaseOperations"></bean>

<bean id="daoTemplate" abstract="true">
    <property name="databaseOperations" ref="databaseOperations" />
</bean>

<bean id="fixedDepositDao" parent="daoTemplate" class="sample.spring.chapter03.bankapp.dao.FixedDepositDaoImpl">
</bean>

<bean id="personalBankingDao" parent="daoTemplate" 
      class="sample.spring.chapter03.bankapp.dao.PersonalBankingDaoImpl"></bean>

In the above the xml, daoTemplate bean defined fixedDepositDao personalBankingDao bean and define a shared common configuration. Because fixedDepositDao and personalBankingDao bean definitions need databaseOperations dependency, daoTemplate bean definition uses Element defines databaseOperations dependencies. parent property element specifies the name of the bean definition inherits the configuration. As the parent property fixedDepositDao and personalBankingDao bean definition is daoTemplate, so they inherit the property databaseOperation daoTemplate bean definition.

If abstract characteristic value of the element is set to true, the bean definition is abstract. In the above xml, daoTemplate bean abstract definition. Please note, Spring container will not attempt to create a bean definition with the corresponding abstract bean.

注意:
    抽象 bean 不能作为其他 bean 定义的依赖项,也就是说,不能使用 <property> 或 <constructor-arg> 元素来引用抽象  bean 。

You may have noticed daoTemplate bean definition does not specify class characteristics. If the parent class is not specified characteristic bean definition, it is necessary to specify the characteristics of the sub-class bean definition (e.g. fixedDepositDao and personalBankingDao) in. Note that if you do not specify class characteristics, you must define the abstract bean, so the Spring container will not try to create the corresponding bean instance.

To verify fixedDepositDao and personalBankingDao bean defines whether inherited databaseOperations property daoTemplate bean definition. Please execute the following java code. BankApp main method in class calls fixedDepositDao personalBankingDao bean and Method, and examples of these methods on DatabaseOperations bean calls. You will notice, BankApp the main method to run successfully. Without throwing an exception. If no instance of the injection fixedDepositDao DatabaseOperations and personalBankingDao bean, then the code will throw java.lang.NullPointerException.

package sample.spring.chapter03.bankapp;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import sample.spring.chapter03.bankapp.controller.FixedDepositController;
import sample.spring.chapter03.bankapp.controller.PersonalBankingController;
import sample.spring.chapter03.bankapp.domain.FixedDepositDetails;

public class BankApp {
    private static Logger logger = Logger.getLogger(BankApp.class);

    public static void main(String args[]) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                "classpath:META-INF/spring/applicationContext.xml");

        FixedDepositController fixedDepositController = context
                .getBean(FixedDepositController.class);
        fixedDepositController.submit(context
                .getBean(FixedDepositDetails.class).setDepositAmount(100)
                .setEmail("[email protected]").setId(1).setTenure(10));
        logger.info("Your fixed deposit details : " + fixedDepositController.get());

        PersonalBankingController personalBankingController = context
                .getBean(PersonalBankingController.class);
        logger.info(personalBankingController.getMiniStatement());
        
    }
}

The following figure shows fixedDepositDao and personalBankingDao bean definition, bean definition inherits is how it works

image

The figure shows the defined fixedDepositDao personalBankingDao bean and inherited attributes databaseOperations daoTemplate bean definitions from (and to block fixedDeopsitDa personalBankingDao identified in property attribute). The figure also describes bean instances Spring container does not attempt to create a corresponding defined daoTemplate bean, because he is marked as abstract.

What inherited

Child bean definition inherits it from the parent bean definition configuration information:

  1. Property, by Elements specified.
  2. Constructor parameters, by Elements specified.
  3. The method of covering (see Section 4, 5)
  4. Initialization and destruction methods (see Chapter 5);
  5. Factory method, by Property element factory method specified (see section 2, 3, learn how static and instance factory method for creating a bean)

Guess you like

Origin www.cnblogs.com/train99999/p/11964898.html