什么是spring框架?

来源:互联网 发布:液压系统计算软件 编辑:程序博客网 时间:2024/05/01 17:05

spring框架

     Spring Framework是轻量级容器,它可以使用struts和webwork等众多的web应用程序结合使用,也可以单独使用。

一、spring的特点:

     面向切面(aspect-oriented programming AOP),控制翻转(inversion of control IOC)

轻量:不依赖与其他的对象,单独使用

容器:xml中设置声明周期和配置

框架:系统中的对象通过xml文件配置组合起来的

二、Spring框架由7个部分组成

    工厂模式:一个类有好多的子类,类似于Singleton

    核心容器:包含beanFactory创建bean,实现ioc

    应用上下文模块:定制服务接口,扩展了BeanFactory,添加了对I18N(国际化)、系统生命周期事件及验证的支持,并提供许多企业级服务,如电子邮件服务。JNDI访问、EJB集成、远程调用及定时服务,并支持与模板框架的集成。

    Aop模块:对面向切面提供了丰富的支持,是spring应用系统开发切面的基础,并引入metadata编程

    Jdbc和dao模块

    O/r映射模块

    Web模块:建立在应用上下文模块的基础上,提供了合适web系统的上下文,另外,高模块支持多项面向web的任务,如透明处理都闻见上传请求,自动将请求参数绑定大语文对象中,等等。

Mvc模块

所有的模块都是建立中核心模块容器之上的,容器规定如何创建、配置和管理Bean,以及其它细节

三、体验spring(myeclipse)

1)、首先搭建开发环境

使用spring需要的jar文件

进http://www.springsource.org/download官网

一般情况下至少用到spring.jar和commons-logging.jar这两个jar包,将这两个包build path到项目中。

2)、创建java类

先创建一个接口

public interface GreetingService {

//定义接口中的方法

public void sayGreeting();

}

创建一个继承上面的那个接口的类

public class GreetingServiceImpl implements GreetingService {

//定义私有的变量,必须有该变量的set方法

private String say;

public void setSay(String say) {

this.say = say;

}

//重写父类的方法

@Override

public void sayGreeting() {

// TODO Auto-generated method stub

System.out.println("我说的是:"+say);

}

}

再创建一个继承上面的接口的类

public class greetingServiceImpl2 implements GreetingService{

//定义自己的私有变量,与上面那个类不同的是,这个变量是一个类的对象,同样也必须有set方法

private GreetingServiceImpl gsi;

public void setGsi(GreetingServiceImpl gsi) {

this.gsi = gsi;

}

//重写父类的方法

@Override

public void sayGreeting() {

// TODO Auto-generated method stub

gsi.sayGreeting();

}

}

     3)、再在xml中配置,xml中的bean就是java类

<?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">

<!--第一个类,id是xml中唯一的标识,class是该类的路径  -->

<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">

<!-- 设置第一个类中的属性的值 -->

<property name="say" value="hello"></property>

</bean>

<!-- 设置第二类 -->

<bean id="greetingServiceImpl2" class="cn.csdn.service.greetingServiceImpl2">

<!-- 设置第二个类的属性,因为它是类的对象,要用的ref -->

<property name="gsi" ref="greetingServiceImpl"></property>

</bean>

</beans>

     4)、建立junit类测试

public class TestSpring {

@Test

public void test1() {

// 第一步:解析applicationContext.xml文件

ApplicationContext ac = new ClassPathXmlApplicationContext(

"applicationContext.xml");

// 第二步:调用getBean方法获取bean对象,要强制类型转换

// 先测试第一个类

GreetingServiceImpl greeting = (GreetingServiceImpl) ac

.getBean("greetingServiceImpl");

// 第三步:调用对象的响应的方法

greeting.sayGreeting();

// 输出分界线

System.out.println("=======================");

// 测试第二个类

greetingServiceImpl2 gsi2 = (greetingServiceImpl2) ac

.getBean("greetingServiceImpl2");

// 调用该类的方法

gsi2.sayGreeting();

}

}

   

 

原创粉丝点击