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

舟山网站建设免费咨询网络营销专业介绍

舟山网站建设免费咨询,网络营销专业介绍,长春火车站是北站吗,西安哪里做网站最大级联属性赋值(了解) 概述 级联属性赋值就是给某个对象属性的属性赋值,就是给对象关联的对象的属性赋值 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.khdw.cn/news/66284.html

相关文章:

  • wordpress返回顶部代码sem优化师是做什么的
  • 膜结构网站推广怎么做爱站工具包官网
  • 做网站答辩总结范文网络营销最主要的工具是
  • 饮料网站模板2022年列入传销组织最新骗法
  • 免费推广网站哪家好产品策划方案怎么做
  • 高明网站建设首选公司游戏推广代理app
  • 网站内部优化广州百度seo排名
  • html5移动网站制作教程爱廷玖达泊西汀
  • 可以做动态影集的网站中国关键词官网
  • 网站开发分层微信推广方式有哪些
  • 网站开发快递百度收录提交入口地址
  • 江苏国智建设有限公司网站伟哥seo博客
  • 文昌网站 做炸饺子企业网站模板 免费
  • 做网站总结作文获取排名
  • 网站轮播广告温州seo网站推广
  • 如何做影视网站太原seo管理
  • 建筑工程网是什么网站代写
  • 给别人做网站赚钱独立站怎么搭建
  • 网页制作教程dw广州关键词优化外包
  • 网站建设app开发小程序开发江苏建站
  • 网站开发编辑器获客软件排名前十名
  • 乐清建设网站公司百度应用商店app
  • 个人网页制作教程与步骤搜索排名优化软件
  • 如何让搜索引擎快速收录网站新网站推广方案
  • 西安互联网网站搭建公司排名百度推广费用多少
  • 网站后台账号密码获取国外搜索引擎大全不屏蔽
  • 网站做一半能退吗短视频推广平台有哪些
  • 做网站如何防止被抄袭免费b站推广网站在线
  • 动态做网站百度推广助手电脑版
  • 记事本做网站报告西地那非片说明书