Spring4 learning review of the road 04- Configuration Bean (in)

Other references Bean

   Bean component applications often need to cooperate with each other to complete the functionality of the application, so they requested that Bean can visit each other, must be specified in the reference Bean Bean configuration file. It can be used in the configuration file Bean <ref> element or ref attribute specifies a reference to the Bean properties or parameters of the Bean constructor. It may contain declarations of the attributes or Bean constructor, the so called Bean internal Bean. Specifically look at the code, the new class on the Book before the Student class, (a student has a book). code show as below:

Book.java

package com.lql.spring01;

/**
 * @author: lql
 * @date: 2019.10.11
 * Description:
 * Created with IntelliJ IDEA
 */
public class Book {

    private String name;

    private double price;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

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

Student.java 

package com.lql.spring01;/**
 * @author: lql
 * @date: 2019.09.24
 * Description:
 */
public class Student {

    private String name;

    private Integer age;

    private Book book;

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 

Then configure bean Book applicationContext.xml class in the configuration file;

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 

    <-!      configuration bean id: identification class: class path name: attribute name, attribute name for the note set method, the attribute value is not a variable name: property value   -> 
    < the bean ID = "Student" class = "com.lql.spring01 .Student " > 
        < Property name =" name " value="lql"></property>
        <property name="age" value="18"></property>
        <property name="book" ref="book"></property>
        <!--<property name="book">
            <ref bean="book"></ref>
        </property>-->
    </bean>

    <bean id="book" class="com.lql.spring01.Book">
        <>Property> </= "Three Kingdoms"value= "name"nameProperty
        <property name="price" value="79.9"></property>
    </bean>
</beans>

 

Code is used <ref> element or the ref attribute examples to reference, is no problem in the test:

public  void the Hello () { 

        System.out.println ( "the Hello:" + the this .getName () + ", Book:" + the this .book.getName ()); 
    } 

    public  static  void main (String [] args) { // Create IOC container 
        the ApplicationContext App = new new the ClassPathXmlApplicationContext ( "the applicationContext.xml" );
         // Get the bean 
        Student Student = app.getBean ( "Student", Student. class );
         // call the method 
        student.Hello (); 
    } 

/ / output: Hello: lql, Book: Three Kingdoms

 

Internal Bean

  Examples Bean only when a specific attribute to use, it can be declared as internal Bean, Bean statements contained directly inside <property> or <constructor-arg> element, the need to provide any id or name attribute, inside Bean You can not use anywhere else.

Actually very simple, configuration changes are as follows:

 <bean id="student" class="com.lql.spring01.Student">
        <property name="name" value="lql"></property>
        <property name="age" value="18"></property>
        <!--内部bean-->
        <property name="book">
            <bean class="com.lql.spring01.Book">
                <property name="price" value="100.0"></property>
                <property name="name" value="时间简史"></property>
            </bean>
        </property>
</bean>

 

Testing is not the problem, the print is omitted here.

And concatenated attributes NULL value

You can use special <null /> element tags for Bean string or other type of object attributes inject a null value ( of little significance, understanding to know what is going on the line ), of course, Spring also supports cascading configuration attributes.

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 

    <-!      configuration bean id: identification class: class path name: attribute name, attribute name for the note set method, the attribute value is not a variable name: property value   -> 
    < the bean ID = "Student" class = "com.lql.spring01 .Student " > 
        < Property name =" name " value="lql"></property>
        <property name="age" value="18"></property>
        <property name="book">
            <null/>
        </property>
    </bean>
    <bean id="book" class="com.lql.spring01.Book">
        <property name="name" value="三国演义"></property>
        <property name="price" value="79.9"></property>
    </bean>
</bean>

 

As a result, book references is null. Code concatenation following properties:

<? XML Version = "1.0" encoding = "UTF-. 8" ?> 
< Beans xmlns = "http://www.springframework.org/schema/beans" 
       xmlns: the xsi = "http://www.w3.org / 2001 / XMLSchema-instance " 
       xsi: schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd " > 

    <-!      configuration bean id: identification class: class path name: attribute name, attribute name for the note set method, the attribute value is not a variable name: property value   -> 
    < the bean ID = "Student" class = "com.lql.spring01 .Student " > 
        < Property name =" name " value="lql"></property>
        <property name="age" value="18"></property>
        <property name="book" ref="book"></property>
        <property name="book.price" value="19.9"></property>
        <property name="book.name" value="java从入门到放弃"></property>
    </bean>
    <bean id="book" class="com.lql.spring01.Book">
        <property name="name" value="三国演义"></property>
        <property name="price" value="79.9"></property>
    </bean>
</bean>

 

Print result: the Hello: LQL, Book: the Java from entry to give up , instead of the Three Kingdoms. Note that we must provide setter (). I give an assignment book and tune cascade attribute, <Property name = "book" ref = "book"> </ Property>, if there is no direct reference to the first feasible feasible using cascading attribute it? The answer is no, which is supported in Struts2, but Spring is not supported. It will be reported: Value of nested property 'book' is null. (But also in many cases less than cascade property, you know to understand on the line).

 

Guess you like

Origin www.cnblogs.com/-qilin/p/11653239.html