• 07set注入级联属性和特殊字符及表达式语言


    级联属性赋值(了解)

    概述

    级联属性赋值就是给某个对象属性的属性赋值,就是给对象关联的对象的属性赋值

    Clazz班级类

    public class Clazz {
        private String name;
    
        public Clazz() {
        }
    
        public Clazz(String name) {
            this.name = name;
        }
        //set和get方法以及toString方法
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    Student有clazz属性,表示学生属于哪个班级

    public class Student {
        private String name;
        private Clazz clazz; 
        // 要想给clazz属性采用级联属性赋值其必须提供getter方法
        public Clazz getClazz(){
            return clazz;
        }
        public Student() {
        }
        public Student(String name, Clazz clazz) {
            this.name = name;
            this.clazz = clazz;
        }
        //set和get方法以及toString方法
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    采用一般方式直接给Clazz对象的属性赋值

    
    <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="clazzBean" class="com.powernode.spring6.bean.Clazz">
            <property name="name" value="高三一班"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用级联属性给Clazz对象的属性赋值需要注意两点

    • Student类的clazz属性必须提供getter方法,只有这样Spring才能通过调用getClazz()方法拿到对应的Clazz对象然后给它的name属性赋值
    • 标签配置的顺序不能颠倒: 在Student对象中只有先给clazz属性赋值后,我们才能拿到对应的Clazz对象然后给它的name属性赋值
    
    <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="student" class="com.powernode.spring6.beans.Student">
            
            <property name="name" value="张三"/>
            
            <property name="clazz" ref="clazzBean"/>
            
            <property name="clazz.name" value="高三一班"/>
        bean>
        
        <bean id="clazzBean" class="com.powernode.spring6.beans.Clazz"/>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    测试程序

    @Test
    public void testCascade(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-cascade.xml");
        Student student = applicationContext.getBean("student", Student.class);
        System.out.println(student);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注入null和空字符串和特殊字符

    注入空字符串

    注入空字符串使用value/标签或指定属性value=""

    public class Vip {
        private String email;
        public void setEmail(String email) {
            this.email = email;
        }
        @Override
        public String toString() {
            return "Vip{" +
                    "email='" + email + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    <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="vipBean" class="com.powernode.spring6.beans.Vip">
            
            <property name="email" value=""/>
            
            <property name="email">
                <value/>
            property>
        bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注入null

    注入null使用null/标签或者不为该属性赋值

    
    <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="vipBean" class="com.powernode.spring6.beans.Vip" />
        
        <bean id="vipBean" class="com.powernode.spring6.beans.Vip">
            <property name="email">
                <null/>
            property>
        bean>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注入的值中含有特殊符号

    XML中有5个特殊字符<、>、'、"、& : 它们在XML中会被当做XML语法的一部分进行解析所以不能直接出现在value的属性值当中

    第一种使用转义字符代替特殊符号: <(>)、>(<)、'(')、"(")、&(&)

     public class Math {
        private String result;
        public void setResult(String result) {
            this.result = result;
        }
        @Override
        public String toString() {
            return "Math{" +
                    "result='" + result + '\'' +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    
    <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="mathBean" class="com.powernode.spring6.beans.Math">
            
            <property name="result" value="2 < 3"/>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    第二种在value标签中将含有特殊符号的字符串当作普通字符串处理: 中的数据不会被XML文件解析器解析

    • 使用时不能使用value属性,只能使用value标签-
    
    <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="mathBean" class="com.powernode.spring6.beans.Math">
            <property name="result">
                
                <value>value>
            property>
        bean>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    测试程序

    @Test
    public void testSpecial(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-special.xml");
        Math mathBean = applicationContext.getBean("mathBean", Math.class);
        //Math{result='2 < 3'}
        System.out.println(mathBean);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    SpEL(Spring Expression Language)

    Spring表达式语言

    使用property标签的value属性给简单类型的属性赋值时可以在#{}的括号中使用SpEL表达式

    表达式内容
    #{12345.67*12}含运算符的字面量, 支持使用任何运算符
    #{book01.bookName}引用其他的bean的某个属性值
    #{car}引用其他的bean的id
    #{T(全类名).静态方法名(实参)}调用静态方法
    #{ 对象bean.非静态方法名(实参) }调用非静态方法
    <bean id="book01" class="com.atguigu.bean.Book">
        <property name="bookName" value="book1">property>
    bean>
    <bean id="person" class="com.atguigu.bean.Person">
        
        <property name="salary" value="#{12345.67*12}">property>
        
        <property name="lastName" value="#{book01.bookName}">property>
        
        <property name="car" value="#{car}">property>
        
        <property name="email" value="#{T(java.util.UUID).randomUUID().toString().substring(0,5)}">property>
        
        <property name="gender" value="#{book01.getBookName()}">property>
    bean>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
  • 相关阅读:
    智慧电网解决方案-最新全套文件
    基于邻接矩阵的克鲁斯卡尔算法和普利姆算法
    深入理解JVM(十八)GC日志分析
    Open3D Ransac拟合空间直线(python详细过程版)
    HTTP1.x协议详解和HTTP2.0
    XILINX FIR IP 详解、Verilog 源码、Vivado 工程
    git--远程仓库的命令--使用/实例
    智慧加油站AI智能视频分析系统
    Linux ARM平台开发系列讲解(PCIE) 2.13.2 PCI设备的访问方法(非桥设备)
    新160个CrackMe分析-第2组:11-20(下)
  • 原文地址:https://blog.csdn.net/qq_57005976/article/details/132948861