Spring学习之获取其他Bean的属性值(PropertyPathFactoryBean)

本文通过一个具体的例子展示了如何使用Spring框架进行依赖注入。通过定义Person和Son两个类,并在spring-conf.xml配置文件中设置属性,实现了对象之间的依赖关系。

Person.java:

package com.kinsey.woo.dto;

public class Person {
	private Son son;
	
	private String Id;

	public Son getSon() {
		return son;
	}

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

	public String getId() {
		return Id;
	}

	public void setId(String id) {
		Id = id;
	}

	public Person() {
		super();
	}
	
	@Override
	public String toString() {
		return "Id: " + getId() + " " + getSon();
	}
}

Son.java

package com.kinsey.woo.dto;

public class Son {
	
	private int age;
	
	private String name;

	public int getAge() {
		return age;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Son() {
		super();
	}
	
	@Override
	public String toString() {
		return  "name: " + getName() + " age : "  + getAge();
	}

}

RunMain.java

package com.kinsey.woo.main;

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

import com.kinsey.woo.dto.Person;
import com.kinsey.woo.dto.Son;

public class RunMain {
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-conf.xml");
		Person person = ctx.getBean("person", Person.class);
		System.out.println("Person:" + person);
		Son son = ctx.getBean("son1", Son.class);
		System.out.println("\n"+"son:" + son);
		
		String name = ctx.getBean("theName1", String.class);
		System.out.println("\n" + "Name: " + name);
	}
}

spring-conf.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


	<bean id="person" class="com.kinsey.woo.dto.Person">
		<property name="id" value="No.1001"></property>
		<property name="son">
			<bean class="com.kinsey.woo.dto.Son">
				<property name="age" value="22"></property>
				<property name="name" value="David"></property>
			</bean>
		</property>
	</bean>
	
	
	
	<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
		<property name="targetBeanName" value="person" />
		<!-- 调用person.getSon()方法给son1 -->
		<property name="propertyPath" value="son" />
	</bean>
	
	
	<bean id="theName1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
		<property name="targetBeanName" value="son1"/>
		<!-- 调用son1.getName()方法给theName1 -->
		<property name="propertyPath" value="name"/>
	</bean>

</beans>

结果:





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值