基本流程:
一.新建工程,构建package包结构
二.导入spring的核心包和依赖包(或根据提示导入相应的包),测试的话就导入log4j和junit测试环境
三.编写spring上下文配置文件
1.配置spring上下文的环境是需要注意xsd的应用。
2.spring包含了很多功能模块,不同的功能模块需要使用不同的xsd。
3.spring核心工厂引入的xsd是:http://www.springframework.org/schema/beans/spring-beans.xsd
四.spring工厂初始化(实例化)
把需要的由spring bean工厂初始化的(实例化的)类交个spring进行管理。例如:在spring
配置文件中,配置一个bean关联类),<bean id="now" class="java.util.Date"></bean>
五.编写测试程序或在程序中使用spring实例化对象,spring实例化对象时,可以使用spring核心IOC/DT
1).实例化spring上下文环境
2).通过spring上下文context环境得到实例化对象
3).使用实例化对象进行业务操作
下面先配置环境
这是我键的包
加入要用到的包
在bean包里写一个类 这是测试用的一些属性(同时写构造方法,get和set方法)
public class Course {
private String courseId;
private String courseName;
private int courseScore;
private Date startDate;
//生产日志
private Logger logger=Logger.getLogger(Course.class);
在config包里创建一个.xml文件,用于配置
在.xml文件加入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
在写入
<!-- spring的配置环境 -->
<!-- spring的核心包含一个bean facotry工厂,它的作用可以实例化指定的类 -->
//class加入Date工具包
<bean id="now" class="java.util.Date"></bean>
这个时候配置文件写好后就在Test包,创建一个TestSpring测试类写入下面的代码
//生产日志
private static Logger logger=Logger.getLogger(TestSpring.class);
@Test
public void test1(){
//1.实例化spring环境
String configLocationString="config/application-context.xml";
ApplicationContext context=new ClassPathXmlApplicationContext(configLocationString);
//2.通过spring bean工厂实例化java对象
Date now=(Date)context.getBean("now");
//3.使用java对象进行业务操作
logger.info(now.toLocaleString()+" 学习了spring框架");
}
剩下的就是运行调试是否成功