Spring的入门的程序

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

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 --- 用于整合其他的日志的包(类似Hibernate中slf4j)
  • com.springsource.org.apache.log4j-1.2.15.jar

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">

4 在配置中配置类:

5创建测试类:

@Test
// Spring开发
public void demo2() {
    // 创建一个工厂类.
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
            "applicationContext.xml");
    HelloService helloService = (HelloService) applicationContext.getBean("userService");
    helloService.sayHello();
}

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

  • IOC:控制反转:将对象的创建权,由Spring管理.
  • DI:依赖注入:在Spring创建对象的过程中,把对象依赖的属性注入到类中.
  • 面向对象中对象之间的关系;
  • 依赖:
public class A{
private B b;
}
  • 继承:is a
  • 聚合:
  • 聚集:
  • 组合:

7 Spring框架加载配置文件:

  • ApplicationContext 应用上下文,加载Spring 框架配置文件
  • 加载classpath:
    new ClassPathXmlApplicationContext("applicationContext.xml"); :加载classpath下面配置文件.
  • 加载磁盘路径:
    new FileSystemXmlApplicationContext("applicationContext.xml"); :加载磁盘下配置文件.

8 BeanFactory与ApplicationContext区别?

  • ApplicationContext类继承了BeanFactory.
  • BeanFactory在使用到这个类的时候,getBean()方法的时候才会加载这个类.
  • ApplicationContext类加载配置文件的时候,创建所有的类.(在程序加载配置文件的过程中配置文件中需要实现的类就全部创建好了)
  • ApplicationContext对BeanFactory提供了扩展:
  • 国际化处理
  • 事件传递
  • Bean自动装配
  • 各种不同应用层的Context实现
    ***** 早期开发使用BeanFactory.现在基本都是用的是ApplicationContext

9 MyEclipse配置XML提示:

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

posted @ 2016-10-21 11:07  小坨男  阅读(463)  评论(0编辑  收藏  举报