以下回复参考:皆我百晓生、券券喵儿等免费微信小程序作答:
根据给定的代码,可以看出在Spring配置文件中使用了p命名空间来注入属性。但是,Attribute p:sid is not allowed here的错误提示表示p命名空间的使用不正确。
要解决这个问题,可以尝试以下方法:
- 确保在使用p命名空间之前,已经正确引入了p命名空间的约束。在XML文件的开头添加以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- 确保在使用p命名空间时,属性名和值之间的等号是英文状态下的等号,而不是中文状态下的等号。将代码中的中文等号替换为英文等号:
<bean id="studentSix" class="com.atguigu.spring6.iocxml.dimap.Student"
p:sid="1006" p:sname="小明" p:teacherMap-ref="teacherMap">
</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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="studentSix" class="com.atguigu.spring6.iocxml.dimap.Student"
p:sid="1006" p:sname="小明" p:teacherMap-ref="teacherMap">
</bean>
<!--
1.创建两个类
2.注入普通类型属性
3.在学生bean注入map集合类型属性
-->
<bean id="teacherone" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<property name="teacherId" value="100"></property>
<property name="teacherName" value="西门庆"></property>
</bean>
<bean id="teachertwo" class="com.atguigu.spring6.iocxml.dimap.Teacher">
<property name="teacherId" value="200"></property>
<property name="teacherName" value="东方不败"></property>
</bean>
<bean id="student" class="com.atguigu.spring6.iocxml.dimap.Student">
<property name="sid" value="10000"></property>
<property name="sname" value="李四"></property>
<property name="teacherMap">
<map>
<entry>
<key>
<value>110</value>
</key>
<ref bean="teacherone"></ref>
</entry>
<entry>
<key>
<value>210</value>
</key>
<ref bean="teachertwo"></ref>
</entry>
</map>
</property>
</bean>
</beans>