搭建环境

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

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd"> <bean id="birthdate" class="java.util.Date" /> <util:map map-class="java.util.HashMap" id="map">
<entry key="罗玉凤" value="30" />
<entry key="罗玉龙" value="40" />
</util:map> </beans>

每一个bean的Id唯一

2、新建Java测试类

package ecut.ioc.ex;

import java.util.Date;
import java.util.Map; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
//classpath和classpath下的所有jar包
System.out.println( System.getProperty( "java.class.path" ) );
//configLocations为不定参数,classpath当前工程下的bin目录下D:\java_workspace\java\Spring\bin;
String configLocations = "classpath:ecut/**/ex/beans.xml" ;
//String configLocations = "classpath:ecut/ioc/ex/beans.xml" ;
//String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下 // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口
// ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext
AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
//name 与xml中的bean标签的ID相对应
Date date = context.getBean( "birthdate", Date.class ); System.out.println( date ); Map<?,?> map = context.getBean( "map" , Map.class ); System.out.println( map );
//ApplicationContext没有close方法,AbstractApplicationContext
context.close(); } }
package ecut.ioc.ex;

import java.util.Date;
import java.util.Map; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) {
//classpath和classpath下的所有jar包
System.out.println( System.getProperty( "java.class.path" ) );
//configLocations为不定参数,classpath当前工程下的bin目录下D:\java_workspace\java\Spring\bin;
String configLocations = "classpath:ecut/**/ex/beans.xml" ;
//String configLocations = "classpath:ecut/ioc/ex/beans.xml" ;
//String configLocations = "classpath:beans.xml" ;//若配置文件在src目录底下 // AbstractApplicationContext 实现了 org.springframework.context.ApplicationContext 接口
// ClassPathXmlApplicationContext 继承了 org.springframework.context.support.AbstractApplicationContext
AbstractApplicationContext context = new ClassPathXmlApplicationContext(configLocations);
//name 与xml中的bean标签的ID相对应
Date date = context.getBean( "birthdate", Date.class ); System.out.println( date ); Map<?,?> map = context.getBean( "map" , Map.class ); System.out.println( map );
//ApplicationContext没有close方法,建议使用AbstractApplicationContext
context.close(); } }

因为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. 507,介绍一下标准的css盒子模型?低版本ie的盒子模型有什么不同的?

    有两种,IE盒子模型,另外是W3C盒子模型: 盒模型都包括:内容(content),填充(padding),边界(margin),边框(border): 区别:IE的content部分吧border和 ...

  2. 【PAT甲级】1109 Group Photo (25分)(模拟)

    题意: 输入两个整数N和K(N<=1e4,K<=10),分别表示人数和行数,接着输入N行每行包括学生的姓名(八位无空格字母且唯一)和身高([30,300]的整数).按照身高逆序,姓名字典序 ...

  3. DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL 未能在“xmlhttpRequest”上执行“open”:无效的URL。

    出现这个报错主要是baseurl:http://192.168.*.*/后面的(/)或是请求里面的url:/user/login中前面的(/)有一个漏掉了,导致合成的路径不完整,所以报错:无效的URL

  4. AxureRP 9安装、激活、汉化

    AxureRP安装 AxureRP激活 AxureRP汉化

  5. composer update 或者 composer install提示killed解决办法

    出现此原因大多因为缓存不足造成,在linux环境可增加缓存解决. free -mmkdir -p /var/_swap_cd /var/_swap_#Here, 1M * 2000 ~= 2GB of ...

  6. 猴博士4小时讲完C语言视频教程

    猴博士4小时讲完C语言视频教程,一共有9节课. 目录结构如下: 目录:/2020030-猴博士4小时讲完C语言 [1G] ┣━━1.C语言基本语句(上)(更多资源访问:www.jimeng365.cn ...

  7. Python整合pdf【新手必学】

    在下载课件时往往会分成很多个小的pdf,一个也就几页,想要整合成一整个大pdf,于是百度了一下,网上有很多在线的pdf整合器,但是由于这蛋疼的网速,流量还要花钱,还是想要本地搞. 说python是万能 ...

  8. SpringBoot-自动装载

    1,SpringBoot里面有内置的tomcat容器. 2,SpringBoot是基于已有的东西创建的新的东西.核心:(AutoConfiguration)自动装载,Starter,Actuator, ...

  9. MySQL导出数据到文件报错

    执行如下语句: mysql> select * from users into outfile "F:\Develop\MySQL57\Uploads\users.txt" ...

  10. buuctf——facebook1

    分为注册和登陆两个页面 据大佬wp登陆页面又盲注,但我没测试 直接注册后登陆 注册后发现,会显示输入的url 根据目录扫描,发现robots.txt和flag.php robots.txt有源码备份 ...