Spring框架之快速入门

# IoC的概述
这里写图片描述

IoC的编写过程

这里写图片描述

IoC的功能

  • IoC——Inverse of Control ,控制反转,将对象的创建权反转给Spring
  • 使用IoC可以解决的程序耦合性高的问题

步骤一:下载Spring框架的开发包

步骤二:创建JavaWEB项目,引入Spring的开发包

  • 引入Spring框架IoC 核心功能需要的具体的jar包
    • Spring框架的IoC 的功能,那么根据Spring框架的体系结构图能看到,只㤇引入如下的jar包
      • Beans
      • Core
      • Context
      • Expression Language
    • Spring 框架也需要引入日志相关的jar包
    • 在spring-framework-3.0.2. RELEASE-dependencies/org.apache.commons/com.springsource.org.apache.commons.logging/1.1.1
    • com.springsource.org.apache.log4j-1.2.15.jar

步骤三:创建对应的包结构,编写Java的类,要注意:以后使用Spring框架做开发,都需要来编写接口与实现类。

  • com.xd.demo1
    • UserService———接口
    • UserServiceImpl——–具体的实现类

步骤四:想把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件,完成配置。

  • 在src目录下创建applicationContext.xml的配置文件,名称可以是任意的,但是一般都会使用默认名称。
  • 引入spring的约束,需要先找到具体的约束头信息
    • spring-framework-3.2.0 RELEASE\docs\spring-framework-reference\html\xsd-config.html
    • 具体的约束如下:
      • <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">
        </beans>
  • 完成UserService的配置
    • <!-- Spring的入门案例================ -->
      <bean id="userDao" class="cn.itcast.spring.demo1.UserDaoImpl"></bean>

步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类。

public void demo2(){
    //使用Spring的工厂
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //通过工厂获得类
    UserService userService =(UserService) applicationContext.getBean("userService");
    userService.sayHello();


}

猜你喜欢

转载自blog.csdn.net/u011301372/article/details/81347542
今日推荐