• Spring5之IOC容器中IOC操作之Bean管理(二)之p名称空间注入、外部bean、内部bean、级联赋值


    Bean管理的多种属性注入方式

    1、p名称空间注入

    1.1 使用p名称空间注入,可以简化基于xml配置方式

    (1)添加p名称空间在配置文件中

    
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    (2)进行属性注入,在bean标签里面进行操作

    
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd">
      
      
      <bean id="book" class="com.example.spring5.domain.Book" p:bname="盒饭" p:bauthor="阿姨">
      bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2、xml注入其他类型属性

    2.1 字面量

    (1)null值

    
    <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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
            
            <bean id="book" class="com.example.spring5.domain.Book">
                
                <property name="bname" value="西瓜">property>
                <property name="bauthor" value="瓜农">property>
                
                <property name="address">
                    <null/>
                property>
            bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    (2)属性值包含特殊符号

    <!--属性值包含特殊符号
        1<>进行转义
        2  把带特殊符号内容写到CDATA
    -->
    <property name="address">
         <value><![CDATA[南瓜]]></value>
    </property>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.2 注入属性-外部bean

    (1)创建两个类service类和dao类
    UserDao

    package com.example.spring5.dao;
    
    public interface UserDao {
        public void update();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    UserDaoImpl

    package com.example.spring5.dao;
    
    public class UserDaoImpl implements UserDao{
    
        @Override
        public void update() {
            System.out.println("dao update........");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2)在service调用dao里面的方法

    package com.example.spring5.service;
    
    import com.example.spring5.dao.UserDao;
    
    public class UserService {
        //创建UserDao类型属性,生成set方法
        private UserDao userDao;
    
        public void setUserDao(UserDao userDao) {
            this.userDao = userDao;
        }
    
        public void add(){
            System.out.println("service add.........");
            userDao.update();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (3)在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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--1 service和dao对象创建-->
        <bean id="userService" name="com.example.spring5.service.UserService">
            <!--注入userDao对象
            name属性值:类里面属性名称
            ref属性:创建userDao对象bean标签id值
            -->
            <property name="userDao" ref="userDaoImpl"/>
        </bean>
        <bean id="userDaoImpl" class="com.example.spring5.dao.UserDaoImpl"/>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    3、注入属性-内部bean和级联赋值

    (1)一对多关系:部门和员工
    一个部门有多个员工,一个员工属于一个部门
    部门是一,员工是多
    (2)在实体类之间表示一对多关系,员工表示所属部门,使用对象类型属性进行表示
    Dept类

    package com.example.spring5.bean;
    
    //部门类
    public class Dept {
        private String dname;
    
        public void setDname(String dname) {
            this.dname = dname;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Emp类

    package com.example.spring5.bean;
    
    //员工类
    public class Emp {
        private String ename;
        private String gender;
    
        //员工属于某一个部门,使用对象形式表示
        private Dept dept;
    
        public void setDept(Dept dept) {
            this.dept = dept;
        }
    
        public void setEname(String ename) {
            this.ename = ename;
        }
    
        public void setGender(String gender) {
            this.gender = gender;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    (3)在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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
           <bean id="emp" class="com.example.spring5.bean.Emp">
               <!--设置两个普通属性-->
               <property name="ename" value="lucy"></property>
               <property name="gender" value="女"></property>
               <!--设置对象类型属性-->
               <property name="dept">
                   <bean id="dept" class="com.example.spring5.bean.Dept">
                       <property name="dname" value="安保部"></property>
                   </bean>
               </property>
           </bean>
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    (4)测试样例

    package com.example.spring5;
    
    import com.example.spring5.bean.Emp;
    import com.example.spring5.domain.Orders;
    import com.example.spring5.service.UserService;
    import org.junit.jupiter.api.Test;
    import org.junit.runner.RunWith;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("classpath:bean1.xml")
    class OneToMoreTest {
    
        @Test
        public void testAdd(){
            //1 加载spring配置文件
            ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");
    
            //2 获取配置创建的对象
            Emp emp = context.getBean("emp", Emp.class);
            System.out.println(emp);
            emp.add();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    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 id="emp" class="com.example.spring5.bean.Emp">
                <!--设置两个普通属性-->
                <property name="ename" value="lucy"/>
                <property name="gender" value="女"/>
                <!--级联赋值-->
                <property name="dept" ref="dept"/>
            </bean>
            <bean id="dept" class="com.example.spring5.bean.Dept">
                <property name="dname" value="财务部"/>
            </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    (2)第二种写法
    先在对应实体类里添加get方法

    public Dept getDept() {
            return dept;
    }
    
    
    • 1
    • 2
    • 3
    • 4

    再写对应配置文件

    <?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="emp" class="com.example.spring5.bean.Emp">
                <!--设置两个普通属性-->
                <property name="ename" value="lucy"/>
                <property name="gender" value="女"/>
                <!--级联赋值-->
                <property name="dept" ref="dept"/>
                <property name="dept.dname" value="技术部"/>
            </bean>
            <bean id="dept" class="com.example.spring5.bean.Dept">
                <property name="dname" value="财务部"/>
            </bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    Leetcode-2 两数相加
    【LeetCode】1282. 用户分组
    Flink系列之Flink中StateBackend深入剖析和应用
    【Android】AndroidStudio自动下载的Gradle大东西从系统盘里怎样转移出去
    常见git提交规范
    树形排序(Tree Sort)
    ES6新特性
    linux离线环境中进入docker镜像安装python包
    Apache Hudi Timeline:支持 ACID 事务的基础
    MATLAB算术运算符、关系运算符、逻辑运算符、按位集合运算符
  • 原文地址:https://blog.csdn.net/qq_46106857/article/details/126140059