当前位置: 首页 > news >正文

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

级联属性赋值(了解)

概述

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

Clazz班级类

public class Clazz {private String name;public Clazz() {}public Clazz(String name) {this.name = name;}//set和get方法以及toString方法
}

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方法}

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

<?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="clazzBean" class="com.powernode.spring6.bean.Clazz"><property name="name" value="高三一班"/></bean>
</beans>

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

  • Student类的clazz属性必须提供getter方法,只有这样Spring才能通过调用getClazz()方法拿到对应的Clazz对象然后给它的name属性赋值
  • 标签配置的顺序不能颠倒: 在Student对象中只有先给clazz属性赋值后,我们才能拿到对应的Clazz对象然后给它的name属性赋值
<?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"><!--使用级联属性通过给Student对象关联的对象的属性赋值--><bean id="student" class="com.powernode.spring6.beans.Student"><!--简单类型使用value属性--><property name="name" value="张三"/><!--先给Student的clazz属性赋值--><property name="clazz" ref="clazzBean"/><!--然后调用getClazz()方法拿到clazz属性对应的Clazz对象,然后给它的name属性赋值--><property name="clazz.name" value="高三一班"/></bean><bean id="clazzBean" class="com.powernode.spring6.beans.Clazz"/>
</beans>

测试程序

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

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

注入空字符串

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

public class Vip {private String email;public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "Vip{" +"email='" + email + '\'' +'}';}
}
<?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="vipBean" class="com.powernode.spring6.beans.Vip"><!--空串的第一种方式--><property name="email" value=""/><!--空串的第二种方式--><property name="email"><value/></property></bean></beans>

注入null

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

<?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"><!--第一种方式:不给属性赋值(默认就是null)--><bean id="vipBean" class="com.powernode.spring6.beans.Vip" /><!--第二种方式:使用<null/>标签--><bean id="vipBean" class="com.powernode.spring6.beans.Vip"><property name="email"><null/></property></bean></beans>

注入的值中含有特殊符号

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

第一种使用转义字符代替特殊符号: <(&gt;)、>(&lt;)、'(&apos;)、"(&quot;)、&(&amp;)

 public class Math {private String result;public void setResult(String result) {this.result = result;}@Overridepublic String toString() {return "Math{" +"result='" + result + '\'' +'}';}
}
<?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="mathBean" class="com.powernode.spring6.beans.Math"><!--注入2>3--><property name="result" value="2 &lt; 3"/></bean>
</beans>

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

  • 使用<![CDATA[含特殊字符的串]]>时不能使用value属性,只能使用value标签-
<?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="mathBean" class="com.powernode.spring6.beans.Math"><property name="result"><!--使用CDATA时不能使用value属性只能使用value标签--><value><![CDATA[2 < 3]]></value></property></bean>
</beans>

测试程序

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

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><!--引用其他bean的某个属性值--><property name="lastName" value="#{book01.bookName}"></property><!--引用其他bean,也可以使用ref属性应用--><property name="car" value="#{car}"></property><!--调用静态方法:UUID.randomUUID().toString()--><property name="email" value="#{T(java.util.UUID).randomUUID().toString().substring(0,5)}"></property><!--调用非静态方法:对象.方法名--><property name="gender" value="#{book01.getBookName()}"></property>
</bean>
http://www.lryc.cn/news/168734.html

相关文章:

  • 用AI在小红书做早教启蒙,2个月涨粉11.7万,获赞10万的新流量玩法
  • Recommender Systems in the Era of Large Language Models (LLMs)
  • 红心向阳 百鸟朝凤
  • C语言自己实现一个memcpy函数
  • C#教师考勤管理系统asp.net+sqlserver
  • Nginx代理配置详解
  • DAG 的深度优先搜索标记
  • 网络存储解决方案:选择与配置
  • java中BigDecimal除法运算指定小数点保留位数和取舍规则
  • 车联网远程监控管理提升车辆调度效率,实现高效运营
  • 数据治理-数据建模和设计
  • 博客系统(升级(Spring))(四)(完)基本功能(阅读,修改,添加,删除文章)(附带项目)
  • 常用的辅助类(必会)
  • Java常用类之 String、StringBuffer、StringBuilder
  • linux在所有文件中查找某一个字符串
  • WebSocket vs SSE: 实时数据推送到前端的选择与实现(详细)
  • Redis从入门到精通(二:数据类型)
  • 基于SSM的珠宝首饰交易平台
  • 4款视频号数据分析平台!
  • 【系统架构】什么是集群?为什么要使用集群架构?
  • Java手写拓扑排序和拓扑排序应用拓展案例
  • 练习:使用servlet显示试卷页面
  • 视频监控系统/视频云存储EasyCVR接入国标GB28181设备无法播放设备录像,是什么原因?
  • 四叶草clover配置工具:Clover Configurator for Mac
  • 计算机网络第四章——网络层(中)
  • 时序分解 | MATLAB实现基于小波分解信号分解分量可视化
  • VMware虚拟化环境搭建
  • Jenkins :添加node权限获取凭据、执行命令
  • 如何实现不同MongoDB实例间的数据复制?
  • 微服务保护-隔离