Spring对象属性注入

注入--对象属性赋值

一:基础数据的set注入

1)实体类User

public class User {

	private int age;
	private String username;
	private String password;
}

对User类添加setter/getter

2)配置文件,比如叫beans.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" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context                http://www.springframework.org/schema/context/spring-context-3.0.xsd">
	<!-- id 表示你这个组件的名字,class表示组件类 -->
	<bean id="user" class="com.demo.User">
		<property name="age">
			<value>25</value>
		</property>
		<property name="username">
			<value>Tom</value>
		</property>
		<property name="password">
			<value>123456</value>
		</property>
	</bean>
}

3)测试类UserTest

public class UserTest {
	
	/**
	 * 测试基础数据类型的set注入
	 * */
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml");
		User user = ctx.getBean("user",User.class);// 自己不用进行类型转换
		System.out.println(user);
		System.out.println(user.getAge());
		System.out.println(user.getUsername());
		System.out.println(user.getPassword());
	}
}

二:基础数据类型的构造器注入(构造方法)

1)实体类Student

public class Student {

	private int age;
	private String username;
	private String password;

	// 明写缺省构造方法
	public Student() {
	}

	// 重写构造方法
	public Student(String username, String password) {
		this.username = username;
		this.password = password;
	}

	// 重写构造方法
	public Student(int age, String username, String password) {
		this.age = age;
		this.username = username;
		this.password = password;
	}
}

2)配置文件

<bean id="student" class="com.demo.Student">
		<constructor-arg value="18"></constructor-arg>
		<constructor-arg value="Lilei"></constructor-arg>
		<constructor-arg value="111111"></constructor-arg>
	</bean>

如果是多构造器,应该指定参数的类型和顺序

<constructor-arg value="18" type="int" index="0"></constructor-arg>
		<constructor-arg value="Lilei" type="String" index="1"></constructor-arg>

3)测试类SrudentTest

public class StudentTest {

	/**
	 * 测试基础数据类型的构造方法注入
	 * */
	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml");
		Student student = ctx.getBean("student",Student.class);
		System.out.println(student);
		System.out.println(student.getAge());
		System.out.println(student.getUsername());
		System.out.println(student.getPassword());
	}
}

三:特殊数据类型的注入

1)实体类Person

public class Person {

	private String name;
	private int age;
	private double money;
	private List<String> list;
	private Set<String> set;
	private Map<String,String> map;
	private String[] array;
	private Properties properties;
	private User user;
}

为Person类增加setteer/getter

2)配置文件

<bean id="person" class="com.demo.Person">
		<property name="name">
			<value>Tom</value>
		</property>
		<property name="age">
			<value>28</value>
		</property>
		<property name="money">
			<value>100.9</value>
		</property>
		<property name="list">
			<list>
				<value>shandong</value>
				<value>杭州</value>
				<value>xihu</value>
			</list>
		</property>
		<property name="set">
			<set>
				<value>13900</value>
				<value>13900</value>
				<value>13800</value>
			</set>
		</property>
		<property name="map">
			<map>
				<entry>
					<key>
						<value>key1</value>
					</key>
					<value>value1</value>
				</entry>
				<entry key="key2" value="value2">
				</entry>
			</map>
		</property>
		<property name="array">
			<list>
				<value>xiaoh</value>
				<value>xiaob</value>
			</list>
		</property>
		<property name="properties">
			<props>
				<prop key="pp1">pp1Value</prop>
				<prop key="pp2">pp2Value</prop>
			</props>
		</property>
		<property name="user">
			<ref bean="user"/>
		</property>
	</bean>
3)测试类PersonTest
public class PersonTest {

	public static void main(String[] args) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("/beans.xml");
		Person person = ctx.getBean("person",Person.class);
		System.out.println(person);
		System.out.println(person.getName());
		System.out.println(person.getAge());
		System.out.println(person.getMoney());
		System.out.println(person.getList());
		System.out.println(person.getSet());
		System.out.println(person.getMap());
		System.out.println(person.getArray());
		System.out.println(person.getProperties());
		System.out.println(person.getUser());
		System.out.println(person.getUser().getUsername());
	}
}


你可能感兴趣的:(java,spring,bean)