Spring实战篇(一) —— XML注入(基础)

在开始第一个Spring应用前,让我们先做如下一些准备:
JDK
1.5以上)
Eclipse
Maven
m2eclipse插件(可选,如果熟悉Maven可以很快的在项目中加入所需的Spring Jar包,如果想用Maven,那就自学一下,只要学一点基础就可以了)

 

Ok,让我们开始吧。首先,在Eclipse中创建一个Maven ProjectJava Project,如果创建Maven Project,在第一个页面直接勾选Create a simple project即可:


 

然后点击next,下一步中的输入任意Group IdArtifact Id即可。

 

创建好Maven工程后,在pom.xml中添加如下信息:

	<properties>
		<spring.version>3.2.0.RELEASE</spring.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>

  

添加的jar包如下:


 

那么没用Maven的同学就自己去网上找下这三个jar包添加到自己的工程中吧,其中Spring jar的版本可以任意,最好是2.5以上并且Springjar包之间的版本号最好保持一致。

  

一切准备就绪,下面就开始code了。假设我们需要一个Car的类,其中包含一些汽车的部件,比如发动机和轮胎,那么首先我们来构造这些零件,创建如下的两个抽象类:

  

 Engine抽象类及实现类:

package com.zrabbit.production.part; 

public abstract class Engine 
{ 
    public abstract void launch(); 

    public abstract void showInfo(); 
}  
package com.zrabbit.production.part; 

public class EngineOne extends Engine 
{ 
    @Override 
    public void launch() 
    { 
        System.out.println(&quot;Engine one lauch!!&quot;); 
    } 

    @Override 
    public void showInfo() 
    { 
        System.out.println(&quot;Engine is One!!&quot;); 
    } 
} 
 

  Wheel抽象类及实现类:

package com.zrabbit.production.part; 

public abstract class Wheel 
{ 
    public abstract void showInfo(); 
}
  
package com.zrabbit.production.part; 

public class WheelNumOne extends Wheel 
{ 
    @Override 
    public void showInfo() 
    { 
        System.out.println(&quot;Wheel is No.1!&quot;); 
    } 
} 

 

好吧,上面的代码很简单,现在我们来构架Car

package com.zrabbit.production; 

import java.util.List; 

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 

import com.zrabbit.production.part.Engine; 
import com.zrabbit.production.part.Wheel; 

@SuppressWarnings(deprecation) 
public class Car 
{ 
    private Engine engine; 

    private List wheels; 

    public Engine getEngine() 
    { 
        return engine; 
    } 

    public void setEngine(final Engine engine) 
    { 
        this.engine = engine; 
    } 

    public List getWheels() 
    { 
        return wheels; 
    } 

    public void setWheels(final List wheels) 
    { 
        this.wheels = wheels; 
    } 

    public void showInfo() 
    { 
        engine.showInfo(); 
        for (final Wheel wheel : wheels) 
        { 
            wheel.showInfo(); 
        } 
    } 

    public void launch() 
    { 
        engine.launch(); 
    } 
}

  

接下来在工程任意位置创建一个XML文件,此文件为Spring配置文件,由于习惯,我在src/main/resources下创建了一个名叫applicationConfig.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.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<bean id="myCar" class="com.zrabbit.production.Car">
		<property name="engine" ref="engineOne" />
		<property name="wheels" >
			<list>
				<ref bean="wheelNumOne" />
				<ref bean="wheelNumOne" />
				<ref bean="wheelNumOne" />
				<ref bean="wheelNumOne" />
			</list>
		</property>
	</bean>
	
	<bean id="engineOne" class="com.zrabbit.production.part.EngineOne" />
	<bean id="wheelNumOne" class="com.zrabbit.production.part.WheelNumOne" />
</beans>
 

其中根标签beans表明其内部为Spring bean的配置信息,Spring bean其实就是一个交由Spring管理的pojoSpring通过Ioc将这些bean的成员填充进去。

 

beans标签会有若干个命名空间的别名及Location定义,这些在初学的时候可以不必太关心,copy下即可。其中别名代表在这个配置文件中需要用到的模块,比如context,aop,jdbc等等。xsi:schemaLocation则指定这些模块的xsd文件所在url,这些文件主要用于提示和验证。 

 

beans标签内会包含若干个bean标签,每一个bean标签就代表一个Spring bean。其中id为唯一表示,通常用于成员依赖(reference)和取到这个beanclass指明这个bean关联的pojo,一般情况下,每一个bean都必须指明其class

 

bean标签的内部是property标签,每一个property标签都代表这个bean关联的pojo中的一个成员,简单类型,如基本类型、基本类型的包装类、String等可以通过value属性制定,复杂类型一般通过ref属性找到对应的bean进行注入(当然像内部bean这样的方法也可以)。

 

最后说一下Car中的wheels成员,这个成员是一个List类型,在指定这个property的时候,可以嵌套使用list标签,其内部可以使用refvalue标签。

 

接下来就可以运行一下第一个Spring程序了,在Car类中加一个main方法:

    public static void main(final String[] args) 
    { 
        final Resource res = new ClassPathResource("applicationContext.xml"); 
        final BeanFactory bf = new XmlBeanFactory(res); 
        final Car myCar = (Car) bf.getBean("myCar"); 
        myCar.showInfo(); 
    } 

 

 Okrun一下就会出现一下结果:

 这表明第一个Spring应用已经成功。

猜你喜欢

转载自z-rabbit.iteye.com/blog/1853066