spring框架的组件结构图



IOC(Inversion of Control):反转控制,思想是反转资源获取方向,传统的资源查找方向是组件向容器发起请求资源查找,作为回应,容器适时的返回资源,IOC则相反,容器主动将资源推送给它所管理的组件,组件所要做的就是选择一种合适的方式接受资源。

DI(Dependency Injection):依赖注入,(IOC另一种表述)组件以一些预先定义的方式来自容器的资源注入。

spring用到的jar包

1、创建Bean

  1. public class HelloSpring {
  2. private String name;
  3. public void setName(String name) {
  4. System.out.println("setname= " + name);
  5. this.name = name;
  6. }
  7. public void Hello() {
  8. System.out.println("hello" + name);
  9. }
  10. public HelloSpring() {
  11. // TODO Auto-generated constructor stub
  12. System.out.println("constructor start。。。。 ");
  13. }

2、在根目录下创建New Spring Bean Definition file xml文件,通过属性方法注入

  1. <!-- 配置beans class :Bean的全类名,通过反射的方式在IOC容器中创建Bean,所以Bean中必须要有无参数的构造器 id:标识容器中的Bean,id唯一 -->
  2. <bean id="helloSpring" class="com.test.HelloSpring">
  3. <!-- property 的name属性和beans的set方法相对应 -->
  4. <property name="name" value="spring"></property>
  5. </bean>

3、使用构造方法注入,在constructor-arg元素里面声明属性值

3.1 新建User Bean

  1. public class User {
  2. private String name;
  3. private int age;
  4. private double money;
  5. private String no;
  6. public User(String name, double money, String no) {
  7. super();
  8. this.name = name;
  9. this.money = money;
  10. this.no = no;
  11. }
  12. public User(String name, int age, String no) {
  13. super();
  14. this.name = name;
  15. this.age = age;
  16. this.no = no;
  17. }
  18. @Override
  19. public String toString() {
  20. return "User [name=" + name + ", age=" + age + ", money=" + money
  21. + ", no=" + no + "]";
  22. }
  23. }

3.2 通过构造方法注入

  1. //使用构造器注入属性值,可以指定参数的位置和参数的类型,以区分重载的构造器 index:位置 type:类型
  2. <bean id="user1" class="com.test.User">
  3. <constructor-arg value="aa" index="0"></constructor-arg>
  4. <constructor-arg value="10" index="1" type="double"></constructor-arg>
  5. <constructor-arg value="123456" index="2"></constructor-arg>
  6. </bean>
  7. <bean id="user2" class="com.test.User">
  8. <constructor-arg value="cc"></constructor-arg>
  9. <constructor-arg value="10" type="int"></constructor-arg>
  10. <constructor-arg value="78901" ></constructor-arg>
  11. </bean>

4、创建spring IOC容器对象,并从IOC容器获取bean实例,然后调用Bean中的方法

  1. //ApplicationContext IOC容器
  2. //ClassPathXmlApplicationContext:是ApplicationContext接口实现类,该实现从类路径下来加载配置文件
  3. ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
  4. //利用id定位到IOC容器的bean
  5. HelloSpring hello = (HelloSpring) ctx.getBean("helloSpring");
  6. //利用类型获取,不推荐使用
  7. //HelloSpring hello = ctx.getBean(HelloSpring.class);
  8. // 调用类的方法
  9. hello.Hello();
  10. User use = (User) ctx.getBean("user1");
  11. System.out.println(use);
  12. use = (User) ctx.getBean("user2");
  13. System.out.println(use);

5、运行效果

  1. 一月 17, 2017 10:38:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  2. 信息: Loading XML bean definitions from class path resource [applicationContext.xml]
  3. constructor start。。。。
  4. setname= spring
  5. hellospring
  6. User [name=aa, age=0, money=10.0, no=123456]
  7. User [name=cc, age=10, money=0.0, no=78901]

此系列学习源码:传送门

spring学习-1的更多相关文章

  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. phalcon builder 用法

    $rawSql = $builder->columns(["aa","bb"]) ->from("TableName") -&g ...

  2. Coursera machine learning 第二周 quiz 答案 Linear Regression with Multiple Variables

    https://www.coursera.org/learn/machine-learning/exam/7pytE/linear-regression-with-multiple-variables ...

  3. python 基础 9.9 查询数据

      #/usr/bin/python #-*- coding:utf-8 -*- #@Time   :2017/11/24 4:21 #@Auther :liuzhenchuan #@File   : ...

  4. lua例子(进出栈)

    #include <stdio.h> extern "C" { #include "lua-5.2.2/src/lauxlib.h" #includ ...

  5. Hadoop DataNode 节点的动态添加和动态删除

    动态添加 DataNode 节点 hadoop环境是必须的 需要加入新的 DataNode 节点,前提是已经配置好 SSH 无密登录:直接复制已有DataNode中.ssh目录中的authorized ...

  6. elasticsearch从入门到出门-02-简单的CRUD

    操作背景: 电商网站上面的一个商品的增删改查: es 能接受的都是JSON格式的数据 Es 提供了一套简单的集群信息健康监控的api GET /_cat/health?v   epoch      t ...

  7. 1.设计模式-------Iterator

    本文主要是参考<图解设计模式>写的读书笔记: 开发中我用到遍历集合时候,无非我常用的就是简单的for循环,foreach,iterator 这三种方式进行遍历! 当然这三种的效率: 学习I ...

  8. TCP协议要点和难点全解

    转载自http://www.cnblogs.com/leetieniu2014/p/5771324.html TCP协议要点和难点全解 说明: 1).本文以TCP的发展历程解析容易引起混淆,误会的方方 ...

  9. Webpack探索【4】--- entry和output详解

    本文主要讲entry和output相关内容.

  10. androidAndroid开发学习--Ionic+Cordova 环境搭建

    我们看 Ionic 能给我们提供什么?  一个样式库,你可以使用它 来 装饰你的 HTML 网页 ,看起来 想 移动程序的 界面,什么 header .content.footer.grid.list ...