Spring IOC 訪問屬性及方法

  • 調用getter方法:

org.springframework.beans.factory.config.PropertyPathFactoryBean

getter方法要求:實例方法;有返回值;不能有參數

setter方法要求:實例方法;無返回值;只能一個參數

<bean id=""
class="org.springframework.beans.factory.config.PropertyPathFactoryBean"
p:targetObject-ref="對象" p:propertyPath="getter方法"></bean>
System.out.println(context.getBean("XXX"));

簡化:

<!-- 引入必要的schema -->
xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd

<util:property-path id="" path="對象名.屬性值"/>
  • 訪問類或對象的Field值:org.springframework.beans.factory.config.FieldRetrievingFactoryBean

靜態Filed:<util:constant id="" static-field=""></util:constant>

<util:constant id="pi" static-field="java.lang.Math.PI" />

實例Field:(一般爲private修飾),Spring只能訪問public修飾的Field

  • 調用普通方法:org.springframework.beans.factory.config.MethodInvokingFactoryBean

靜態方法(類方法):需要指定3個信息

哪個類:setTargetClass

哪個方法:setTargetMethod

調用參數:setArguments

實例方法:需要指定3個信息:

哪個對象:setTargetObject

哪個方法:setTargetMethod

調用參數:setArguments

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章