2-Spring-xml based configuration bean

Keywords: Java Spring xml

Spring - configuring bean s based on xml

1, Four methods of Bean instantiation

Premise: all classes managed by Spring are required to provide open and parameter free constructs.
Tell Spring how to get the object of the class through the Bean tag

1.1 nonparametric construction method (most commonly used in development)

First write a pojo class

public class User {
    private Integer id;
    private String name;

    public User(){
        System.out.println("Call default construct");
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

Then tell Spring how to manage

    <!--    Give Way Spring Manage current User class-->
    <bean id = "user" class="org.buaa.pojo.User">
    </bean>

Finally, use it

    public void testUser(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
        /* result
            Call default construct
            User{id=null, name='null'}
        */
    }

1.2 static plant method

Static factory method: write a static method in a class, and then return the object of a Bean in this method.

First write a basic factory

public class UserFactory {
    public static User getUserInstance(){
        return new User();
    }
}

Tell Spring how to configure

    <!--Demonstrates how to get an object of a class through a static factory-->
    <!--adopt factory-method The label tells us that we use a static factory-->
    <bean id = "user" class = "org.buaa.factory.UserFactory" factory-method="getUserInstance"></bean>

In fact, Spring does not manage the UserFactory class, but the User class

Write test

    public void testStaticFactory(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // The final result of this method is a User object, so you should receive a User object
        // User is obtained by static factory method
        User user = (User) context.getBean("user");
        System.out.println(user);
        /*
            Call default construct
            Call default construct
            User{id=null, name='null'}
        */
    }

You can see that the constructor is executed twice. Reason: when loading the core configuration file, Spring will load each tag, but we did not delete #1.1 tags, so the core configuration file is as follows. It can be seen that Spring manages two users and will initialize once, so it will call the construct twice.

    <!--    Give Way Spring Manage current User class-->
    <bean id = "user" class="org.buaa.pojo.User"></bean>

    <!--    
        Demonstrates how to get an object of a class through a static factory
        id:Returned by the current static factory Bean Unique identification of
        class: Static factory class
        factory-method: Static method of configured static factory
    -->
    <bean id = "userFactory" class = "org.buaa.factory.UserFactory" factory-method="getUserInstance"></bean>

In fact, if we only write the code to load the core configuration file, we will call the construction twice.
Therefore, the core configuration file should be loaded only once. However, a Spring core container can manage multiple objects from the same class and new

1.3 example factory method

Instance factory method: similar to static factory, but factory itself is an instance.
Provide a non static method in the class, and return the Bean object to be obtained in this method

Example factory

public class InstanceFactory {
    public User getUserInstance(){
        return new User();
    }
}

to configure

    <!--  Configure the instance factory's own Bean  -->
    <bean id = "instanceFactory" class = "org.buaa.factory.InstanceFactory"></bean>
    <!--
        The instance factory itself has been Spring Management, and the instance factory returns another Bean Object,
        During configuration, you need to pass factory-bean Property to reference an existing factory Bean
        Need to pass factory-method To specify the instance factory bean Return another in bean Method of
    -->
    <bean id = "user2" factory-method="getUserInstance" factory-bean="instanceFactory"></bean>

test

    public void testInstanceFactory(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user2");
        System.out.println(user);
    }

1.4 FactoryBean mode (Spring bottom layer is used more)

Often interview
Is a way to instantiate beans.

First, write a custom class to implement the FactoryBean interface

/**
 *  Use the FactoryBean interface provided by Spring to instantiate the object of a class
 *  Customize a class to implement the FactoryBean interface
 */
public class UserInstance implements FactoryBean<User> {
    
    // The Bean object to be managed is created in the getObject method
    @Override
    public User getObject() throws Exception {
        return new User();
    }

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

to configure

    <!--
        Configuration passed FactoryBean Interface acquisition User example
        Current configuration Bean Yes, although not User,But it did FactoryBean Interface, and generics write User,
        Spring When instantiating, you will find that it implements FactoryBean Interface, then you can copy from the getObject Method
    -->
    <bean id = "user3" class = "org.buaa.factory.UserInstance"></bean>

test

1.5 difference between factorybean and BeanFactory

FactoryBean: an interface used to create a specified Bean instance, which is used to tell Spring how to instantiate a specific class object
Bean factory: it manages beans. It is one of the top-level interfaces in spring (ApplicationContext is the sub interface of its sub interface). It represents the object that the spring container loads the spring core configuration file

2, Bean related knowledge

2.1 scope of bean

When configuring a bean, there is a label called scope, which represents its scope

<bean id = "user4" class = "org.buaa.pojo.User" scope="singleton"></bean>`
<bean id = "user5" class = "org.buaa.pojo.User" scope="prototype"></bean>`
  • Default: singleton
  • Singleton: singleton. The object associated with the current Bean tag is always unique (common)
  • prototype: multiple instances. After a Bean is configured as multiple instances, as long as the Bean is obtained through the Spring core object, the Bean object will be recreated

Each getBean under singleton gets the same result.
A new object will be created for each getBean under prototype.

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user1 = (User) context.getBean("user4");
User user2 = (User) context.getBean("user4");
user1 == user2;// true

User user3 = (User) context.getBean("user5");
User user4 = (User) context.getBean("user5");
user3 == user4;// false

2.2 Bean life cycle

When the Bean is created / destroyed, let it execute some methods.
pojo is as follows:

public class User {
    ...
    public void init(){
        System.out.println("Methods that can be executed during initialization");
    }
    public void destory(){
        System.out.println("Methods that can be executed during destruction");
    }
    ...
}

to configure

    <!--  to configure Bean cover Spring During management, you can specify some of your own initialization and destruction methods -->
    <bean id = "user5" class = "org.buaa.pojo.User" init-method="init" destroy-method="destory"></bean>

test

    public void testUserInitAndDestory(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) context.getBean("user5");
        System.out.println(user);
        /*
            Call default construct
            Methods that can be executed during initialization
            User{id=null, name='null'}
        */
        ((ClassPathXmlApplicationContext)context).close();
        /* Methods that can be executed during destruction */
    }

Note that there is no way to execute the destruction. If you want to execute it, you need to manually close the Spring container, while the ApplicationContext interface does not provide the close method, so you must force it into a ClassPathXmlApplicationContext object and then call close.

Note that if the Bean is set to prototype multi instance, it will not be executed even if it is manually closed.

2.3 Bean attribute injection

2.3.1 construction method

Disadvantages: the value passed in is written in the xml file. Of course, it can also be changed to "${}" by referencing the configuration file, but it is very troublesome
pojo

public class User {
    ...
    public User(Integer id, String name){
        this.id = id;
        this.name = name;
    }
    ...
}

to configure

    <!--
        Demonstrate how to construct Bean Attribute injection data for
        use constructor-arg Parameters required by label configuration construction method
            name:Variable name
            value: value
            index: The first index Parameters
            ref: You can pass a complex object
    -->
    <bean id = "car" class = "org.buaa.pojo.Car"/>
    <bean id="user6" class = "org.buaa.pojo.User">
        <constructor-arg name = "name" value = "jackl"/>
        <constructor-arg index = "1" value ="1"/>
        <constructor-arg name = "car" ref = "car"/>
    </bean>

2.3.2 setter method injection

First write the set method in the pojo class, and then the configuration file
pojo

public class Car {
    ...
    private String color;
    private Integer price;
    private String type;

    public void setColor(String color) {
        this.color = color;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public void setType(String type) {
        this.type = type;
    }
    ...
}

to configure

    <!--
        Demonstration passed set Method to Bean Attribute injection data for
        use property Parameters required by label configuration construction method
            name:set Posterior XXX Name, e.g setColor Method, corresponding name = "Color"
            value: value
            ref: You can pass a complex object
    -->
    <bean id = "car" class = "org.buaa.pojo.Car">
        <property name = "Color" value = "yellow"/>
        <property name = "Price" value = "100"/>
        <property name = "Type" value = "yellow"/>
    </bean>

2.3.3 set attribute injection

In fact, the essence is still the first two injection methods, but the injection set is somewhat different

    <!--
        Demonstration passed set Method to Bean Collection injection properties for,requirement pojo One exists in the class setLists Methods.
    -->
    <bean id = "student" class = "org.buaa.pojo.Student">
        <property name = "lists">
            <list>
                <value>Mountain climbing</value>
                <value>go to sea</value>
            </list>
        </property>
        <!--Show me map How to inject-->
        <property name = "maps">
            <map>
                <entry key = "addr" value = "Beijing"/>
            </map>
        </property>
    </bean>
    <!--
        Demonstration passed set Method to Bean Collection injection properties for
    -->
    <bean id = "student" class = "org.buaa.pojo.Student">
        <constructor-arg name = "set"/>
            <set>
                <value>Mountain climbing</value>
                <value>go to sea</value>
            </set>
        </constructor-arg>
    </bean>

Tags and attributes in xml configuration

Properties in bean Tags

  • Class: essentially, it provides services for reflection. Tell the framework what this class is, for example: class = "com.service.UserDao"
  • id: unique identification
  • Factory method: the static method of the configured static factory
  • Factory bean: refers to an existing factory bean
  • Scope: used to declare the scope of the Bean, singleton: Singleton; prototype: multiple cases
  • Init method: declares a method to execute when creating a Bean
  • Destroy method: a method is executed when declaring to destroy a Bean. It is only executed when the Bean is a singleton and Spring needs to be manually closed

Constructor Arg tag

Is the sub tag of the bean, which is used to pass in data using the constructor

  • Name: parameter name on construction method
  • Value: the value passed in
  • Index: the index parameter on the construction method
  • ref: you can transfer a complex object, such as custom and wrapped objects

property tag set

Is the sub tag of the bean, which is used to pass in data using the set method

  • Name: XXX name after set, such as setColor method, corresponding to name = "Color"
  • Value: the value passed in
  • ref: you can transfer a complex object, such as custom and wrapped objects

Collection label

If the injected data is a collection, use a specific collection label

<list>
    <value>value</value>
</list>
<set>
    <value>value</value>
</set>
<map>
    <entry key = "k", value = "v"/>
</map>
<array>
    <value>11</value>
    <value>12</value>
    <value>13</value>
</array>
<props>
    <!--Properties Class, similar map,Save attributes for persistence, such as environment variables-->
    <prop key = "sex">male</prop>
    <prop key = "height">2.0</prop>
</props>

Posted by Dream$of$uccess on Sun, 10 Oct 2021 03:28:59 -0700