Spring之属性注入

代码总览

代码

db.properties

UserId=sa
Password=sa

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-2.5.xsd
		http://www.springframework.org/schema/context 
       	http://www.springframework.org/schema/context/spring-context-2.5.xsd
       	">
       	
	<context:property-placeholder location="classpath:db.properties" />
	
	<bean id="userDao" class="com.itlwc.Test.UserDaoImpl">
		<!-- 简单属性注入 -->
		<property name="name" value="Lwc" />
		
		<!-- 读取配置文件注入 -->
		<property name="username" value="${UserId}" />
		<property name="password" value="${Password}" />
		
		<!-- 集合注入 -->
		<property name="sets">
			<set>
				<value>1</value>
			</set>
		</property>
		<property name="lists">
			<list>
				<value>1</value>
				<value>2</value>
			</list>
		</property>
		<property name="maps">
			<map>
				<entry key="1" value="1"/>
				<entry key="2" value="2"/>
				<entry key="3" value="3"/>
			</map>
		</property>
	</bean>
</beans>

Test.java

package com.itlwc.Test;

import java.util.List;
import java.util.Map;
import java.util.Set;

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

//dao
interface UserDao {
	public void save();
}

// daoImp
class UserDaoImpl implements UserDao {
	// 简单属性注入
	private String name;

	// 读取配置文件注入
	private String username;
	private String password;

	// 集合注入
	private Set<String> sets;
	private List<String> lists;
	private Map<String, String> maps;

	public String getName() {
		return name;
	}

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

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Set<String> getSets() {
		return sets;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Map<String, String> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, String> maps) {
		this.maps = maps;
	}

	public void save() {
		System.out.println("简单注入 : name = " + name);
		System.out.println("配置文件注入 : " + "username = " + username
				+ " | password = " + password);
		System.out.println("集合注入长度分配为 : " + sets.size() + "," + lists.size()
				+ "," + maps.size());
	}
}

public class Test {
	public static void main(String[] args) {
		// 尽量使用ApplicationContext不使用BeanFactory,因为功能更强大
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		UserDaoImpl udi = (UserDaoImpl) ac.getBean("userDao");
		udi.save();
	}
}

打印结果

简单注入 : name = Lwc
配置文件注入 : username = sa | password = sa
集合注入长度分配为 : 1,2,3

你可能感兴趣的:(Spring之属性注入)