三、IOC 容器 - Bean 管理(xml 注入其他类型属性)

1. 字面量

null 值

<property name="address">
    <null/>
</property>

2. 属性值包含特殊符号

<!-- 属性值包含特殊符号 1. 把<>进行转义 &lt;,&gt;  2.把带特殊符号内容写到CDATA -->
<property name="address">
    <value>
        <![CDATA[<<南京>>]]>
    </value>
</property>

3. 注入属性-外部 bean

  1. 创建两个类Service类和dao类
  2. 在service类调用dao类 
  3. 在srping的配置文件中进行配置
// 创建dao属性
private UserDao userDao;
public void setUserDao(UserDao userDao) {
    this.userDao = userDao;
}
// 配置文件
<bean id="userService" class="com.study.service.UserService">
    <property name="userDao" ref="userDaoImpl"></property>
</bean>

<bean id="userDaoImpl" class="com.study.serviceImpl"></bean>

4. 注入属性-内部 bean 和级联赋值

package com.study.modules.myinclass.entity;


/**
* Emp.
*
* @author lipw
* @since 2021/11/26 16:08
*/
public class Emp {
    private String empName;
    private String gender;
    private Dept dept;


    public String getEmpName() {
        return empName;
    }


    public void add() {
        System.out.println(empName + "," + gender + "," + dept);
    }


    public void setEmpName(String empName) {
        this.empName = empName;
    }


    public String getGender() {
        return gender;
    }


    public void setGender(String gender) {
        this.gender = gender;
    }


    public Dept getDept() {
        return dept;
    }


    public void setDept(Dept dept) {
        this.dept = dept;
    }


    @Override
    public String toString() {
        return "Emp{" +
                "empName='" + empName + '\'' +
                ", gender='" + gender + '\'' +
                ", dept=" + dept +
                '}';
    }
}
package com.study.modules.myinclass.entity;


/**
* Dept.
*
* @author lipw
* @since 2021/11/26 16:09
*/
public class Dept {
    private String deptName;


    public String getDeptName() {
        return deptName;
    }


    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }


    @Override
    public String toString() {
        return "Dept{" +
                "deptName='" + deptName + '\'' +
                '}';
    }
}
<?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 -->
    <bean id="emp" class="com.study.modules.myinclass.entity.Emp">
        <property name="empName" value="张三"></property>
        <property name="gender" value="男"></property>


        <property name="dept">
            <bean id="dept" class="com.study.modules.myinclass.entity.Dept">
                <property name="deptName" value="战略部"></property>
            </bean>
        </property>
    </bean>
</beans>

4. 注入属性-级联赋值

        1. 第一种

<?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 -->
    <bean id="emp" class="com.study.modules.myinclass.entity.Emp">
        <property name="empName" value="张三"></property>
        <property name="gender" value="男"></property>
        <!-- 级联 -->
        <property name="dept" ref="dept"></property>
    </bean>
    <bean id="dept" class="com.study.modules.myinclass.entity.Dept">
        <property name="deptName" value="战略部"></property>
    </bean>
</beans>

        2. 第二种

<?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 -->
    <bean id="emp" class="com.study.modules.myinclass.entity.Emp">
        <property name="empName" value="张三"></property>
        <property name="gender" value="男"></property>
        <!-- 级联 -->
        <property name="dept" ref="dept"></property>
        <property name="dept.deptName" value="技术部"></property>
    </bean>
    <bean id="dept" class="com.study.modules.myinclass.entity.Dept">
        <property name="deptName" value="战略部"></property>
    </bean>
</beans>

5. xml 注入集合属性

  1. 注入数组类型属性
  2. 注入List集合属性
  3. 注入Map集合属性
// 实体对象
package com.study.modules.collection.entity;


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


/**
* Stu.
*
* @author lipw
* @since 2021/11/26 17:29
*/
public class Stu {


    private String[] courses;


    private List<String> list;


    private Map<String, String> map;


    private Set<String> mySet;


    public void togo() {
        System.out.println(courses + "," + list + "," + map + "," + mySet);
    }


    public void setCourses(String[] courses) {
        this.courses = courses;
    }


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


    public void setMap(Map<String, String> map) {
        this.map = map;
    }


    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }


    @Override
    public String toString() {
        return "Stu{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", map=" + map +
                ", mySet=" + mySet +
                '}';
    }
}

        配置文件:

<?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="stu" class="com.study.modules.collection.entity.Stu">
        <!-- 数组类型注入 -->
        <property name="courses" >
            <array>
                <value>语文</value>
                <value>数学</value>
            </array>
        </property>
        <!-- list集合注入 -->
        <property name="list" >
            <list>
                <value>英语</value>
                <value>德语</value>
            </list>
        </property>
        <!-- map 集合注入 -->
        <property name="map">
            <map>
                <entry key="name" value="体育"></entry>
                <entry key="age" value="4"></entry>
            </map>
        </property>
        <!-- set 集合注入 -->
        <property name="mySet" >
            <set>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>
    </bean>
</beans>

6. 在集合里面设置一个对象类型的值

package com.study.modules.collection.entity;


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


/**
* Stu.
*
* @author lipw
* @since 2021/11/26 17:29
*/
public class Stu {


    private String[] courses;


    private List<String> list;


    private Map<String, String> map;


    private Set<String> mySet;


    private List<Course> courseList;


    public void togo() {
        System.out.println(courses.toString() + "," + list + "," + map + "," + mySet +
                "," + courseList);
    }


    public void setCourseList(List<Course> courseList) {
        this.courseList = courseList;
    }


    public void setCourses(String[] courses) {
        this.courses = courses;
    }


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


    public void setMap(Map<String, String> map) {
        this.map = map;
    }


    public void setMySet(Set<String> mySet) {
        this.mySet = mySet;
    }


    @Override
    public String toString() {
        return "Stu{" +
                "courses=" + Arrays.toString(courses) +
                ", list=" + list +
                ", map=" + map +
                ", mySet=" + mySet +
                '}';
    }
}

course对象

package com.study.modules.collection.entity;


/**
* Course.
*
* @author lipw
* @since 2021/11/27 11:46
*/
public class Course {


    private String CourseName;


    public void setCourseName(String courseName) {
        CourseName = courseName;
    }


    @Override
    public String toString() {
        return "Course{" +
                "CourseName='" + CourseName + '\'' +
                '}';
    }

配置文件:

<?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="stu" class="com.study.modules.collection.entity.Stu">
        <!-- 数组类型注入 -->
        <property name="courses" >
            <array>
                <value>语文</value>
                <value>数学</value>
            </array>
        </property>
        <!-- list集合注入 -->
        <property name="list" >
            <list>
                <value>英语</value>
                <value>德语</value>
            </list>
        </property>
        <!-- map 集合注入 -->
        <property name="map">
            <map>
                <entry key="name" value="体育"></entry>
                <entry key="age" value="4"></entry>
            </map>
        </property>
        <!-- set 集合注入 -->
        <property name="mySet" >
            <set>
                <value>set1</value>
                <value>set2</value>
            </set>
        </property>


        <property name="courseList" >
            <list>
                <ref bean="course1" ></ref>
                <ref bean="course2" ></ref>
            </list>
        </property>
    </bean>


    <bean id="course1" class="com.study.modules.collection.entity.Course">
        <property name="courseName" value="Spring5框架" ></property>
    </bean>


    <bean id="course2" class="com.study.modules.collection.entity.Course">
        <property name="courseName" value="Mybatis框架" ></property>
    </bean>


</beans>

7. 提取list集合类型属性注入

实体类

package com.study.modules.collection.entity;


import java.util.List;


/**
* Book.
*
* @author lipw
* @since 2021/11/30 10:10
*/
public class Book {


    private List<String> list;


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


    public void say() {
        System.out.println(list.toString());
    }
}

配置文件:

<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util.xsd">
    <util:list id="bookList" >
        <value>易筋经</value>
        <value>九阳神功</value>
        <value>道德经</value>
    </util:list>


    <bean id="book" class="com.study.modules.collection.entity.Book">
        <property name="list" ref="bookList"></property>
    </bean>
</beans>

测试方法

 @Test
    public void test6() {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("bean6.xml");


        Book book = applicationContext.getBean("book", Book.class);
        book.say();


    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值