spring IOC——注入null类型

applicationontext中null_string注入的值是null,property中标签只有name属性,值是由<null>标签提供

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<bean id="null_string_ref" class="test.test">
		<property name="null_string">
			<null />
		</property>
	</bean>

	<bean id="runit_ref" class="runit.runit">
		<property name="test_ref" ref="null_string_ref"></property>
	</bean>

</beans>

TestRun

package com.run;

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

import com.test.Test;

public class TestRun {

	
	Test test_ref;
	
	public Test getTest_ref() {
		return test_ref;
	}
	
	public void setTest_ref(Test test_ref) {
		this.test_ref = test_ref;
	}
	
	public static void main(String[] args) {
		//取得上下文接口
		ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContext.xml");
		//从IOC容器中取出Bean
		TestRun run = (TestRun) apl.getBean("run");
		//调用bean中方法
		run.test_ref.print_null_string();
	}
}

test中加入了判断

package com.test;

public class Test {

	String null_string;
	
	public String getNull_string() {
		return null_string;
	}
	
	public void setNull_string(String null_string) {
		this.null_string = null_string;
	}
	
	public void print_null_string(){
		if(null_string == null){
			System.out.println("null........");
		}
	}
}

猜你喜欢

转载自blog.csdn.net/Milan__Kundera/article/details/82053750