首先到http://www.springsource.org/download下载spring,然后进行解压缩,在解压目录下找到下面jar文件,拷贝到类路径下

dist\spring.jar

lib\jakarta-commons\commons-logging.jar

如果使用了切面编程(AOP),还需要下列jar文件

lib/aspectj/aspectjweaver.jar 和aspectjrt.jar

lib/cglib/cglib-nodep-2.1_3.jar

如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件

lib\j2ee\common-annotations.jar

当包导入后就可进行spring编程了

首先在项目底下建立一个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">      </beans>

然后在<beans>底下输入<bean></bean>进行类的自动装载

如在com.dao底下有一个叫做PersonDao的接口,在com.dao.impl底下有一个personDaoBean的实现类

则<bean>内容为 <bean id="personDao" class="com.txr.dao.impl.PersonDaoBean"/>

id是对类取的名字,class是要实例化的类(如果类取的名字有特殊字符如"/"则建议用name,id是唯一的,而name可以有多个)

如在com.service.impl下有个叫做PersonSeviceBean,PsersonServiceBean中有个属性类型为PersonDao则在装载时可以如下

<bean id="personService" class="com.txr.service.impl.PersonServiceBean">                <property name="persondao" ref="personDao"/><!-- ref表示映射上面的personDao-->               <property name="id" value="1"></property><!-- (基础类型可如此赋值)-->    </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: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">            <!-- 使用静态工厂方法实例化 -->           <bean id="personService" class="com.txr.service.impl.PersonServiceBeanFactory"             factory-method="createPersonServiceBean"></bean> </beans>

在com.txr.service.impl下的PersonServiceBeanFactory中创建createPersonServiceBean()方法返回PersonServiceBean实例就可

方法三使用实例化工厂方法实例化

<?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">          <!-- 使用实例工厂方法实例化 -->             <bean id="personServiceFactory" class="com.txr.service.impl.PersonServiceBeanFactory"/>            <bean id="personService" factory-bean="personServiceFactory" factory-method="createPersonServiceBean"></bean></beans>

以上所有装载方式默认为单例模式即single模式,当你第二次实例时所取对象与第一次相同,而如果你想实例化多个

不同对象这时你可以使用scope属性。即:

<bean id="personService" class="com.txr.service.impl.PersonServiceBean" Scope="prototype">               <property name="persondao" ref="personDao"/><!-- ref表示映射上面的personDao-->              <property name="id" value="1"></property><!-- (基础类型可如此赋值)-->   </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"               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:component-scan base-package="com"/><!--base-package指的是哪个包--> </beans>

这样就能全部进行扫描了,对了这时你需要在类上进行标注哪些是需要交给spring来管理的。

标注分为四种分别是:

@Service 用于标注业务层组件 @Controller用于标注控制层组件 @Repository用于标注数据访问组件,即dao组件 @Component泛指组件,当组件不好归类的时候我们可以使用这个注解进行标注

这样标注后这些类就会自动由spring进行管理了

在属性值进行装载时也可以使用注解的方式进行

只需在属性上标注@Resource即可当然也可以使用@Autowried不过更推荐使用@Resource,此标注由javaEE所提供

在JDK6.0版本之后就可以使用了