1.下载 spring 框架 http://www.springsource.org/download/community
2.将spring lib目录下的这些包拷贝到我们应用的lib中
commons-logging.jar
spring-beans-4.0.0.M1.jar
spring-context-4.0.0.M1.jar
spring-core-4.0.0.M1.jar
spring-expression-4.0.0.M1.jar
3.建立一个 bean 类
代码如下 |
复制代码 |
package org.Rudiment.spring;
public class PersonService
{
private String name;
public void setName(String name)
{
this.name = name;
}
public void info()
{
System.out.println("此人的名为" + name);
}
}
|
4.建立一个测试的类
代码如下 |
复制代码 |
import org.Rudiment.spring.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest
{
/**
* @param args
*/
public static void main(String[] args)
{
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
PersonService p = ac.getBean("personService", PersonService.class);
p.info();
}
}
|
5.建立Spring的配置文件(applicationContext.xml),这个文件放在classes下面,如果在myeclipse中做开发,那直接放在src下面(IDE会帮我们移到classes下面)
代码如下 |
复制代码 |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
|
至此我们的spring就算配置完成了。
有一个比较难弄的就是。spring 框架中貌似没有提供配置文件的模版,所以我这个配置文档也是QQ好友发给我,然后修改的 - -!