Spring基础知识
首先,简单介绍spring中IoC.
其次,装配Bean,介绍如何利用IOC进行系统间的松耦合关联,如何利用XML在spring容器中定义系统对象,装配其依赖类。
一 Spring的特点:
1.轻量级 从大小及系统开支上说。且spring是非入侵式的。
2.反向控制 使用IoC对象是被动接受依赖类而不是主动去找。
3.面向切面 将业务逻辑从系统服务中分离,实现内聚开发。
4.容器 包含管理对象的生命周期和配置,通过配置设定Bean是单一的还是每次请求产生一个实例,并且设定Bean之间的关联关系。
5.框架 使用简单的组件配置一个复杂的系统。
二 Spring模块
Spring框架由7个模块组成:
① 核心容器:提供了基础功能。包含BeanFactory类
② 应用上下文模块:是 BeanFactory的扩展,添加了兑 II8N、系统生命周期事件及验证的支持,并提供很多企业级服务
③ AOP模块:对面向切面提供了丰富的支持,是Spring应用系统开发切面的基础,并引入了metadata编程
④ JDBC和DAO模块
⑤ O/R映射模块
⑥ Web模块
⑦ MVC框架
下面是Spring的一个简单实例
需要用到的包spring.jar/commons-logging.jar
代码:
package cn.csdn.dao;
public interface GreetingDao {
void say();
}
-----------------------------------------------------------------------------
package cn.csdn.dao;
public class GreetingDaoImpl implements GreetingDao{
/**私有属性*/
private String say;
@Override
//方法
public void say() {
System.out.println("负责打的招呼"+say);
}
//set方法赋值
public void setSay(String say) {
this.say = say;
}
}
------------------------------------------------------------------------------
package cn.csdn.service;
public interface GreetingService {
void say();
}
--------------------------------------------------------------------------------
package cn.csdn.service;
import cn.csdn.dao.GreetingDaoImpl;
public class GreetingServiceImpl implements GreetingService{
//真正的业务操作对象
private GreetingDaoImpl greetingDaoImpl;
@Override
public void say() {
greetingDaoImpl.say();
}
//赋值方法
public void setGreetingDaoImpl(GreetingDaoImpl greetingDaoImpl) {
this.greetingDaoImpl = greetingDaoImpl;
}
}
----------------------------------------------------------------------------
配置文件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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<!-- 创建一个DAO的bean id是唯一的标识 class指明类路径 property是bean的属性 -->
<bean id="greetingDaoImpl" class="cn.csdn.dao.GreetingDaoImpl">
<property name="say">
<value>hello</value>
</property>
</bean>
<!-- 创建一个Service的bean ref引用对象-->
<bean id="greetingServiceImpl" class="cn.csdn.service.GreetingServiceImpl">
<property name="greetingDaoImpl" ref="greetingDaoImpl">
</property>
</bean>
</beans>
----------------------------------------------------------------------------
测试一下吧
package cn.csdn.junit;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import cn.csdn.service.GreetingServiceImpl;
public class TestSpring {
@Test
public void test1(){
/**解析Spring配置文件*/
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
/**获取Bean*/
GreetingServiceImpl gsi=(GreetingServiceImpl) ac.getBean("greetingServiceImpl");
gsi.say();
}
}
结果是:负责打的招呼hello
希望今天的内容能帮到你,再见