Spring 框架 详解 (二)

 Spring的入门的程序:

1.1.1 下载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开发中的依赖包

1.1.2 创建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 --- 用于整合其他的日志的包(类似Hibernateslf4j)

com.springsource.org.apache.log4j-1.2.15.jar

1.1.3 创建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">

 

1.1.4 在配置中配置类:

<bean id="userService" class="cn.itcast.spring3.demo1.HelloServiceImpl"></bean>

1.1.5 创建测试类:

@Test

// Spring开发

public void demo2() {

// 创建一个工厂类.

ApplicationContext applicationContext = new ClassPathXmlApplicationContext(

"applicationContext.xml");

HelloService helloService = (HelloService) applicationContext.getBean("userService");

helloService.sayHello();

}

1.1.6 IOCDI(*****)区别?

IOC:控制反转:将对象的创建权,Spring管理.

DI:依赖注入:Spring创建对象的过程中,把对象依赖的属性注入到类中.

面向对象中对象之间的关系;

依赖:

public class A{

private B b;

}

继承:is a

聚合:

聚集:

组合:

 

 

1.1.7 Spring框架加载配置文件:

 

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

 

加载classpath

 

     new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件.

 

加载磁盘路径:

 

     new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件.

 

 

1.1.1 BeanFactoryApplicationContext区别?

 

ApplicationContext类继承了BeanFactory.

 

BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类.

 

ApplicationContext类加载配置文件的时候,创建所有的类.

 

ApplicationContextBeanFactory提供了扩展:

 

国际化处理

 

事件传递

 

* Bean自动装配

 

各种不同应用层的Context实现

 

***** 早期开发使用BeanFactory.

 

 

 

 

原文地址:https://www.cnblogs.com/yuanfuqiang/p/5824661.html