搭建环境

1、创建普通的Java工程

2、添加相应的jar包,下载链接:https://files.cnblogs.com/files/AmyZheng/lib.rar,此外,为了打印信息,我们还需要一个Apache Commons Logging API,在这里下载commons-logging-1.2

3、jar包

  • Spring核心必须依赖的库:commons-logging-1.1.1.jar
  • Spring IoC部分核心库:

    spring-beans-4.3.9.RELEASE.jar
    spring-context-4.3.9.RELEASE.jar
    spring-context-support-4.3.9.RELEASE.jar
    spring-core-4.3.9.RELEASE.jar
            spring-expression-4.3.9.RELEASE.jar
            spring-web-4.3.9.RELEASE.jar      ------> 支持在Web环境中使用Spring IoC容器

  • Spring AOP部分核心库:

  spring-aop-4.3.9.RELEASE.jar
          spring-aspects-4.3.9.RELEASE.jar

  • Spring AOP需要依赖于aspectj库:

  aspectjrt.jar
          aspectjweaver.jar

第一个实例

1、新建一个配置文件,用于配置和管理所有的bean。

beans.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd">
  8.  
  9. <bean id="birthdate" class="java.util.Date" />
  10.  
  11. <util:map map-class="java.util.HashMap" id="map">
  12. <entry key="罗玉凤" value="30" />
  13. <entry key="罗玉龙" value="40" />
  14. </util:map>
  15.  
  16. </beans>

每一个bean的Id唯一

2、新建Java测试类

  1. package ecut.ioc.ex;
  2.  
  3. import java.util.Date;
  4. import java.util.Map;
  5.  
  6. import org.springframework.context.support.AbstractApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8.  
  9. public class Test {
  10.  
  11. public static void main(String[] args) {
  12. //classpath和classpath下的所有jar包
  13. System.out.println( System.getProperty( "java.class.path" ) );
  14. //configLocations为不定参数,classpath当前工程下的bin目录下D:\java_workspace\java\Spring\bin;
  15. String configLocations = "classpath:ecut/**/ex/beans.xml" ;
  16. //String configLocations = "classpath:ecut/ioc/ex/beans.xml" ;
  17. //String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下
  18.  
  19. // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口
  20. // ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext
  21. AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
  22. //name 与xml中的bean标签的ID相对应
  23. Date date = context.getBean( "birthdate", Date.class );
  24.  
  25. System.out.println( date );
  26.  
  27. Map<?,?> map = context.getBean( "map" , Map.class );
  28.  
  29. System.out.println( map );
  30. //ApplicationContext没有close方法,AbstractApplicationContext
  31. context.close();
  32.  
  33. }
  34.  
  35. }
  1. package ecut.ioc.ex;
  2.  
  3. import java.util.Date;
  4. import java.util.Map;
  5.  
  6. import org.springframework.context.support.AbstractApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8.  
  9. public class Test {
  10.  
  11. public static void main(String[] args) {
  12. //classpath和classpath下的所有jar包
  13. System.out.println( System.getProperty( "java.class.path" ) );
  14. //configLocations为不定参数,classpath当前工程下的bin目录下D:\java_workspace\java\Spring\bin;
  15. String configLocations = "classpath:ecut/**/ex/beans.xml" ;
  16. //String configLocations = "classpath:ecut/ioc/ex/beans.xml" ;
  17. //String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下
  18.  
  19. // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口
  20. // ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext
  21. AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
  22. //name 与xml中的bean标签的ID相对应
  23. Date date = context.getBean( "birthdate", Date.class );
  24.  
  25. System.out.println( date );
  26.  
  27. Map<?,?> map = context.getBean( "map" , Map.class );
  28.  
  29. System.out.println( map );
  30. //ApplicationContext没有close方法,建议使用AbstractApplicationContext
  31. context.close();
  32.  
  33. }
  34.  
  35. }

因为ApplicationContext没有close方法,建议使用AbstractApplicationContext来创建一个spring 容器, 这个容器读取classpath(当前工程下的bin目录)下的配置文件,并由容器去创建相应的对象,最后提供getBean方法获取name(  与xml中的bean标签的Id相对应)所指定的对象。

转载请于明显处标明出处

https://www.cnblogs.com/AmyZheng/p/9243783.html

Spring学习(一)的更多相关文章

  1. spring 学习之 bean 的注入方式 property和constructor-arg的使用方式

    spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...

  2. Spring学习之AOP总结帖

    AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...

  3. Spring学习之第一个AOP程序

    IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...

  4. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  5. Spring学习 Ioc篇(一 )

    一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...

  6. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  7. Spring学习(二)——Spring中的AOP的初步理解[转]

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  8. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  9. Spring学习8-Spring事务管理

      http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html   Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...

  10. Spring学习之Ioc控制反转(1)

    开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...

随机推荐

  1. 其他 - 阻塞 & 同步 的基本认识

    1. 概述 有些概念, 老是弄不清楚 同步异步 阻塞非阻塞 2. 准备 场景 角色 client 发起请求 接受请求 server 接受请求 执行操作 返回响应 行为 大致是一个 C/S 模式的模型 ...

  2. FILES源代码

     FILESの源码 #include <bits/stdc++.h> #include <game.h> #define pause getchar(); #define c ...

  3. etc/hosts文件详解

    Linux 修改 etc/hosts文件 hosts文件 hosts —— the static table lookup for host name(主机名查询静态表). hosts文件是Linux ...

  4. IIS反向代理配置教程(最终完整版本)

    IIS代理配置教程 插件下载:https://download.csdn.net/download/song_yan_/11996489 一.安装反向代理插件 1.rewrite插件安装 (1) 双击 ...

  5. 对 Element UI table中数据进行二次处理

    (1)<el-table-column>标签加上 :formatter="dateFormat" <el-table-column prop="Star ...

  6. LVS的概念和重要性

    LVS: 概念:是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统 作用:举例 像有三个小区,但是工作的时间和休息的时间不一样,第一个是白天工作,一 ...

  7. LFTP命令笔记

    安装 因为在OpenWrt命令行下scp传输文件很慢(只有2.5MB/s不到), 于是改用FTP下载. lftp是OpenWrt下的FTP客户端软件. 如果固件中未安装的话, 需要自己安装, 其依赖于 ...

  8. A Kill Cord for your Laptop

    前言 昨晚在朋友圈看到国外一篇文章利用U盘锁笔记本电脑,刚好有一个坏的金士顿U盘,所以就折腾了一下. 准备 USB设备*1 Arch系统*1 走过的坑 因为systemd-udevd带起来的进程是ro ...

  9. DVWA实验之Brute Force(暴力破解)- High

    DVWA实验之Brute Force(暴力破解)- High   有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2046/p/10928380.ht ...

  10. h5页面判断移动端系统为Android或IOS

    最近遇到了一个需求,即所谓的 app+web 混合开发,需要将 h5 内嵌到 APP 中,这个时候因为要对 Android 和 IOS 有不同的处理逻辑,所以我们就需要判断一下,移动端的系统到时是哪一 ...