`

跟我学Spring3 学习笔记六 注入

阅读更多


跟我学Spring3 学习笔记一

跟我学Spring3 学习笔记二

跟我学Spring3 学习笔记三

跟我学Spring3 学习笔记四

跟我学Spring3 学习笔记五 注入

 

 

引用其它Bean

 

一、构造器注入方式:

(1)通过” <constructor-arg>”标签的ref属性来引用其他Bean

 

(2)通过” <constructor-arg>”标签的子<ref>标签来引用其他Bean,使用bean属性来指定引用的Bean

二、setter注入方式:

(1)通过” <property>”标签的ref属性来引用其他Bean

(2)通过” <property>”标签的子<ref>标签来引用其他Bean,使用bean属性来指定引用的Bean

 

 

public class HelloDiBean implements HelloApi{

	private HelloApi helloApi;
	private HelloApi helloApi2;
	

	public HelloDiBean(HelloApi helloApi){
		this.helloApi = helloApi;
	}
	
	public void sayHello() {
		helloApi.sayHello();
		helloApi2.sayHello();
	}
	

	public HelloApi getHelloApi2() {
		return helloApi2;
	}

	public void setHelloApi2(HelloApi helloApi2) {
		this.helloApi2 = helloApi2;
	}
}

 配置注入引用其他的bean

 

<!-- 引用其他的bean进行注入 -->
	<bean id="helloBean" class="com.dilist.HelloDiBean">
		<constructor-arg index="0" ref="mapBean" />
		<property name="helloApi2">
			<ref bean="properBean" />
		</property>
	</bean>
	
 

其他引用bean 的高级用法:

 

/**
 * Spring还提供了另外两种更高级的配置方式,<ref local=””/>和<ref parent=””/>:
 * (1)<ref local=””/>配置方式:用于引用通过<bean id=”beanName”>方式中通过id属性指定的Bean,
 * 		它能利用XML解析器的验证功能在读取配置文件时来验证引用的Bean是否存在。
 * 		因此如果在当前配置文件中有相互引用的Bean可以采用<ref local>方式从而如果配置错误能在开发调试时就发现错误。
 * (2)<ref parent=””/>配置方式:用于引用父容器中的Bean,不会引用当前容器中的Bean,
 *       当然父容器中的Bean和当前容器的Bean是可以重名的,获取顺序是直接到父容器找。
 */
public class HelloHigh implements HelloApi{
	
	private HelloApi helloApi;
	private HelloApi helloApi2;
	

	public HelloHigh(HelloApi helloApi){
		this.helloApi = helloApi;
	}
	
	public void sayHello() {
		helloApi.sayHello();
		System.out.println("");
		helloApi2.sayHello();
	}
	

	public HelloApi getHelloApi2() {
		return helloApi2;
	}

	public void setHelloApi2(HelloApi helloApi2) {
		this.helloApi2 = helloApi2;
	}

}
 

helloworld.xml:

 

<!-- 注入properties类型 -->
	<bean id="properBean" class="com.dilist.HelloDiProperties">
		<property name="properties">
			<props value-type="int" merge="default"><!-- 虽然指定value-type,但是不起作用 -->
				<prop key="1">1sss</prop>           <!-- Properties 建和值都是String类型 -->
				<prop key="2">2</prop>
			</props>
		</property>
		<property name="properties2">
			<value> <!-- 分隔符可以是 “换行”、“;”、“,” 不建议该方式,优先选择第一种方式 -->
				1=11
				2=22;<!-- 这样的分隔符好像没用 -->
			    3=33,
				4=44
			</value>
		</property>
	</bean>

	<!-- Spring还提供了另外两种更高级的配置方式,<ref local=””/>和<ref parent=””/> -->
	<bean id="helloHigh" class="com.dilist.HelloHigh">
		<constructor-arg index="0"><ref local="properBean" /></constructor-arg>
		<property name="helloApi2"><ref parent="properBean" /></property>	
	</bean>
 

 

helloworldParent.xml:

 

<!-- 注入properties类型 -->
	<bean id="properBean" class="com.dilist.HelloDiProperties">
		<property name="properties">
			<props value-type="int" merge="default"><!-- 虽然指定value-type,但是不起作用 -->
				<prop key="1">2dss</prop>           <!-- Properties 建和值都是String类型 -->
				<prop key="2">3aas</prop>
			</props>
		</property>
		<property name="properties2">
			<value> <!-- 分隔符可以是 “换行”、“;”、“,” 不建议该方式,优先选择第一种方式 -->
				1=111
				2=222;<!-- 这样的分隔符好像没用 -->
			    3=333,
				4=444
			</value>
		</property>
	</bean>
 

调用处 利用加载父容器的方式,注入父容器中的Bean:

 

 

@Test
	public void testDiBeanHigh() {
		// 以classes为根目录算起
		// 读取配置文件实例化一个Ioc容器

		// 初始化父容器
		ApplicationContext parentContext = new ClassPathXmlApplicationContext(
				"helloworldParent.xml");

		// 初始化当前容器
		ApplicationContext context = new ClassPathXmlApplicationContext(
				new String[] { "helloworld.xml" }, parentContext);

		// 构造 + setter注入 引用其他的bean注入
		HelloApi helloApi = context.getBean("helloHigh", HelloApi.class);
		helloApi.sayHello();

	}
1
0
分享到:
评论

相关推荐

    跟我学spring3(1-7)

    【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我学spring3 【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我学spring3 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3 【第六章】 AOP 之 6.5 AspectJ...

    跟我学spring3(8-13)

    【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3 【第十二章】零配置 之 12.3 注解实现Bean定义 ——跟我学spring3 【第十二章】零配置 之 12.4 基于Java类定义Bean配置元数据 ——跟我学spring...

    跟我学Spring,Spring3学习资料

    跟我学Spring,Spring3学习资料,讲的还是很详细的,适合新手看

    跟我学spring3

    跟我学spring3 跟我学spring3 跟我学spring3

    Springcloud学习笔记.md

    Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...

    跟我学spring3pdf,高清

    跟我学spring3的pdf,便于学习,供大家分享,一起学习

    跟我学Spring3

    跟我学Spring3

    跟我学spring3 pdf+源码

    跟我学spring3(1-7).pdf 跟我学spring3(8-13).pdf 跟我学spring3-源码.rar 跟我学spring3-项目源码(pointShop)(基于注解).rar 跟我学spring3-项目源码(pointShop)(基于XML配置文件).rar

    Spring框架学习笔记

    学习spring总结的笔记 希望对初学者有所帮助

    spring指南学习笔记

    spring指南学习笔记

    Spring6学习笔记

    Spring6学习笔记,师承老杜

    springsecurity学习笔记

    三更springsecurity学习笔记

    Spring Cloud 学习笔记.pdf

    Spring Cloud 学习入门笔记,全方面实践,包含 spring cloud alibaba 模块

    springcloud学习笔记.pdf

    springcloud学习笔记.pdf

    跟我学spring3(8-13).pdf

    11.2 实现通用层 11.3 实现积分商城层 12.1 概述 12.2 注解实现Bean依赖注入 12.3 注解实现Bean定义 12.4 基于Java类定义Bean配置元数据 12.5 综合示例-积分商城 13.1 概述 13.2 单元测试 集成测试 ——跟我学spring3

Global site tag (gtag.js) - Google Analytics