<bean id="Hello" class="com.yan.pojo.Hello"> <property name="name" value="我叫Spring"></property> </bean>属性什么意思
时间: 2023-05-26 19:03:22 浏览: 51
这段 XML 配置代码定义了一个名为 "Hello" 的 Bean,其类为 "com.yan.pojo.Hello",并设置了一个名为 "name" 的属性,属性值为 "我叫Spring"。当 Spring 容器加载这个配置文件并创建该 Bean 实例时,Spring 将会自动调用 set 方法将属性值设置到 Bean 实例中。
相关问题
<bean id="hello" class="com.kuang.pojo.Hello"> <property name="name" value="Spring"/> </bean> 请分析作用
这段代码是一个 Spring 的配置文件中的一个 bean 定义。通过这段配置,我们创建了一个名为 "hello" 的 bean,它的类是 "com.kuang.pojo.Hello"。这个 bean 的作用是创建一个 Hello 对象,并设置其 name 属性为 "Spring"。在 Spring 容器启动时,会根据这个配置来创建该 bean,并将其放入容器中供其他组件使用。
<?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 class="com.itheima.pojo.Student" id="student"> <property name="name" value="张三"/> <property name="age" value="20"/> <property name="cid" value="1"/> </bean> <bean class="com.itheima.dao.impl.StudentImpl" id="studentimpl"/> <bean class="com.itheima.service.impl.StudentService" id="studentService"> <property name="userDao" ref="studentimpl"/> </bean> <!--任务5:注入班级bean【10分】--> </beans>
在这段Spring配置文件中,没有定义班级的bean,因此需要在任务5中注入班级bean。可以这样定义班级的bean:
<bean class="com.itheima.pojo.Class" id="class">
<property name="name" value="一年级一班"/>
<property name="teacher" value="李老师"/>
</bean>
然后,在学生bean中添加一个属性class,引用班级bean:
<bean class="com.itheima.pojo.Student" id="student">
<property name="name" value="张三"/>
<property name="age" value="20"/>
<property name="cid" value="1"/>
<property name="class" ref="class"/>
</bean>
最后,在学生服务bean中添加一个属性classService,引用班级服务bean:
<bean class="com.itheima.service.impl.StudentService" id="studentService">
<property name="userDao" ref="studentimpl"/>
<property name="classService" ref="classService"/>
</bean>
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)