9. spring bean Foundation (1)

Keywords: Spring xml Session Attribute

9. spring bean Foundation (1)

This article mainly explains the following knowledge points.

  • 1. An example of using bean s in spring
  • 2. Injecting values to bean properties
  • 3. Loading multiple configuration files
  • 4. Example of spring internal bean s
  • 5. Scope of spring bean s

1. An example of using bean s in spring

Referencing bean s under the same configuration file

<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-2.5.xsd">

    <bean id="HelloWorld" class="...">
        <property name="outputGenerator" >
            <ref local="Custom"/>
        </property>
    </bean>

    <bean id="Custom" class="..." />
</beans>

Beans that reference other configuration files

    <!-- first.xml -->
    <bean id="HelloWorld" class="...">
        <property name="outputGenerator" >
            <ref bean="Custom"/>
        </property>
    </bean>
    <!-- second.xml -->
    <bean id="Custom" class="..."/>

2. Injecting values to bean properties

Suppose there is now a bean class as follows:

package com.main;

public class HelloWorld{
    private String name;
    private int state;
    //setter and getter methods
    //toString methods
}

There are three ways to inject values into bean attributes in the Spring framework
The first is the normal approach

    <bean id="HelloWorld" class="com.main">
        <property name="name">
            <value>jack</value>
        </property>
        <property name="state">
            <value>1</value>
        </property>
    </bean>

Second: shortcuts

    <bean id="HelloWorld" class="com.main">
        <property name="name" value="jack"/>
        <property name="state" value="1"/>
    </bean>

Third: P-labeling

Note: To use the p tag at this point, you must declare it in the spring xml configuration file
xmlns:p="http://www.springframework.org/schema/p"

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="HelloWorld" class="com.main" 
             p:name="jack" p:type="1" />
</beans>

3. spring loads multiple configuration files

Background Note: In the actual development project, there are many bean configuration files, so a configuration file repository is needed to manage and use a bean configuration file uniformly, which will reduce a lot of search work.

Assume that there are two bean configuration files in the project
First.xml (path is classpath:/com/main/first/first.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="FirstBean" class="com.main.first" />
</beans>

Second.xml (path is classpath:/com/main/second/second.xml)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="SecondBean" class="com.main.first" />
</beans>

Organize the above two bean configuration files in an XML file, temporarily named Spring-Module.xml

Then Spring-Module.xml is configured as follows:

<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-2.5.xsd">

        <import resource="com/main/first/first.xml"/>
        <import resource="com/main/second/second.xml"/>

</beans>

So far, the work of loading multiple configuration files has been completed.
The methods for obtaining bean s from Spring-Module.xml are as follows

ApplicationContext context = 
            new ClassPathXmlApplicationContext(Spring-Module.xml);

Object object = (Object)context.getBean("value");

4. Instances of internal bean s

Note: In some scenarios, we may need to set an internal bean for the properties of a bean (that is, only allow ourselves to use it), which can be achieved in two ways.

The first is to construct internal bean s by setter method

<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-2.5.xsd">

    <bean id="Customer" class="...">

        <property name="person">
            <!--Statement internal bean -->
            <bean class="...">
                <!-- inside bean Attribute -->
                <property name="name" value="yiibai" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </property>
    </bean>
</beans>

The second is to construct internal bean s by constructing methods

<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-2.5.xsd">

    <bean id="Customer" class="...">

        <constructor-arg>
            <!--Statement internal bean -->
            <bean class="...">
                <!-- inside bean Attribute -->
                <property name="name" value="yiibai" />
                <property name="address" value="address1" />
                <property name="age" value="28" />
            </bean>
        </constructor-arg>
    </bean>
</beans>

5. Scope of spring bean s

The scope of spring bean s is to tell the spring container which type of bean instance to choose to return to the caller. There are five scopes of support.

  1. Singleton - Returns the same bean instance each time
  2. Prototype - Returns a new instance every time
  3. Request - Returns a Bean instance for each HTTP request
  4. Session - Returns a bean instance for each HTTP session
  5. Global Session - Returns a bean instance of a global HTTP session

Singleton (no scope attribute is set in the bean)

<bean id="normalBean" class="..."/>

prototype

<bean id="normalBean" class="..." scope="prototype" />

Use annotations to use scopes

package com.main;

@Service
@Scope("prototype")
public class{
    //property
    //methods
}

To use annotated scopes, component scanning must be enabled

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

       <context:component-scan base-package="com.main" />

</beans>

Posted by erikhillis on Mon, 01 Apr 2019 02:27:28 -0700