spring—-配置bean(通过反射)—-笔记

配置文件

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



<!-- 
	默认的bean是单例的 
	如需更改使用scope属性 
	scope="singleton"
	prototype
	singleton
	request
	session
-->
	<bean  id="hispring" class="top.demo.test.spring1.HiSpring">
		<property name="str"  value="hahahahaha"></property>
		
		<!-- 使用null标签给引用类型赋值null -->
		<property name="other"><null></null></property>
		
		<!--ref 属性给引用类型赋值  value 为xml内bean的id  -->
		<property name="other2" ref="other"></property>
		
		<!--使用内部bean给引用类型赋值  -->
		<property name="other3">
			<bean class="top.demo.test.spring1.Other">
				<!--使用value标签给属性赋值  -->
				<property name="str"><value>123</value></property>
			</bean>
		</property>
	</bean>


	<!--p名称空间  和 容器的赋值-->
	<bean id="other" class="top.demo.test.spring1.Other"  p:nt="123" p:str="使用p名称空间的进行属性赋值" >
		<property name="map">
			<map>
				<entry key="1" value="这是赋值给map,1"></entry>
				<entry key="2" value="这是赋值给map,3"></entry>
				<entry key="3" value="这是赋值给map,3"></entry>
			</map>
		</property>
		
		<property name="list">
			<list>
				<value>这是给list赋值1</value>
				<value>这是给list赋值2</value>
			</list>
		</property>
	</bean>
	
	<!--属性的继承parent  和 bean之间的依赖关系depends-on  -->
	<bean id="other2" class="top.demo.test.spring1.Other" parent="other" depends-on="other">
		<property name="map">
			<map>
				<entry key="1" value="这是继承other1的属性配置,并且覆盖了map属性里的一条数据"></entry>
			</map>
		</property>
		
		<!--使用构造器进行属性赋值 和 普通的property一样用	
			<constructor-arg name=""  value="" type=""></constructor-arg>
		-->

	</bean>
	
	<!--Properties的赋值   -->
	<bean id="other3" class="top.demo.test.spring1.Other">
		<property name="pro">
			<props>
				<prop key="这是给Properties">赋值</prop>
			</props>
		</property>
	</bean>
	
	
	<util:list id="listpack">
		<value>这是</value>
		<value>通过</value>
		<value>util命名空间生成单独的ListBean以便其他bean引用</value>
	</util:list>
	
	
	<!--自动装配   autowire属性设置自动装配模式
	byName 其实是通过set方法如果后面的方法名匹配 xml里的某个bean就被赋值 不匹配就不赋值
	byType  是匹配类型 。通过Class全类名  如果有两个如上面的other2 3都是一个类但是有多个id就会异常
	-->
	<bean  id="hispring2" class="top.demo.test.spring1.HiSpring" autowire="byName">

	</bean>
	
	
	
</beans>

测试用到的类

package top.demo.test.spring1;

public class HiSpring {
	
	public String str;
	public Other other;
	public Other other2;
	public Other other3;

	public void setStr(String str) {
		this.str = str;
	}
	
	public void test() {
		
		System.out.println("hi this is HiSpring.test()+str:"+str);
		
	}


	@Override
	public String toString() {
		return "HiSpring [str=" + str + ", other=" + other + ", other2=" + other2 + ", other3=" + other3 + "]";
	}

	public void setOther(Other other) {
		this.other = other;
	}
	
	public void setOther2(Other other) {
		this.other2 = other;
	}
	public void setOther3(Other other) {
		this.other3 = other;
	}
	


}

package top.demo.test.spring1;

import java.util.List;
import java.util.Map;
import java.util.Properties;

public class Other {
	
	public int nt;
	public String str;
	public Map<Integer, String> map;
	public List<String> list;
	public Properties pro;
	
	public int getNt() {
		return nt;
	}
	public void setNt(int nt) {
		this.nt = nt;
	}

	@Override
	public String toString() {
		return "Other [nt=" + nt + ", str=" + str + ", map=" + map + ", list=" + list + ", pro=" + pro + "]";
	}
	public Properties getPro() {
		return pro;
	}
	public void setPro(Properties pro) {
		this.pro = pro;
	}
	public String getStr() {
		return str;
	}
	public void setStr(String str) {
		this.str = str;
	}
	public Map<Integer, String> getMap() {
		return map;
	}
	public void setMap(Map<Integer, String> map) {
		this.map = map;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}

}

测试的main入口

package top.demo.test.spring1;

import java.util.List;

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

public class Main {
	
	
	public static void main(String argv[]) {
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		HiSpring hi=(HiSpring)ctx.getBean("hispring");
		HiSpring hi2=(HiSpring)ctx.getBean("hispring2");
		Other other2=(Other) ctx.getBean("other2");
		Other other3=(Other) ctx.getBean("other3");
		List<String> listpack=(List<String>) ctx.getBean("listpack");
		System.out.println(hi);
		System.out.println(hi2);
		System.out.println(other2);
		System.out.println(other3);
		System.out.println(listpack.get(2));
		
	}
	
}

输出结果

HiSpring [str=hahahahaha, other=null, other2=Other [nt=123, str=使用p名称空间的进行属性赋值, map={1=这是赋值给map,1, 2=这是赋值给map,3, 3=这是赋值给map,3}, list=[这是给list赋值1, 这是给list赋值2], pro=null], other3=Other [nt=0, str=123, map=null, list=null, pro=null]]
HiSpring [str=null, other=Other [nt=123, str=使用p名称空间的进行属性赋值, map={1=这是赋值给map,1, 2=这是赋值给map,3, 3=这是赋值给map,3}, list=[这是给list赋值1, 这是给list赋值2], pro=null], other2=Other [nt=123, str=使用p名称空间的进行属性赋值, map={1=这是继承other1的属性配置,并且覆盖了map属性里的一条数据}, list=[这是给list赋值1, 这是给list赋值2], pro=null], other3=Other [nt=0, str=null, map=null, list=null, pro={这是给Properties=赋值}]]
Other [nt=123, str=使用p名称空间的进行属性赋值, map={1=这是继承other1的属性配置,并且覆盖了map属性里的一条数据}, list=[这是给list赋值1, 这是给list赋值2], pro=null]
Other [nt=0, str=null, map=null, list=null, pro={这是给Properties=赋值}]
util命名空间生成单独的ListBean以便其他bean引用


版权声明:本文为qq_27617675原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>