IOC核心功能快速入门

1. 什么是IOC的功能?

* Inverse of Control,控制反转,将对象的创建权反转给Spring

* 使用IOC可以解决的程序耦合性高的问题

 

下图是传统的方式创建对象和使用spring管理对象的区别:

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

* 官网:http://spring.io/

* 下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring解压:(Spring目录结构:)

* docs                -- API和开发规范

* libs                -- jar包和源码

* schema        -- 约束

 

3. 步骤二:创建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.commons.logging-1.1.1.jar

 

*还需要引入log4j的jar包spring-framework-3.0.2.RELEASE-dependencies\org.apache.log4j\com.springsource.org.apache.log4j\1.2.15

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

 

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

* com.itcast.demo1

* UserService                        -- 接口

* UserServiceImpl                -- 具体的实现类

 

5. 步骤四:想把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="userService" class="com.itcast.demo1.UserServiceImpl"/>

 

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

public void demo2(){

// 使用Spring的工厂:

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

// 通过工厂获得类:

UserService userService = (UserService) applicationContext.getBean("userService");

userService.sayHello();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值