首页 > 代码库 > spring中的bean装配详解

spring中的bean装配详解

本文主要介绍了spring配置文件的配置:set方法,constructor构造函数注入各种类型的属性值,bean的继承,自动装配autowire以及通过spring提供的特殊bean进行了分散配置.

首先,配置文件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"
		xmlns:tx="http://www.springframework.org/schema/tx"
		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
				http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
				default-autowire="no">	
			<!-- <context:property-placeholder location="classpath:com/assembly/db.properties,classpath:com/assembly/db1.properties"/>	 -->
<bean id="personService" class="com.assembly.PersonService" >
		<property name="name" >
				<value>siege</value>
		</property>
		<property name="bookList">
				<list>
						<ref bean="book1"/>
						<ref bean="book1"/>
						<ref bean="book2"/>
						<ref bean="book2"/>
				</list>
		</property>
		<property name="bookSet">
				<set>
						<ref bean="book1"/>
						<ref bean="book1"/>
						<ref bean="book2"/>
						<ref bean="book2"/>
				</set>
		</property>
		<property name="map">
				<map>
						<entry key="11" value-ref="book1"/>
						<entry key="11" value-ref="book1"/>
						<entry key="22" value-ref="book2"/>
						<entry key="22" value-ref="book2"/>
				</map>	
		</property>
		<property name="pro">
				<props>
						<prop key="001" >hello</prop>
						<prop key="002">world</prop>
				</props>
		</property>
</bean>
<bean id="book1"  class="com.assembly.Book">
		<property name="title">
				<value>CSS</value>
		</property>
		<property name="id">
				<value>1</value>
		</property>
</bean>
<bean id="book2"  class="com.assembly.Book">
		<property name="title">
				<value>JavaScript</value>
		</property>
		<property name="id">
				<value>2</value>
		</property>
</bean>
<bean id="student" parent="personService" class="com.assembly.Student">
		<property name="score">
				<value>100</value>
		</property>
</bean>
<!-- 以下是通过构造函数进行属性注入 -->
<bean id="worker" class="com.assembly.Worker">
		<constructor-arg index="0" type="java.lang.String" value=http://www.mamicode.com/"cage"/>>Book.java

package com.assembly;

public class Book {
	private String title;
	private int id;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Override
	public String toString() {
		return "bookId:"+this.id+",bookTitle:"+this.title;
	}
	
}
DBUtil.java

package com.assembly;

public class DBUtil {
	private String driver;
	private String name;
	private String pwd;
	private String url;
	
	public String getDriver() {
		return driver;
	}
	public void setDriver(String driver) {
		this.driver = driver;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}

}

Dog.java

package com.assembly;

public class Dog {
		private String name;
		private int age;
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
}

Master.java

package com.assembly;

public class Master {
		private String name;
		private Dog dog;
		
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Dog getDog() {
			return dog;
		}
		public void setDog(Dog dog) {
			this.dog = dog;
		}

}

PersonService.java

package com.assembly;

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

public class PersonService{
	private String name;
	private List<Book> bookList;
	private Set<Book> bookSet;
	private Map<String,Book> map;
	private Properties pro;
	
	
	
	public Properties getPro() {
		return pro;
	}
	public void setPro(Properties pro) {
		this.pro = pro;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<Book> getBookList() {
		return bookList;
	}
	public void setBookList(List<Book> bookList) {
		this.bookList = bookList;
	}
	public Set<Book> getBookSet() {
		return bookSet;
	}
	public void setBookSet(Set<Book> bookSet) {
		this.bookSet = bookSet;
	}
	public Map<String, Book> getMap() {
		return map;
	}
	public void setMap(Map<String, Book> map) {
		this.map = map;
	}
}

Student.java

package com.assembly;

public class Student extends PersonService {
		private int score;

		public int getScore() {
			return score;
		}

		public void setScore(int score) {
			this.score = score;
		}
}
Worker.java

package com.assembly;

public class Worker {
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}

	public Worker(String name, int age) {
		this.name = name;
		this.age = age;
	}

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Worker() {
	}

}

db.properties

driver=driver
name=name
pwd=pwd
url=url

db1.properties
user.driver=driver1
user.name=name1
user.pwd=pwd1
user.url=url1


测试类Test

package com.assembly;

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

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

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("com/assembly/beans.xml");
		PersonService p=(PersonService) ac.getBean("personService");
		Student student=(Student) ac.getBean("student");
		Worker worker= (Worker) ac.getBean("worker");
		Master master=(Master) ac.getBean("master");
		DBUtil dbutil=(DBUtil) ac.getBean("dbutil1");
		String name=p.getName();
		System.out.println(name);
		List<Book> bookList=p.getBookList();
		Set<Book> bookSet=p.getBookSet();
		Map<String,Book> map=p.getMap();
		Properties pro=p.getPro();
		System.out.println("********List******");
		for(Book book:bookList){
			System.out.println("bookId:"+book.getId()+" bookTitle:"+book.getTitle());
		}
		System.out.println("********Set******");
		for(Book book:bookSet){
			System.out.println("bookId:"+book.getId()+" bookTitle:"+book.getTitle());
		}
		System.out.println("********Map******");
		for(Entry<String, Book> book:map.entrySet()){
			System.out.println("bookId:"+book.getValue().getId()+" bookTitle:"+book.getValue().getTitle());
		}
		System.out.println("********Properties******");
		for(Entry<Object,Object> pp:pro.entrySet()){
			System.out.println("key:"+pp.getKey()+" value:"+pp.getValue());
		}
		System.out.println("********关于bean的继承*********");
		System.out.println("score:"+student.getScore()+" name:"+student.getName()+"\r\n"+"list:"+student.getBookList()+"\r\n"+
										"set:"+student.getBookSet()+"\r\n"+"map:"+student.getMap()+"\r\n"+"properties:"+student.getPro());
		System.out.println("*********通过构造函数注入属性值********");
		System.out.println("name:"+worker.getAge()+" age:"+worker.getName());
		System.out.println("********关于自动装配autowire");
		System.out.println("masterName:"+master.getName()+" dogName:"+master.getDog().getName()+" dogAge:"+master.getDog().getAge());
		System.out.println("*******关于分散配置*********");
		System.out.println("driver:"+dbutil.getDriver()+"\r\nname:"+dbutil.getName()+"\r\npwd:"+dbutil.getPwd()+"\r\nurl:"+dbutil.getUrl());
	}

}

最后的结果如下:

siege
********List******
bookId:1 bookTitle:CSS
bookId:1 bookTitle:CSS
bookId:2 bookTitle:JavaScript
bookId:2 bookTitle:JavaScript
********Set******
bookId:1 bookTitle:CSS
bookId:2 bookTitle:JavaScript
********Map******
bookId:1 bookTitle:CSS
bookId:2 bookTitle:JavaScript
********Properties******
key:002 value:world
key:001 value:hello
********关于bean的继承*********
score:100 name:siege
list:[bookId:1,bookTitle:CSS, bookId:1,bookTitle:CSS, bookId:2,bookTitle:JavaScript, bookId:2,bookTitle:JavaScript]
set:[bookId:1,bookTitle:CSS, bookId:2,bookTitle:JavaScript]
map:{11=bookId:1,bookTitle:CSS, 22=bookId:2,bookTitle:JavaScript}
properties:{002=world, 001=hello}
*********通过构造函数注入属性值********
name:24 age:cage
********关于自动装配autowire
masterName:siege dogName:小黄 dogAge:3
*******关于分散配置*********
driver:driver1
name:name1
pwd:pwd1
url:url1






spring中的bean装配详解