使用Spring(五) 引用其它的bean(协作者)

1.applicationContext.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属性及构造器参数详解 -->
	<bean id="myDataSource" destroy-method="close" class="com.yw.test05.DataSource">
		<!-- 直接量(基本类型、Strings类型等。) -->
		<!-- results in a setDriverClassName(String) call -->
		<property name="driverClassName">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<property name="url">
			<value>jdbc:mysql://localhost:3306/mydb</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
	</bean>

	<bean id="theTargetBean" class="com.yw.test05.Target">
		<property name="username">
			<value>root</value>
		</property>
	</bean>

	<bean id="theClientBean" class="com.yw.test05.Client">

		<!-- idref元素 -->
		<!-- idref标记允许容器在部署时 验证所被引用的bean是否存在 -->
		<property name="targetName">
			<!-- idref元素用来将容器内其它bean的id传给<constructor-arg/> 或 <property/>元素,同时提供错误验证功能 -->
			<idref bean="theTargetBean" />
		</property>

		<!-- 引用其它的bean(协作者) -->
		<property name="target">
			<!-- 使用<ref/>标记指定bean属性的目标bean,通过该标签可以引用同一容器或父容器内的任何bean(无论是否在同一XML文件中)。XML 
				'bean'元素的值既可以是指定bean的id值也可以是其name值。 -->

			<ref bean="theTargetBean" />
		</property>
	</bean>



</beans>

2.

package com.yw.test05;

public class Client
{
	private String targetName;
	
	private Target target;
	

	public void setTarget(Target target)
	{
		System.out.println("==Client==public void setTarget(Target target)==start==");
		this.target = target;
		System.out.println("target="+target);
		System.out.println("==Client==public void setTarget(Target target)==end===");
	}


	public void setTargetName(String target)
	{
		System.out.println("==Client==public void setTargetName(String target)==start==");
		this.targetName = target;
		System.out.println("target="+target);
		System.out.println("==Client==public void setTargetName(String target)==end===");
	}
	
	
}

package com.yw.test05;

public class Target
{
	private String username;

	public void setUsername(String username)
	{
		this.username = username;
	}
	
}

package com.yw.test05;

public class DataSource
{
	private String driverClassName;
	private String url;
	private String username;
	public void setDriverClassName(String driverClassName)
	{
		this.driverClassName = driverClassName;
	}
	public void setUrl(String url)
	{
		this.url = url;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	
	public void close()
	{
		
	}
	public String getDriverClassName()
	{
		return driverClassName;
	}
	public String getUrl()
	{
		return url;
	}
	public String getUsername()
	{
		return username;
	}
	@Override
	public String toString()
	{
		return "DataSource [driverClassName=" + driverClassName + ", url=" + url + ", username=" + username + "]";
	}
	
}

package com.yw.test05;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Test01
{
	public static void main(String[] args)
	{
		
		//实例化容器方法三classpath:
//		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "com/yw/test05/applicationContext.xml"});
		ApplicationContext context = new FileSystemXmlApplicationContext(new String[] { "classpath:com/yw/test05/applicationContext.xml"});
		// of course, an ApplicationContext is just a BeanFactory
		BeanFactory factory = (BeanFactory) context;
		
		Object obj=factory.getBean("myDataSource");
		System.out.println("obj="+obj);
		
	}
}

3.运行


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值