`

Spring初体验(一)

阅读更多


SpringFramework(简称Spring)是J2EE应用程序框架,不过,更严格的讲它是针对Bean的生命周期进行管理的轻量级容器(Lightweight  container),可以单独利用Spring构筑应用程序,也可以和StrutsWebwork等众多Web应用程序框架组合使用,并且可以与Swing等桌面应用程序API组合。所以Spring并不仅仅只能应用在J2EE中,也可以应用在桌面应用及小应用程序中。针对Spring开发的组件不需要任何外部库。


Spring是一个轻量级的IoCAOP容器框架。

  • 轻量级:从大小及系统开支上说。且Spring是非侵入式的(基于Spring开发的系统中对象一般不依赖于Spring的类)
  •  反向控制:使用IoC对象是被动接受依赖类而不是主动去找(容器在实例化对象时主动将其依赖类注入给它)
  • 面向切面:将业务逻辑从系统服务中分离,实现内聚开发。系统对象只做该做的业务逻辑不负责其他系统问题(如日志和事务支持)
  • 容器:包含且管理系统对象的生命周期和配置,通过配置设定Bean是单一实例还是每次请求产生一个实例,并设定Bean之间的关联关系
  • 框架:使用简单组件配置组合成一个复杂的系统,系统中的对象是通过XML文件配置组合起来的,且Spring提供了很多基础功能(事务管理、持久层集成等)

Spring框架用7个模块组成

  • 核心容器:BeanFactory(spring框架的核心采用工厂模式实现IOC)
  • 应用上下文模块:扩展了BeanFactory
  • AOP模块:对面向切面提供了丰富的支持,是Spring应用系统开发切面的基础,并引入了metodata编程
  • JDBCDAO模块
  • O/R映射模块
  • Web模块
  • MVC框架
案例:应用简单的spring框架:

1、在建好项目之后我们首先要引入spring的类库文件(类库文件需要在官方网站中载:http://www.springsource.org/download

需要在下载的文件中找到以下两个jar包,并导入到我们建立好的项中(这里使用2.5.6举例)

spring-framework-2.5.6 \spring-framework-2.5.6\dist----àspring.jar
spring-framework-2.5.6 \spring-framework-2.5.6\lib\jakarta-commons----à commons-logging.jar

      2、创建接口ServiceDao,代码如下:

package com.zd.dao;

public interface ServiceDao {
	
	void sayHello();
}
   
  创建接口实现类ServiceDaoimpl,代码如下:

package com.zd.dao;

public class ServiceDaoimpl implements ServiceDao {

	private String say;
	
	@Override
	public void sayHello() {
		System.out.println("我想说的是:"+say);
	}
	
	public void setSay(String say) {
		this.say = say;
	}
}
 
解析:此接口实现类实现了ServiceDao中的方法,并且必需要为变量say添加一个set方法,因为Spring框架是通过set方法为变量注入值的。

      3、创建一个Spring的配置文件 applicationContext.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"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>

<bean id="servicedao" class="com.zd.dao.ServiceDaoimpl">
	<property name="say" value="你好!"/>
</bean>

</beans>
 

解析:bean标签中,id属性为所指java类的别名,class属性为指定的Java类的路径。

property标签中,name属性为在指定Java类中定义的变量名称,value属性为变量的值。

注意:Spring的配置文件名字与Struts、Hibernate等框架的配置有所区别,Spring配置文件名字可以更改,而后两者不可更改。


4、编写测试类TestSpring,代码如下:



public class TestSpring {
	@Test
	public void test1(){
		
		/**加载Spring容器,可以解析多个配置文件,采用输入数组方式传递ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"})*/
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		/**通过xml文件解析bean文件,getBean()方法中的参数为在xml文件中指定的java类的id值*/
		ServiceDaoimpl sd = (ServiceDaoimpl)ac.getBean("servicedao");
		sd.sayHello();
	}
}
	

 

运行结果:

我想说的是:你好!

 

 

案例2:应用简单的spring框架:

 

此案例还需案例1所有代码,需要在案例1中做出以下修改:

1、创建接口SpringService,代码如下:

 

 

package com.zd.service;

public interface SpringService {

	void say();
}
 

 

2、创建接口实现类SpringServiceImpl,代码如下:

 

 

package com.zd.service;

import com.zd.dao.ServiceDaoimpl;

public class SpringServiceImpl implements SpringService {

        /**这里引用案例1中的Java类*/
	private ServiceDaoimpl serviceDaoImpl;
	@Override
	public void say() {
		serviceDaoImpl.sayHello();
	}
	
	public void setServiceDaoImpl(ServiceDaoimpl serviceDaoImpl) {
		this.serviceDaoImpl = serviceDaoImpl;
	}

}

 

 

解析:这里引用案例1中的java类,并且也同样要为其加入set方法。

 

3、修改案例1中配置文件 applicationContext.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"
		xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
>

<bean id="servicedao" class="com.zd.dao.ServiceDaoimpl">
	<property name="say" value="你好!"/>
</bean>

<bean id="springServiceImpl" class="com.zd.service.SpringServiceImpl">
	<property name="serviceDaoImpl" ref="servicedao"/>
</bean>	
</beans>
 

 

 解析:此时的配置文件中,新加入了一个bean标签,因为在SpringServiceImpl中引入了案例1中的类,所以property标签的class属性变为ref属性,其值为servicedao(案例1中ServiceDaoimpl类的id值)。


注意:ref属性中的值必须和需要引用的类的id值一样。

 

4、创建测试类TestSpring,代码如下:

 

 

public class TestSpring {
	
	@Test
	public void test2(){
               /**加载spring容器,这里同样可以使用数组加载多个配置文件*/
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		SpringServiceImpl ssi = (SpringServiceImpl) ac.getBean("springServiceImpl");
		ssi.say();
	}

 

运行结果:

我想说的是:你好!

 

 

----------------------------------------------------------------

以上属个人理解,若有不足,请各位高手指点,谢谢..

 

 

 

 

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics