深入理解依赖关系配置

前面介绍的依赖关系,要么是JavaBean式的值,要么直接依赖于其他Bean。在实际的应用中,某个实例的属性可能是某个方法的返回值,或者类的Field值,或者属性值,Spring同样可以支持这种非常规的注入方式。Spring甚至支持将Bean实例的属性值、方法返回值、Field值,直接定义成容器中的一个变量。下面将深入介绍这些特殊的注入形式。


1、注入其他Bean的属性值

PropertyPathFactoryBean用来获得目标Bean的属性值(实际上就是它的getter方法的返回值),获得的值可注入给其他Bean,也可直接定义成新的Bean。

package UnderstandingDependencyConfiguration;

public class Person {

	private String age;
	private Son son;

	public void setAge(String age) {
		this.age = age;
	}

	public void setSon(Son son) {
		this.son = son;
	}

	public Son getSon() {
		return son;
	}
	
	public String getAge() {
		return age;
	}
	
}

package UnderstandingDependencyConfiguration;

public class Son {

	private String age;
	
	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>	
	<bean id="person" class="UnderstandingDependencyConfiguration.Person" >
		<property name="age" value="30"/>
		<property name="son">
			<!-- 使用嵌套Bean定义属性值 -->
			<bean class="UnderstandingDependencyConfiguration.Son">
				<property name="age" value="11"/>
			</bean>
		</property>
	</bean>
	<!-- 如下定义son2的Bean,该Bean的age属性不是直接注入,而是依赖于其他Bean的属性值 -->
	<bean id="son2" class="UnderstandingDependencyConfiguration.Son">
		<property name="age">
			<bean id="person.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
		</property>
	</bean>
	<!-- 将指定Bean实例的属性值定义成指定Bean实例 -->
	<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<!-- targetBeanName或targetObject 用于指定目标Bean,确定获取哪个Bean的属性 -->
		<property name="targetBeanName" value="person"/>
<!-- propertyPath 用于指定属性,确定获取目标Bean的哪个属性值 -->
		<property name="propertyPath" value="son"/>
	</bean>
	<!-- 将基本数据类型的属性值定义成Bean实例 -->
	<bean id="theAge1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
		<property name="targetBeanName" value="person"/>
		<property name="propertyPath" value="son.age"/>
	</bean>
	<bean id="theAge2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
		<property name="targetObject">
			<bean class="UnderstandingDependencyConfiguration.Person">
				<property name="age" value="20"/>
			</bean>
		</property>
		<property name="propertyPath" value="age"/>
	</bean>
	
</beans>

package UnderstandingDependencyConfiguration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext("bean.xml");
		
		//注入其他Bean的属性值

		System.out.println("系统获取son2:"+((Son)ctx.getBean("son2")).getAge());
		
		System.out.println("系统获取son1:"+((Son)ctx.getBean("son1")).getAge());
		
		System.out.println("系统获取theAge1:"+ctx.getBean("theAge1"));
		System.out.println("系统获取theAge2:"+ctx.getBean("theAge2"));
	}
}



2、注入其他Bean的Field值

FieldRetrievingFactoryBean获得目标Bean的Field值之后,得到的值可注入其他Bean,也可直接定义成新的Bean。


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>	
	<bean id="fieldSon" class="UnderstandingDependencyConfiguration.Son">
		<property name="age">
			<bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE"
				class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>
		</property>
	</bean>
	<!-- 将Field值定义成Bean实例 -->
	<bean id="fieldAge1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="targetClass" value="java.sql.Connection"/>
		<property name="targetField" value="TRANSACTION_SERIALIZABLE"/>
	</bean>
	<!-- 静态Field 更简洁的写法 -->
	<bean id="fieldAge2" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
		<property name="staticField" value="java.sql.Connection.TRANSACTION_SERIALIZABLE"/>
	</bean>
	
</beans>

package UnderstandingDependencyConfiguration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext("bean.xml");
		
		System.out.println("系统获取fieldSon的age属性值:"+((Son)ctx.getBean("fieldSon")).getAge());
		System.out.println("系统获取fieldAge1:"+ctx.getBean("fieldAge1"));
		System.out.println("系统获取fieldAge2:"+ctx.getBean("fieldAge2"));
	}
}



3、注入其他Bean的方法返回值

通过MethodInvokingFactoryBean工厂Bean,可将指定方法返回值注入成目标Ban的属性值,MethodInvokingFactoryBean用来获得指定方法的返回值,该方法既可以是静态方法,也可以是实例方法;获得的方法返回值既可被注入到指定Bean实例的指定属性,也可以直接定义成Bean实例。

package UnderstandingDependencyConfiguration;

public class ValueGenerator {

	public int getValue(){
		return 2;
	}
	
	public static int getStaticValue(){
		return 9;
	}
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.springframework.org/schema/beans"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd"
>
	<bean id="valueGenerator" class="UnderstandingDependencyConfiguration.ValueGenerator"/>
	<bean id="valueSon1" class="UnderstandingDependencyConfiguration.Son">
		<property name="age">
			<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
				<property name="targetObject" ref="valueGenerator"/>
				<property name="targetMethod" value="getValue"/>
			</bean>
		</property>
	</bean>
	<bean id="valueSon2" class="UnderstandingDependencyConfiguration.Son">
		<property name="age">
			<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
				<property name="targetClass" value="UnderstandingDependencyConfiguration.ValueGenerator"/>
				<!-- targetMethod确定目标方法,指定调用目标class的哪个方法。
					该方法必须是静态方法 -->
				<property name="targetMethod" value="getStaticValue"/>
			</bean>
		</property>
	</bean>
	<!-- 将静态方法返回值直接定义成Bean -->
	<bean id="sysProps" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
		<property name="targetClass" value="java.lang.System"/>
		<property name="targetMethod" value="getProperties"/>
	</bean>
	<!-- 将实例返回值直接定义成Bean -->
	<bean id="javaVersion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
		<property name="targetObject" ref="sysProps"/>
		<property name="targetMethod" value="getProperty"/>
		<property name="arguments">
			<list>
				<value>java.version</value>
			</list>
		</property>
	</bean>
</beans>

package UnderstandingDependencyConfiguration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ctx =
				new ClassPathXmlApplicationContext("bean.xml");
		
		System.out.println("系统获取valueSon1的age属性值:"+((Son)ctx.getBean("valueSon1")).getAge());
		System.out.println("系统获取valueSon2的age属性值:"+((Son)ctx.getBean("valueSon2")).getAge());
		System.out.println("系统获取sysProps:"+ctx.getBean("sysProps"));
		System.out.println("系统获取java版本:"+ctx.getBean("javaVersion"));
		
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值