bean的属性赋值(p名称命名空间,set赋值。构造器赋值,List集合赋值,Map集合赋值)

JAVA学习网 2020-11-07 00:45:05

文章简介:

利用bean的属性赋值,通过setXXX(),p命名空间,构造器,给属性赋值。

利用bean的属性赋值,给List,Map 集合赋值。

实体类必须提供get/set/无参构造器,因为Spring是通过反射注入

本项目基于Maven创建。

 

 

 导入pom依赖

<dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.7.RELEASE</version>
      <scope>test</scope>
    </dependency>
pom依赖

 

 

创建ApplicationContext.xml (Spring配置文件)

<?xml version="1.0" encoding="UTF-8"?>
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <!--p名称命名空间-->
    <bean id="student6"
          class="com.charon.enty.Student"
          p:stuName="花花"
          p:stuAge="18"
          p:stuGender="女"
          p:stuDept="英语">
    </bean>

    <!--通过有参构造方法给bean的属性赋值-->
    <bean id="student1" class="com.charon.enty.Student">
        <property name="stuName" value="张三"/>
        <property name="stuAge" value="19"/>
        <property name="stuDept" value="数学"/>
        <property name="stuGender" value="男"/>
    </bean>

    <!--无参构造给bean赋值-->
    <bean id="student2" class="com.charon.enty.Student"></bean>

    <!--
          Spring 自动匹配合适的构造器;
         通过索引指定参数的位置;
        通过类型区分重载的构造器。
     -->
    <bean id="student3" class="com.charon.enty.Student">
        <constructor-arg name="stuName" value="Lucy" index="0" type="java.lang.String"/>
        <constructor-arg name="stuAge" value="18" index="1" type="java.lang.Integer"/>
        <constructor-arg name="stuGender" value="女" index="2" type="java.lang.String"/>
    </bean>

    <bean id="accout" class="com.charon.enty.Acount">
        <constructor-arg name="idCard" value="88888888" type="java.lang.String"/>
        <constructor-arg name="location" type="java.lang.String" value="中国"/>
    </bean>
    <!--应用外部bean-->
    <bean id="student4" class="com.charon.enty.Student">
        <constructor-arg name="stuName" value="娜娜"/>
        <constructor-arg name="stuAge" value="18"/>
        <constructor-arg name="stuGender" value="女"/>
        <constructor-arg name="acount" ref="accout"/>
    </bean>

    <!--给list集合赋值-->
    <bean id="list1" class="com.charon.enty.Acount">
        <property name="hobby">
        <list>
            <value type="java.lang.String">足球</value>
            <value type="java.lang.String">篮球</value>
        </list>
        </property>
    </bean>

    <!--给Map集合赋值-->
    <bean id="account3" class="com.charon.enty.Acount">
        <property name="hashMap">
            <map>
                <entry>
                    <key>
                        <value>name</value>
                    </key>
                    <value>小芳</value>
                </entry>
                <entry>
                    <key>
                        <value>age</value>
                    </key>
                    <value>18</value>
                </entry>
            </map>
        </property>
    </bean>
</beans>
ApplicationContext

 

创建Acount实体类

package com.charon.enty;

import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author Charon <1819248612@qq.com>
 * @create 2020-11-06-8:32
 * @Version 1.0.0
 */
public class Acount {
    private String idCard;
    private String location;
    private List<String> hobby;
    private HashMap<String,String> hashMap;

    public HashMap<String, String> getHashMap() {
        return hashMap;
    }

    public void setHashMap(HashMap<String, String> hashMap) {
        this.hashMap = hashMap;
    }

    public List<String> getHobby() {
        return hobby;
    }

    public void setHobby(List<String> hobby) {
        this.hobby = hobby;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public Acount() {
    }

    public Acount(String idCard, String location) {
        this.idCard = idCard;
        this.location = location;
    }

    public Acount(String idCard, String location, List<String> hobby) {
        this.idCard = idCard;
        this.location = location;
        this.hobby = hobby;
    }
}
Acount

 

 

创建实体类Student 类

package com.charon.enty;

/**
 * @Description TODO
 * @Author Charon <1819248612@qq.com>
 * @create 2020-11-06-0:39
 * @Version 1.0.0
 */
public class Student {
    private String stuName;
    private Integer stuAge;
    private String stuDept;
    private String stuGender;
    private Acount acount;

    public Acount getAcount() {
        return acount;
    }

    public void setAcount(Acount acount) {
        this.acount = acount;
    }

    public String getStuName() {
        return stuName;
    }

    public void setStuName(String stuName) {
        this.stuName = stuName;
    }

    public Integer getStuAge() {
        return stuAge;
    }

    public void setStuAge(Integer stuAge) {
        this.stuAge = stuAge;
    }

    public String getStuDept() {
        return stuDept;
    }

    public void setStuDept(String stuDept) {
        this.stuDept = stuDept;
    }

    public String getStuGender() {
        return stuGender;
    }

    public void setStuGender(String stuGender) {
        this.stuGender = stuGender;
    }

    public Student(String stuName, Integer stuAge, String stuGender) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuGender = stuGender;
    }

    public Student(String stuName, Integer stuAge, String stuDept, String stuGender) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuDept = stuDept;
        this.stuGender = stuGender;
    }

    public Student(String stuName, Integer stuAge, String stuGender, Acount acount) {
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.stuGender = stuGender;
        this.acount = acount;
    }

    public Student() {
    }
}
Student

 

 

创建测试类StudentTests

package com.test;

import com.charon.enty.Acount;
import com.charon.enty.Student;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashMap;

/**
 * @Description TODO
 * @Author Charon <1819248612@qq.com>
 * @create 2020-11-06-0:45
 * @Version 1.0.0
 */
public class StudentTest {
    ApplicationContext applicationContext;
    @Before
    public void before(){
        //定义Spring配置文件的位置
        String xmlPath = "ApplicationContext.xml";
        //初始化Spring配置文件,加载配置文件
        applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    }


    /**
     * 测试通过有参构造给bean赋值
     */
    @Test
    public void test(){
         Student student = (Student) applicationContext.getBean("student1");
         System.out.println(student.getStuName());
    }

    /**
     * 测试通过有参构造给bean赋值
     */
    @Test
    public void test1(){
        Student student = (Student) applicationContext.getBean("student2");
        student.setStuName("李四");
        System.out.println(student.getStuName());
    }

    /**
     *  Spring 自动匹配合适的构造器;
     */
   @Test
    public void test3(){
       Student student3 = (Student)applicationContext.getBean("student3");
       System.out.println(student3.getStuName());
       System.out.println(student3.getStuAge());
       System.out.println(student3.getStuDept());
   }
    /**
     * 测试引用外部bean给对象赋值
     */
    @Test
    public void test4(){
        Student student = (Student) applicationContext.getBean("student4");
        System.out.println(student.getStuName());
        System.out.println(student.getAcount().getIdCard());
    }

    /**
     * 测试给List集合赋值结果
     */
    @Test
    public void test5(){
        Acount acount = (Acount)applicationContext.getBean("list1");
        for (int i = 0; i < acount.getHobby().size(); i++) {
            System.out.println(acount.getHobby().get(i));
        }

    }

    /**
     * 测试给Map集合赋值结果
     */
    @Test
    public void test6(){
        Acount acount = (Acount)applicationContext.getBean("account3");
        HashMap<String, String> hashMap = acount.getHashMap();
        System.out.println(hashMap.get("name"));
        System.out.println(hashMap.get("age"));
    }
}
StudntTest

 

阅读(2308) 评论(0)