舟山网站建设免费咨询网络营销专业介绍
级联属性赋值(了解)
概述
级联属性赋值就是给某个对象属性的属性赋值,就是给对象关联的对象的属性赋值
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的属性值当中
第一种使用转义字符代替特殊符号: <(>)、>(<)、'(')、"(")、&(&)
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 < 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>