初识Spring对象(Bean)实例化及属性注入(xml方式)

Posted flytree

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初识Spring对象(Bean)实例化及属性注入(xml方式)相关的知识,希望对你有一定的参考价值。


title: 【初识Spring】对象(Bean)实例化及属性注入(xml方式)
date: 2018-08-29 17:35:15
tags: [Java,Web,Spring]
---

?#初识Spring之Bean实例化及属性注入

1.通过xml配置的方式进行实例化。

  • 配置文件中bean标签的常用属性
  • 通过无参构造(常用)
  • 使用静态工厂进行创建
  • 使用实例工厂进行创建

2.属性注入。

  • 使用有参数构造方法注入属性
  • 使用set方法注入属性(常用)
  • 注入对象类型属性
  • 注入复杂类型属性

xml配置的方式进行实例化

  • 配置文件中bean标签的属性

(1)id属性:起名称,id属性值名称任意命名

  • id属性值,不能包含特殊符号
  • 根据id值得到配置对象

(2)class属性:创建对象所在类的全路径

(3)name属性:功能和id属性一样的,id属性值不能包含特殊符号,但是在name属性值里面可以包含特殊符号

(4)scope属性

  • singleton:默认值,单例
  • prototype:多例

  • 无参构造实例化对象

//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();
    

注:java类中默认有无参构造方法,若类中已声明了有参构造,则需手动声明无参构造方法。

  • 使用静态工厂进行创建
//静态工厂类
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();
    
  • 使用实例工厂进行创建
//实列工厂类:
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();
    

属性注入

  • 使用有参数构造方法注入属性:

Phone类改写为:

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配置文件修改为:

<?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>

测试类:

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();
    

结果:

诺基亚Phone.......
  • 使用set方法注入属性:

Phone类改写为:

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配置文件修改为:

<?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>

结果:

三星Phone.......
  • 注入对象类型属性

新建Person类:

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类修改为:

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());
    

配置文件作如下修改:

<?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>

测试方法不变,结果为:

小王::11::man
  • 注入复杂类型属性

Phone类修改为:

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);
    

配置文件作如下修改:

<?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>

结果如下:

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

以上是关于初识Spring对象(Bean)实例化及属性注入(xml方式)的主要内容,如果未能解决你的问题,请参考以下文章

初识Spring Framework——Bean注册Bean依赖注入

初识Spring Framework——Bean注册Bean依赖注入

Spring之Bean的注入

Java Spring5之IOC容器

Spring Bean生命周期详解

Spring入门第三课