Bean的属性注入

集合实体类
public class CollectionDemo {
	private List list;
	private Set set;
	private Map map;
	private Properties prop;
Car实体类
public class Car {
	private String name;
	private double price;
Person实体类
public class Person {
	private String name;
	private Car car;
applicationContext.xml

<!-- 构造方法注入 -->
	<bean name="car" class="di.Car">
		<constructor-arg index="0" type="java.lang.String" value="奥迪" ></constructor-arg>
		<constructor-arg index="1" type="double" value="400000"></constructor-arg>
	</bean>
	
	<!-- set方法注入 -->
	<bean name="car1" class="di.Car">
		<property name="name" value="宝马"></property>
		<property name="price" value="500000"></property>
	</bean>
	
	<bean name="person" class="di.Person">
		<property name="name" value="张三"></property>
		<!-- ref:引用 -->
		<property name="car" ref="car1"></property>
	</bean>
	
	<!-- 集合属性注入 -->
	<bean id="collectionDemo" class="di.CollectionDemo">
		<property name="list">
			<list>
				<value>张三</value>
				<value>10</value>
				<ref bean="car"/>
			</list>
		</property>
		
		<property name="set">
			<set>
				<value>李四</value>
				<value>20</value>
				<ref bean="person"/>
			</set>
		</property>
		
		<property name="map">
			<map>
				<entry key="username" value="李四"></entry>
				<entry key-ref="person" value-ref="car"></entry>
			</map>
		</property>
		
		<property name="prop">
			<props>
				<prop key="company">yl</prop>
				<prop key="price">10000000</prop>
			</props>
		</property>
		
	</bean>
DiTest
	//构造方法注入
	@Test
	public void test1() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) applicationContext.getBean("car");
		System.out.println(car.getName()+" "+car.getPrice());
	}
	//set方法注入
	@Test
	public void test2() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Car car = (Car) applicationContext.getBean("car1");
		System.out.println(car.getName()+" "+car.getPrice());
	}
	
	//set方法注入
	@Test
	public void test3() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		Person person = (Person) applicationContext.getBean("person");
		System.out.println(person.getName()+" "+person.getCar().getName());
	}

	//测试集合属性注入---list
	@Test
	public void test4() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionDemo cd = (CollectionDemo) applicationContext.getBean("collectionDemo");
		System.out.println(cd.getList());
	}
	
	//测试集合属性注入---set
	@Test
	public void test5() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionDemo cd = (CollectionDemo) applicationContext.getBean("collectionDemo");
		System.out.println(cd.getSet());
	}
	
	//测试集合属性注入---map
	@Test
	public void test6() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionDemo cd = (CollectionDemo) applicationContext.getBean("collectionDemo");
		System.out.println(cd.getMap());
	}
	
	// 测试集合属性注入---Properties
	@Test
	public void test7() {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CollectionDemo cd = (CollectionDemo) applicationContext.getBean("collectionDemo");
		System.out.println(cd.getProp());
	}
p/c命名空间
<!-- p名称空间 -->
    <bean id="dog" class="pc.Dog" p:name="小白" p:color="白色"></bean>
    <!-- c名称空间 -->
    <bean id="dog1" class="pc.Dog" c:name="小黑" c:color="黑色"></bean>
    
    <bean id="person" class="pc.Person" p:name="小王" p:dog-ref="dog1" p:age="20"></bean>
    
    <!-- SpEl表达式 -->
    <bean id="person1" class="pc.Person">
    	<property name="name" value="#{person.name}"></property>
    	<property name="dog" value="#{dog1}"></property>
    	<property name="age" value="#{person.age+10}"></property>
    	<!-- <property name="age" value="#{person.getAge()+10}"></property> -->
    </bean>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值