(7)spring依赖注入引用其它Bean

一、引入外部bean

新建一个实现类

package shuai.spring.study;

public class HelloImpl5 implements HelloApi {
	private HelloApi helloApi;

	public HelloApi getHelloApi() {
		return helloApi;
	}

	public void setHelloApi(HelloApi helloApi) {
		this.helloApi = helloApi;
	}

	@Override
	public void sayHello() {
		System.out.println("==========装饰一下===========");
		helloApi.sayHello();
		System.out.println("==========装饰一下===========");
	}
}

配置文件

<bean id="bean1" class="shuai.spring.study.HelloImpl"/>
    <bean id="hello" class="shuai.spring.study.HelloImpl5">
    	<property name="helloApi" ref="bean1"></property>
    </bean>

测试结果:

==========装饰一下===========
Hello World!
==========装饰一下===========

跟普通注入差不多,就是value属性变成了ref。

也可以写ref标签

<property name="helloApi"><ref bean="bean1"/></property>

还可以高级配置方式

<ref local="bean1"/>,具有验证功能,不过新spring 不能用了。

The local attribute on the ref element is no longer supported in the 4.0 beans xsd since it does not provide value over a regular bean reference anymore. Simply change your existing ref local references to ref bean when upgrading to the 4.0 schema.
官方建议使用bean在Spring4.0以上的版本。

<ref parent="bean1"/>,引用父容器中的Bean。

添加一个配置文件HelloWorld2.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="bean1" class="shuai.spring.study.HelloImpl3">
			<property name="message" value="父配置文件的bean"/>
		</bean>  

</beans>
修改测试类

package shuai.spring.test;

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

import shuai.spring.study.HelloApi;

public class HelloTest {

	@Test
	public void testHelloWorld() {
		ApplicationContext parentContext = new ClassPathXmlApplicationContext("HelloWorld2.xml");
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "HelloWorld.xml" },
				parentContext);
		HelloApi helloApi = context.getBean("hello", HelloApi.class);
		helloApi.sayHello();

	}

}

修改HelloImpl3.java

package shuai.spring.study;

public class HelloImpl3 implements HelloApi {
	private String message;

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public void sayHello() {
		System.out.println(message);
	}

修改原来的配置文件HelloWorld.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="bean1" class="shuai.spring.study.HelloImpl3">
		<property name="message" value="同一配置文件的bean"/>
	</bean>
    <bean id="hello" class="shuai.spring.study.HelloImpl5">
    	<property name="helloApi"><ref parent="bean1"/></property>
    </bean>  

</beans>

测试结果:

==========装饰一下===========
父配置文件的bean
==========装饰一下===========

这时候引用的是父配置文件里的bean1

二、定义内部bean

    <bean id="hello" class="shuai.spring.study.HelloImpl5">
    	<property name="helloApi">
    		<bean id="hello" class="shuai.spring.study.HelloImpl3">
		    	<property name="message" value="Hello World !"/>
		    </bean>
    	</property>
    </bean>
测试结果:

==========装饰一下===========
Hello World !
==========装饰一下===========

我觉得就是某个bean的属性是我们自定义的class,写成bean的形式,实例化了一个对象。

【摘抄】内部Bean就是在<property>或<constructor-arg>内通过<bean>标签定义的Bean,该Bean不管是否指定id或name,该Bean都会有唯一的匿名标识符,而且不能指定别名,该内部Bean对其他外部Bean不可见

三、处理null,使用<null/>,注入null值,不是字符串null

修改HelloImpl3.java的方法

@Override
	public void sayHello() {
		System.out.println("message的值为:" + message + ",与字符串null比较结果为:" + "null".equals(message));
	}

配置文件

<bean id="hello" class="shuai.spring.study.HelloImpl3">
	    	<property name="message">
	    		<value>null</value>
	    	</property>
	    </bean>

输出结果:message的值为:null,与字符串null比较结果为:true

配置文件

<bean id="hello" class="shuai.spring.study.HelloImpl3">
	    	<property name="message">
	    		<null/>
	    	</property>
	    </bean>

输出结果:message的值为:null,与字符串null比较结果为:false

四、对象图导航


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值