zoukankan      html  css  js  c++  java
  • Spring框架的IOC核心功能快速入门

    2. 步骤一:下载Spring框架的开发包
        * 官网:http://spring.io/
        * 下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring解压:(Spring目录结构:)
            * docs      -- API和开发规范
            * libs      -- jar包和源码
            * schema    -- 约束
        * dist      -- 总包(包含docs、libs、schema)
          * dependencies   -- spring的依赖包
    3. 步骤二:创建JavaWEB项目,引入Spring的开发包
        * 引入Spring框架IOC核心功能需要的具体的jar包
            * Spring框架的IOC的功能,那么根据Spring框架的体系结构图能看到,只需要引入如下的jar包
                * Beans
                * Core
                * Context
                * Expression Language
      (在下载的spring开发总包中-->libs-->复制beans、context、core、expression四个包-->粘贴到创建的web工程的lib中)
            * 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-dependenciesorg.apache.log4jcom.springsource.org.apache.log4j1.2.15
                    * com.springsource.org.apache.log4j-1.2.15.jar
      还需要引入log4j.properties配置文件,将其复制到src目录下。

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

        public interface UserService {

          public void sayHello();
        }

        * UserServiceImpl -- 具体的实现类   

        public class UserServiceImpl implements UserService{

          @Override
          public void sayHello() {
            System.out.println("sayHello!");
            }
          }

    我们原来调用sayHello方法的方式是,创建UserServiceImpl对象,然后再调用sayHello方法。现在我们想要使用框架来不创建对象就可以调用方法,实现方法如下:

    5. 步骤四:想把UserServiceImpl实现类的创建交给Spring框架来管理,需要创建Spring框架的配置文件,完成配置
        * 在src目录下创建applicationContext.xml的配置文件,名称是可以任意的,但是一般都会使用默认名称!!
        * 引入spring的约束,需要先找到具体的约束头信息!!
            * spring-framework-3.2.0.RELEASEdocsspring-framework-referencehtmlxsd-config.html,在网页中找到第一个包含beans标签的大标签(大标签就是开头为<?xml version="1.0" encoding="UTF-8"?>),然后将下面的内容复制到我们的配置文件中。
            * 具体的约束如下:      
                <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.huida.demo1.UserServiceImpl"/>id是唯一的,class中放全路径
    
    6. 步骤五:编写测试程序,采用Spring框架的工厂方式来获取到UserService接口的具体实现类!!
        public void demo2(){
            // 使用Spring的工厂:
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//在xml加载的时候对象就已经创建了。
            // 通过工厂获得类:
            //UserServiceImpl usi = (UserServiceImpl) applicationContext.getBean("userService");
        //也可以使用接口实现
        UserService usi=(UserService)applicationContext.getBean("userService"); usi.sayHello(); }
     使用接口实现更加灵活,因为一个接口可以对应好几个实现类,我们在调用他们的方法时,只需要在配置文件中配置一下,然后更改一下他的id即可,而不需要再对每一个类实例化一个对象。

     

     
     
  • 相关阅读:
    love 玫瑰花
    正则表达式
    .NET Mvc
    html收藏
    winform问题集锦
    MSDE2000
    Oracle 语法
    PowerDesigner
    Oracle 操作
    文件转换(待完善)
  • 原文地址:https://www.cnblogs.com/wyhluckdog/p/10126174.html
Copyright © 2011-2022 走看看