Spring是分层的JavaSE/EE full-stack(一站式) 轻量级开源框架

  • 分层:
    • SUN提供的EE的三层结构:web层、业务层、数据访问层(持久层,集成层)
    • Struts2是web层基于MVC设计模式框架.
    • Hibernate是持久的一个ORM的框架.
  • 一站式:
    • Spring框架有对三层的每层解决方案:
    • web层:Spring MVC.
    • 持久层:JDBC Template
    • 业务层:Spring的Bean管理

Spring的核心

  • IOC:(Inverse of Control 反转控制) 控制反转:将对象的创建权,交由Spring完成.
  • AOP:Aspect Oriented Programming 是 面向对象的功能延伸.不是替换面向对象,是用来解决OO中一些问题.

IOC:控制反转.


Spring优点

  • 方便解耦,简化开发
    • Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理
  • AOP编程的支持
    • Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
  • 声明式事务的支持
    • 只需要通过配置就可以完成对事务的管理,而无需手动编程
  • 方便程序的测试
    • Spring对Junit4支持,可以通过注解方便的测试Spring程序
  • 方便集成各种优秀框架
    • Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
  • 降低JavaEE API的使用难度
    • Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

Spring底层的IOC的原理图


IOC和DI(*****)区别?

IOC:控制反转:将对象的创建权,由Spring管理. DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中

  • 面向对象中对象之间的关系;
    • 依赖: public class A{ private B b; }
    • 继承:is a
    • 聚合:
      • 聚集:
      • 组合:

Spring框架加载配置文件

ApplicationContext 应用上下文,加载Spring 框架配置文件

  • 加载classpath: new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件.
  • 加载磁盘路径: new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件.

BeanFactory与ApplicationContext区别?

ApplicationContext类继承了BeanFactory BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类 ApplicationContext类加载配置文件的时候,创建所有的类 ApplicationContext对BeanFactory提供了扩展

  • 国际化处理
  • 事件传递
  • Bean自动装配
  • 各种不同应用层的Context实现
  • 早期开发使用BeanFactory.

MyEclipse配置XML提示

Window--->xml catalog--->add 找到schema的位置 ,将复制的路径 copy指定位置,选择schema location.

Spring的入门的程序

  • 下载Spring的开发包

https://repo.spring.io/release/org/springframework/spring/

  • spring-framework-3.2.0.RELEASE-dist.zip ---Spring开发包
    • docs :spring框架api和规范
    • libs :spring开发的jar包
    • schema :XML的约束文档.
  • spring-framework-3.0.2.RELEASE-dependencies.zip ---Spring开发中的依赖包

开始我们第一个spring的程序

  1. 创建web工程引入相应jar包:
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
开发的日志记录的包:
com.springsource.org.apache.commons.logging-1.1.1.jar		--- 用于整合其他的日志的包(类似Hibernate中slf4j)
com.springsource.org.apache.log4j-1.2.15.jar

2.创建Spring的配置文件

在src下创建一个applicationContext.xml
引入XML的约束:
找到xsd-config.html.(*****)引入beans约束:
<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">

3.在配置中配置类

<!--通过bean标签 设置类的信息,id为类取个标识 -->
	<bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
		<!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
		<property name="info" value="依赖注入" />
	</bean>
	
	
	
	完整的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"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!--通过bean标签 设置类的信息,id为类起个标识 -->
	<bean id="userService" class="cn.spring.demo1.HelloServiceimpl">
		<!-- 使用<property>标签注入属性 values是普通值 ref是对象-->
		<property name="info" value="依赖注入" />
	</bean>
</beans>
  1. HelloService.java
package cn.spring.demo1;

public interface HelloService {
	public void sayHello();
}

5.HelloServiceimpl..java 实现类

package cn.spring.demo1;

/**
 * @author NOP
 *
 */
public class HelloServiceimpl implements HelloService {
	private String info;

	public void setInfo(String info) {
		this.info = info;
	}

	public void sayHello() {
		// TODO Auto-generated method stub
		System.out.print("hello spring..."+info);
	}

}

  1. 编写测试类SpringTest1.java
package cn.spring.demo1;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class SpringTest1 {
	@Test
	// 传统方式
	public void demo1() {
		// 造成程序紧密耦合
		HelloService helloservice = new HelloServiceimpl();
		helloservice.sayHello();
	}
	
		//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
	@Test
	public void demo2() {
		//创建一个工厂类
		//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");
		ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
				"applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml
		
		HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
		helloservice.sayHello();
	}
	
	//加载磁盘路径:  new FileSystemXmlApplicationContext("applicationContext.xml");
	@Test
	public void demo3( ) {
		//创建一个工厂类
		//加载磁盘路径:  new FileSystemXmlApplicationContext("applicationContext.xml");
		ApplicationContext applicationcontext = new FileSystemXmlApplicationContext(
				"applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xml
		
		HelloService helloservice = (HelloService)applicationcontext.getBean("userService");
		helloservice.sayHello();
	}
	
	//BeanFactory模式
	@Test
	public void demo4(){
		//ClassPathResource
		//BeanFactory beanFactory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); :加载classpath下面配置文件.
		BeanFactory beanFactory= new XmlBeanFactory(new FileSystemResource("applicationContext.xml"));//加载磁盘下的配置文件
		HelloService helloservice = (HelloService)beanFactory.getBean("userService");
		helloservice.sayHello();
	}
}