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

北京pk10做号网站专业网站设计是什么

北京pk10做号网站,专业网站设计是什么,免费空间域名注册免备案,申请163 com免费邮箱级联属性赋值(了解) 概述 级联属性赋值就是给某个对象属性的属性赋值,就是给对象关联的对象的属性赋值 Clazz班级类 public class Clazz {private String name;public Clazz() {}public Clazz(String name) {this.name name;}//set和get方法以及toString方法 }Student有cl…

级联属性赋值(了解)

概述

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

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.yayakq.cn/news/435811/

相关文章:

  • 深圳做网站制作网络推广公司怎么接单
  • 国外贸易网站移动商城网站建设
  • 皮肤自做头像的网站江西住房与城乡建设厅网站
  • 青州做网站的公司做西式快餐店网站
  • 如何建自己的个人网站济南防疫最新动态
  • jquery动画特效网站科技风格设计网站
  • 网站建设需要哪些成本域名注册服务网站
  • 网站主要功能建一个自己的网站有什么用
  • 网站建设 软件有哪些内容网站后台管理系统制作软件
  • 建站多少钱一个云畅网站建设
  • 北京做企业网站的公司wordpress站点推荐
  • 上海住房与城乡建设部网站阿里巴巴网页版登录入口
  • 网站开发课程培训企业微信开放平台
  • 四网合一网站建设网站安全加固
  • 北京朝阳网站设计做网站怎么赚钱
  • 个人简历免费制作网站顶尖网站建设公司
  • 织梦网站定制wordpress修改评论
  • 翠竹营销网站设计国外网站空间 月付
  • 站长论坛 激活网站展厅设计要考虑哪些方面
  • 网站推广策划案哪里有济南 网站优化
  • 专业平台建设网站关了吗淘宝电商运营
  • 用dw代码做美食网站宁波妇科医生推荐
  • wordpress会员注册seo课程培训中心
  • 网站后台登陆网址是多少一个软件开发流程
  • 静态网站做等级保护内蒙古建设住房与城乡厅官方网站
  • 网站建设好的公司哪家好自己在线制作logo免费u钙网
  • 网站开发培训光山天河网站建设哪家好
  • 东莞长安网站优化公司培训课程有哪些
  • 河口区建设工程招标网站推广优化
  • 用手机做自己的网站wordpress微论坛主题