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的开发包
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的程序
创建web工程引入相应jar包:
2.创建Spring的配置文件
在src下创建一个applicationContext.xml引入XML的约束:找到xsd-config.html.(*****)引入beans约束:
3.在配置中配置类
标签注入属性 values是普通值 ref是对象-->完整的applicationContext.xml
HelloService.java
packagecn.spring.demo1;publicinterfaceHelloService{publicvoidsayHello();}
5.HelloServiceimpl..java 实现类
packagecn.spring.demo1;/** *@authorNOP * */publicclassHelloServiceimplimplementsHelloService{privateString info;publicvoidsetInfo(String info){this.info = info; }publicvoidsayHello(){// TODO Auto-generated method stubSystem.out.print("hello spring..."+info); }}
编写测试类SpringTest1.java
packagecn.spring.demo1;importorg.junit.Test;importorg.springframework.beans.factory.BeanFactory;importorg.springframework.beans.factory.xml.XmlBeanFactory;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importorg.springframework.context.support.FileSystemXmlApplicationContext;importorg.springframework.core.io.ClassPathResource;importorg.springframework.core.io.FileSystemResource;publicclassSpringTest1{@Test// 传统方式publicvoiddemo1(){// 造成程序紧密耦合HelloService helloservice =newHelloServiceimpl(); helloservice.sayHello(); }//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");@Testpublicvoiddemo2(){//创建一个工厂类//加载classpath:new ClassPathXmlApplicationContext("applicationContext.xml");ApplicationContext applicationcontext =newClassPathXmlApplicationContext("applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xmlHelloService helloservice = (HelloService)applicationcontext.getBean("userService"); helloservice.sayHello(); }//加载磁盘路径: new FileSystemXmlApplicationContext("applicationContext.xml");@Testpublicvoiddemo3( ){//创建一个工厂类//加载磁盘路径: new FileSystemXmlApplicationContext("applicationContext.xml");ApplicationContext applicationcontext =newFileSystemXmlApplicationContext("applicationContext.xml");// 如果不写会去web-inf下去找applicationContext.xmlHelloService helloservice = (HelloService)applicationcontext.getBean("userService"); helloservice.sayHello(); }//BeanFactory模式@Testpublicvoiddemo4(){//ClassPathResource//BeanFactory beanFactory= new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); :加载classpath下面配置文件.BeanFactory beanFactory=newXmlBeanFactory(newFileSystemResource("applicationContext.xml"));//加载磁盘下的配置文件HelloService helloservice = (HelloService)beanFactory.getBean("userService"); helloservice.sayHello(); }}