当先锋百科网

首页 1 2 3 4 5 6 7

对代码不满足,是任何真正有天才的程序员的根本特征。
1. 基本类型值
2. 注入bean;
3. 内部bean; 在property内部加个bean
4. Null值
5. 级联属性
6. 集合类型属性

基本类型值注入:在bean中加入property 通过value直接赋值
  <bean id="people1" class="com.java1234.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
	</bean>

但是你的实体类People中必须有这些属性名 id,name,age;并且有set方法

注入Bean类型:在bean中加入将一个类作为属性 通过property的ref依赖另一个bean(前提这个bean是有值的),并且在类中也要申明这个类属性属性和他的set方法,一般set的方法是通过xml自动注入的

 <bean id="people2" class="com.java1234.entity.People">
		<property name="id" value="2"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog" ref="dog"></property>
	</bean>
	<bean id="dog" class="com.java1234.entity.Dog">
	    <property name="name" value="jack"></property>
	</bean>

	public class People {
	private int id;
	private String name;
	private int age;
	private Dog dog;
	public void setDog(Dog dog) {
		this.dog = dog;
	}
	}
package com.java1234.entity;

public class Dog {
    private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
	

内部bean:在你的bean中property中加入一个bean类似内部类 跟外部申明一样class都要申明清楚

    <bean id="people3" class="com.java1234.entity.People">
		<property name="id" value="3"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog">
		   <bean class="com.java1234.entity.Dog">
	   			 <property name="name" value="Tom"></property>
		  </bean>
		</property>
	</bean>

Null值:仅限申明null值 同内部bean一样 在property内部用null标签

   <bean id="people4" class="com.java1234.entity.People">
	<property name="id" value="4"></property>
	<property name="name" value="张三"></property>
	<property name="age" value="11"></property>
	<property name="dog">
	    <null></null>
	</property>
  </bean>

级联属性:在property name的输入中类似对象成员域的调用dog.name

     <bean id="people5" class="com.java1234.entity.People">
		<property name="id" value="1"></property>
		<property name="name" value="张三"></property>
		<property name="age" value="11"></property>
		<property name="dog.name" value="Jack2"></property>
	</bean>	
</beans>

这里有3个需要注意的地方:1.在people类中的dog属性需要实例化new Dog();
2.同时在people类中要有dog的get方法 否则会报错

3.在输出dog名字时候 要dog.getName;
配置文件中配置bean的属性时,property标签不仅能直接配置该bean对应的类自身的属性,而且还可以直接配置该类的属性对象向的属性,即级联属性。这里的级联属性被赋值的前提是级联属性所属的对象属性已经被属性注入或构造器注入赋值,因为一般没有注入,所以要new出空对象,让xml注入属性对象的属性,否则会报异常。

<bean id="people5" class="com.java1234.entity.People">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="11"></property>
<property name="dog.name" value="Jack2"></property>
</bean>
    package com.java1234.entity;

public class People {
	private int id;
	private String name;
	private int age;
	private Dog dog=new Dog();//级联属性需要实例化dog
	
	public void setDog(Dog dog) {
		this.dog = dog;
	}

	public Dog getDog() {
		return dog;
	}

	public void setId(int id) {
		this.id = id;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", age=" + age
				+ ", dog=" + dog.getName()+ "]";
	}

}

集合类型属性:property中加入list set props等子标签以 value子标签赋值 分别对应People类中ArrayList HashSet Properties等类型属性 还有HashMap对应map.entry.key(value子标签赋值)和map.entry.value键值对

 <property name="hobbies">
			<list>
				<value>唱歌</value>
				<value>跳舞</value>
			</list>
		</property>
		<property name="loves">
			<set>
				<value>唱歌2</value>
				<value>跳舞2</value>
			</set>
		</property>
		<property name="works">
			<map>
				<entry>
					<key><value>上午</value></key>
					<value>写代码</value>
				</entry>
				<entry>
					<key><value>下午</value></key>
					<value>测试代码</value>
				</entry>
			</map>
		</property>
		<property name="addresses">
			<props>
				<prop key="address1">aaaaa</prop>
				<prop key="address2">bbbbb</prop>
			</props>
		</property>