spring 的配置 bean>>property>>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-3.2.xsd ">
  <bean id="English_level" class="com.myspring.second.EnglishLevel">
      <!-- set方法注入 -->
    <property name="course">
      <value>4級</value>
    </property>
  </bean>
  <bean id="Math_level" class="com.myspring.second.MathCourse">
    <!-- 構造函數注入 -->
    <constructor-arg><value>1001</value></constructor-arg>
    <constructor-arg><value>離散數學</value></constructor-arg>
  </bean>
  <bean id="student" class="com.myspring.second.Student">
    <property name="eng">
      <ref bean="English_level"/>
    </property>
    <property name="mat">
      <ref bean="Math_level"/>
    </property>
  </bean>
</beans>
例如上面這個文件:
  • 配置了3個bean
  • property 的 name 屬性值,在實體 bean 中必須有對應的 set 方法,否則報錯 ,報錯如下:
Bean property '某某屬性' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

例如:
<bean id="student" class="com.myspring.second.Student">
    <property name="eng">
      <ref bean="English_level"/>
    </property>
    <property name="mat">
      <ref bean="Math_level"/>
    </property>
  </bean>
對於這個bean,在 Student 類中必須存在:
setEng(String string){}
setMat(String string){}
這兩個方法;
set 方法寫法就不詳解了,略~

渣渣就得在 bug中掙扎




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM