Spring中的Bean配置 (7)——注入参数详解:null值和级联属性

在这里插入图片描述
Spring中的Bean配置 (1)——内容提要
Spring中的Bean配置 (2)—— IOC和DI
Spring中的Bean配置 (3)——在Spring的IOC容器里配置Bean(通过全类名(反射))——基于XML文件的方式
Spring中的Bean配置 (4)——依赖注入方式
Spring中的Bean配置 (5)——字面值
Spring中的Bean配置 (6)——引用其他Bean
Spring中的Bean配置 (7)——注入参数详解:null值和级联属性
Spring中的Bean配置 (8)—— 集合属性
Spring中的Bean配置 (9)—— XML 配置里的 Bean自动装配
Spring中的Bean配置 (10)—— 继承 Bean 配置和依赖 Bean 配置
Spring中的Bean配置 (11)——Bean的作用域
Spring中的Bean配置 (12)——使用外部属性文件
Spring中的Bean配置 (13)—— Spring表达式语言:SpEl
Spring中的Bean配置 (14)——IOC容器中Bean的生命周期
Spring中的Bean配置 (15)——在Spring的IOC容器里配置Bean(通过工厂方法创建Bean)——基于XML文件的方式
Spring中的Bean配置 (16)——在Spring的IOC容器里配置Bean(通过FactoryBean)——基于XML文件的方式

Spring中的Bean配置 (17)——在Spring的IOC容器里配置Bean——基于注解的方式来配置Bean

  • 级联属性:顾名思义又是属性和属性之间的事
  • 可以使用专用的 null 元素标签为 Bean 的字符串或其它对象类型的属性注入 null 值

格式如下

在这里插入图片描述

  • 和 Struts、Hiberante 等框架一样,Spring 支持级联属性的配置

例:

<!-- 测试赋值null和级联属性 -->
<bean id="bean7" class="com.atguigu.spring.beans.car.Car">
<property name="carName" value="baoma"></property>
<property name="price" value="10000000"></property>
<property name="maxSpeed" value="250"></property>
<!--级联属性演示-->
<property name="person" >
<bean class="com.atguigu.spring.beans.Person">

<constructor-arg> <null/></constructor-arg>
<constructor-arg> <null/></constructor-arg>
<constructor-arg> <null/></constructor-arg>

</bean>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 假设我们有一个班级类Class,其包含了一个学生列表List<Student>,我们可以使用级联属性装配的方式来配置这个班级类的Bean。 首先,我们需要在Spring配置文件声明Class类的Bean: ``` <bean id="class" class="com.example.Class"> <property name="students"> <!-- 级联属性装配,引用名为studentList的Bean --> </property> </bean> ``` 然后,我们需要配置一个名为studentList的Bean,它是一个List<Student>类型的Bean。我们可以使用<list>元素来配置它: ``` <bean id="studentList" class="java.util.ArrayList"> <constructor-arg> <list> <!-- 学生1 --> <bean class="com.example.Student"> <property name="name" value="张三"/> <property name="age" value="18"/> </bean> <!-- 学生2 --> <bean class="com.example.Student"> <property name="name" value="李四"/> <property name="age" value="19"/> </bean> <!-- 学生3 --> <bean class="com.example.Student"> <property name="name" value="王五"/> <property name="age" value="20"/> </bean> </list> </constructor-arg> </bean> ``` 注意,在studentList的配置,我们使用了<list>元素来声明一个列表,然后在列表配置了三个学生Bean。 最后,我们可以使用Class类的Bean来访问学生列表: ``` Class clazz = (Class) applicationContext.getBean("class"); List<Student> students = clazz.getStudents(); ``` ### 回答2: 在使用级联属性装配班级类的Bean时,需先定义班级和学生的类,然后在配置文件进行相关的Bean配置。 首先,创建两个Java类:班级类(Class)和学生类(Student)。 班级类(Class)的代码如下: ```java public class Class { private String name; private List<Student> students; public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Student> getStudents() { return students; } public void setStudents(List<Student> students) { this.students = students; } } ``` 学生类(Student)的代码如下: ```java public class Student { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } ``` 接下来,在Spring配置文件进行相关的Bean配置: ```xml <bean id="student1" class="com.example.Student"> <property name="name" value="张三" /> <property name="age" value="18" /> </bean> <bean id="student2" class="com.example.Student"> <property name="name" value="李四" /> <property name="age" value="19" /> </bean> <bean id="class" class="com.example.Class"> <property name="name" value="一班" /> <property name="students"> <list> <ref bean="student1" /> <ref bean="student2" /> </list> </property> </bean> ``` 以上代码,首先定义了两个学生的Bean,分别为student1和student2,接着定义了班级的Bean,其使用了级联属性来装配学生的Bean。在`<property>`标签,使用`<list>`标签来定义学生列表,然后通过`<ref>`标签引用学生的Bean。 通过上述配置,我们实现了使用级联属性装配班级类的Bean。班级类(Class)的Bean包含了一个学生列表,通过配置文件级联属性,将学生的Bean注入到班级的Bean。 ### 回答3: 要使用级联属性装配班级类的Bean,可以使用Spring配置文件来进行相关配置。以下是示例代码: 1. 创建Student类和Class类并在Class类包含Student类的属性和相关方法。 ```java public class Student { private String name; // 其他属性和方法省略 } public class Class { private String className; private List<Student> students = new ArrayList<>(); public void addStudent(Student student) { students.add(student); } // 其他属性和方法省略 } ``` 2. 在Spring配置文件进行Bean的定义和属性装配。 ```xml <bean id="student1" class="com.example.Student"> <property name="name" value="张三" /> <!-- 其他属性装配 --> </bean> <bean id="student2" class="com.example.Student"> <property name="name" value="李四" /> <!-- 其他属性装配 --> </bean> <bean id="class1" class="com.example.Class"> <property name="className" value="一年级" /> <property name="students"> <list> <ref bean="student1" /> <ref bean="student2" /> </list> </property> <!-- 其他属性装配 --> </bean> ``` 以上代码,首先定义了两个Student的Bean(student1和student2),然后定义了一个Class的Bean(class1),并将student1和student2作为students属性进行装配。students属性使用了ref元素来引用之前定义的student1和student2的Bean。这样,在获取class1的实例时,students属性会自动装配上student1和student2两个对象。 通过以上配置,可以实现级联属性装配班级类的Bean,从而将学生与班级进行关联。在实际应用,可以根据需求定义更多的Student和Class对象,以满足具体的业务需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值