- 可以在配置文件中通过将class指定为PropertyPathFactory,然后就可以通过指定targetClass配合targetField来将别人的成员变量拿过来用啦~~
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--将class指定为FieldRetrievingFactoryBean,然后再指定 targetClass和targetField,就ok--> <bean id="theAge1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean "> <property name="targetClass" value="java.sql.Connection"/> <property name="targetField" value="TRANSACTION_READ_UNCOMMITTED"/> </bean> <!--这是简写,如果这里是实例变量那么staticField就要改啦--> <bean id="theAge2" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean "> <property name="staticField" value="java.sql.Connection.TRANSACTION_NONE"/> </bean> <!--一种比较高级的写法--> <bean id="test" class="InstancePackage.Test"> <property name="show"> <!--<bean id="java.sql.Connection.TRANSACTION_NONE"--> <!--class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>--> <util:constant static-field="java.sql.Connection.TRANSACTION_NONE"/> </property> </bean> </beans>
package InstancePackage; public class Test { private int show; public int getShow() { return show; } public void setShow(int show) { this.show = show; } }
package TestPackage; import InstancePackage.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String []args){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); System.out.println( applicationContext.getBean("theAge1",Integer.class)); System.out.println( applicationContext.getBean("theAge2",Integer.class)); System.out.println( applicationContext.getBean("test", Test.class).getShow()); } }
这是我看李刚编著的《轻量级javaEE企业应用实战(第五版)-Struts2+Spring5+Hibernate5/JAP2》后总结出来的。
Spring(21) 获取其他类成员变量的值
最新推荐文章于 2022-11-09 19:56:51 发布