Spring学习系列之BeanFactory入门(1)

简介:

Spring的BeanFactory接口是Spring框架的最核心接口,它提供了高级IoC配置机制,Spring为BeanFactory提供了多种实现,其中最常用的XmlBeanFactory,它的getBean()方法可以从指定的XML文件中读取数据,返回特定的Bean. 本文从最简单的例子来展示如何使用BeanFactory

 

Bike类:

 

package com.james.beanfactory;

public class Bike {
	private String brand;
	private String type;
	private int price;
	
	public Bike() {
		System.out.println("In Bike Constructor");	
	}
	
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getPrice() {
		return price;
	}
	public void setPrice(int price) {
		this.price = price;
	}
	
	public void display(){
		System.out.println("This is " + brand + " " + type + ", You can get me by CNY: " + price);
	}
	
	public void rideTo(String add){
		System.out.println("Riding to: " + add);
	}

}

 

 

Rider类:

 

package com.james.beanfactory;

public class Rider {
	private String name;
	private Bike bike;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Bike getBike() {
		return bike;
	}
	public void setBike(Bike bike) {
		this.bike = bike;
	}
	
	public void rideTo(String to) {
		System.out.println("In Rider.rideTo()");
		bike.rideTo(to);
	}	

}

 

 

XML bean配置文件:

 

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
	<bean id="bike" class="com.james.beanfactory.Bike" 
		 p:brand="Giant"
		 p:type="ATX 770D"
		 p:price = "2598"
		 />
	<bean id="rider" class="com.james.beanfactory.Rider" 
		 p:name="james"
		 p:bike-ref="bike"
		 />
</beans>

 

 

测试类:

 

package com.james.beanfactory;

import java.io.IOException;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

public class BeanTest {

	public static void main(String[] args) throws IOException {
		// Read the XML configuration file.
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource res = resolver.getResource("classpath:com/james/beanfactory/beans.xml");
		System.out.println(res.getURL());
		
		// Get and use bean.
		BeanFactory bf = new XmlBeanFactory(res);
		Bike myBike = bf.getBean("bike", Bike.class);
		myBike.display();
		
		// A Rider contains a Bike, The dependancy is set in xml. 
		Rider r = bf.getBean("rider", Rider.class);
		r.rideTo("Tibet");
	}

}

 

 

运行结果:

 

file:/E:/masterSpring/chapter3/WebRoot/WEB-INF/classes/com/james/beanfactory/beans.xml
2013-11-02 22:35:15,823  INFO [main] (XmlBeanDefinitionReader.java:315) - Loading XML bean definitions from class path resource [com/james/beanfactory/beans.xml]
In Bike Constructor
This is Giant ATX 770D, You can get me by CNY: 2598
In Rider.rideTo()
Riding to: Tibet

 

 

说明:

  1. 此处我们的Bike和Rider都不需要参数去构造
  2. XML的配置文件里我们传递的是Bike和Rider的属性,以及一个依赖关系
  3. 可以使用多种注入方式获取一个Bean,本例子仅仅只是用了属性注入

你可能感兴趣的:(beanfactory)