[Object] Spring acquaintance (Bean) and instantiating attributes injection (xml mode)


title: [Spring] acquaintance objects (Bean) is instantiated and injected attributes (xml mode)
DATE: 2018-08-29 17:35:15
Tags: [the Java, Web, Spring]
---

# Acquaintance of Spring Bean to instantiate and inject property

1. By way of example xml configuration.

  • The profile of common bean tag attributes
  • No parameters configured by the (common)
  • Created using a static factory
  • Example factories created

2. Properties injection.

  • There constructor parameter using injection properties
  • The method of using the injection set attributes (common)
  • Injection type of object attributes
  • Injecting the complex property

Xml configuration mode instantiated

  • Properties profile bean tag

(1) id attribute: from name, id attribute value of Title be named

  • id attribute value, can not contain special symbols
  • According to the configuration object id worth

(2) class properties: Create the full path where the object of the class

(. 3) the name attribute: id attribute and the same function, the id attribute value can not contain special symbols, but may contain special symbols which name attribute value

(4) scope properties

  • singleton: Default singleton
  • prototype: multiple cases

  • No reference object configuration example

//phone类:
package com.test.vo;
public class Phone {
    public void printTest() {
        System.out.print("Phone.......");
    }
}
<!--applicationContext.xml配置文件-->
<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<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.xsd">
    <bean id="phone" class="com.test.vo.Phone"></bean>
    </beans>
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        //加载配置文件,创建对象
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //得到配置创建的对象
        Phone Phone = (Phone) context.getBean("phone");
        //调用对象方法
        Phone.printTest();
    }
}

Note: java class in default no-argument constructor, if the class has already declared arg constructor, you need to manually declare constructor with no arguments.

  • Created using a static factory
//静态工厂类
package com.test.utils;
import com.test.vo.Phone;

public class BeanFactory {
    //静态方法,返回Phone对象
    public static Phone getPhone() {
        return new Phone();
    }

}
//创建的对象为Phone类对象不变
//配置文件改为:
<!--applicationContext.xml配置文件-->
<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<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.xsd">
        <!--class为静态工厂的路径,factory-method为工厂的方法-->
   <bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone"></bean>
    </beans>
//测试类
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Phone Phone = (Phone) context.getBean("phoneFa");
        Phone.printTest();
    }
}
  • Example factories created
//实列工厂类:
import com.test.vo.Phone;

public class BeanUFactory {
    //普通方法,返回Phone对象
    public Phone getPhone() {
        return new Phone();
    }
}
    配置文件修改:
    <!-- 1.先创建工厂对象 -->
    <!-- 2.再创建Phone对象 -->
    <bean id="BeanUFactory" class="com.test.utils.BeanUFactory"></bean>
    <bean id="phoneUFa" factory-bean="BeanUFactory" factory-method="getPhone"></bean>
//测试类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Phone Phone = (Phone) context.getBean("phoneUFa");
        Phone.printTest();
    }
}

Properties injection

  • There constructor parameter using injection properties:

Phone category rewritten as:

public class Phone {
    private String name;
    //显示声明无参构造
    public Phone() {}
    //有参构造
    public Phone(String name) {
        this.name=name;
    }
    public void printTest() {
        System.out.print(name+"Phone.......");
    }
}

applicationContext.xml configuration file amended as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<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.xsd">
        <!--class为静态工厂的路径,factory-method为工厂的方法-->
   <bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
   <!--name为构造方法的参数名,value为要将其设置的值-->
   <constructor-arg name="name" value="诺基亚"></constructor-arg>
   </bean>
    </beans>

Test categories:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Phone Phone = (Phone) context.getBean("phoneFa");
        Phone.printTest();
    }
}

result:

诺基亚Phone.......
  • The method of using the set injection properties:

Phone category rewritten as:

public class Phone {
    private String name;
    //set方法
    public void setName(String name) {
        this.name = name;
    }
    public void printTest() {
        System.out.print(name+"Phone.......");
    }
}

applicationContext.xml configuration file amended as follows:

<?xml version="1.0" encoding="UTF-8"?>
<!--引入约束-->
<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.xsd">
        <!--class为静态工厂的路径,factory-method为工厂的方法-->
   <bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
   <!--name为要注入的属性的名称,value为要将其设置的值-->
   <property name="name" value="三星"></property>
   </bean>
    </beans>

result:

三星Phone.......
  • Injection type of object attributes

New Person class:

public class Person {
    private String name;
    private String sex;
    private String age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }   
}

Phone type amended as follows:

package com.test.vo;

public class Phone {
    private String name;
    private Person person;
    
    //set方法
    public void setName(String name) {
        this.name = name;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
    public void printTest() {
        System.out.print(person.getName()+"::"+person.getAge()+"::"+person.getSex());
    }
}

Profiles amended as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="person" class="com.test.vo.Person" scope="prototype">
        <property name="name" value="小王"></property>
        <property name="sex" value="man"></property>
        <property name="age" value="11"></property>
    </bean>
    <bean id="phone" class="com.test.vo.Phone">
        <!-- 因注入的是对象写ref属性 -->
        <property name="person" ref="person"></property>
    </bean>
    </beans>

The same test method, the result is:

小王::11::man
  • Injecting the complex property

Phone type amended as follows:

package com.test.vo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class Phone {
    private String arr[];
    private List<Integer> list;
    private Map<String,String> map; 
    
    //set方法
    public void setArr(String[] arr) {
        this.arr = arr;
    }

    public void setList(List<Integer> list) {
        this.list = list;
    }

    public void setMap(Map<String, String> map) {
        this.map = map;
    }
    public void printTest() {
        System.out.println("arr:"+Arrays.toString(arr));
        System.out.println("list:"+list);
        System.out.println("map:"+map);
    }
}

Profiles amended as follows:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd">
    <bean id="phone" class="com.test.vo.Phone">
        <!-- 数组 -->
        <property name="arr">
            <list>
                <value>小米</value>
                <value>中兴</value>
                <value>华为</value>
            </list>
        </property>
        <!-- list集合 -->
        <property name="list">
            <list>
                <value>1</value>
                <value>2</value>
                <value>3</value>
            </list>
        </property>
        <!-- map集合 -->
        <property name="map">
            <map>
                <entry key="aa" value="lucy"></entry>
                <entry key="bb" value="bob"></entry>
                <entry key="cc" value="jerry"></entry>
            </map>
        </property>
    </bean>
    </beans>

The results are as follows:

arr:[小米, 中兴, 华为]
list:[1, 2, 3]
map:{aa=lucy, bb=bob, cc=jerry}

Guess you like

Origin www.cnblogs.com/flytree/p/11622685.html
Recommended