spring学习-1
spring框架的组件结构图

IOC(Inversion of Control):反转控制,思想是反转资源获取方向,传统的资源查找方向是组件向容器发起请求资源查找,作为回应,容器适时的返回资源,IOC则相反,容器主动将资源推送给它所管理的组件,组件所要做的就是选择一种合适的方式接受资源。
DI(Dependency Injection):依赖注入,(IOC另一种表述)组件以一些预先定义的方式来自容器的资源注入。
spring用到的jar包

1、创建Bean
public class HelloSpring {
private String name;
public void setName(String name) {
System.out.println("setname= " + name);
this.name = name;
}
public void Hello() {
System.out.println("hello" + name);
}
public HelloSpring() {
// TODO Auto-generated constructor stub
System.out.println("constructor start。。。。 ");
}
2、在根目录下创建New Spring Bean Definition file xml文件,通过属性方法注入
<!-- 配置beans class :Bean的全类名,通过反射的方式在IOC容器中创建Bean,所以Bean中必须要有无参数的构造器 id:标识容器中的Bean,id唯一 -->
<bean id="helloSpring" class="com.test.HelloSpring">
<!-- property 的name属性和beans的set方法相对应 -->
<property name="name" value="spring"></property>
</bean>
3、使用构造方法注入,在constructor-arg元素里面声明属性值
3.1 新建User Bean
public class User {
private String name;
private int age;
private double money;
private String no;
public User(String name, double money, String no) {
super();
this.name = name;
this.money = money;
this.no = no;
}
public User(String name, int age, String no) {
super();
this.name = name;
this.age = age;
this.no = no;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + ", money=" + money
+ ", no=" + no + "]";
}
}
3.2 通过构造方法注入
//使用构造器注入属性值,可以指定参数的位置和参数的类型,以区分重载的构造器 index:位置 type:类型
<bean id="user1" class="com.test.User">
<constructor-arg value="aa" index="0"></constructor-arg>
<constructor-arg value="10" index="1" type="double"></constructor-arg>
<constructor-arg value="123456" index="2"></constructor-arg>
</bean>
<bean id="user2" class="com.test.User">
<constructor-arg value="cc"></constructor-arg>
<constructor-arg value="10" type="int"></constructor-arg>
<constructor-arg value="78901" ></constructor-arg>
</bean>
4、创建spring IOC容器对象,并从IOC容器获取bean实例,然后调用Bean中的方法
//ApplicationContext IOC容器
//ClassPathXmlApplicationContext:是ApplicationContext接口实现类,该实现从类路径下来加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//利用id定位到IOC容器的bean
HelloSpring hello = (HelloSpring) ctx.getBean("helloSpring");
//利用类型获取,不推荐使用
//HelloSpring hello = ctx.getBean(HelloSpring.class);
// 调用类的方法
hello.Hello();
User use = (User) ctx.getBean("user1");
System.out.println(use);
use = (User) ctx.getBean("user2");
System.out.println(use);
5、运行效果
一月 17, 2017 10:38:52 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
constructor start。。。。
setname= spring
hellospring
User [name=aa, age=0, money=10.0, no=123456]
User [name=cc, age=10, money=0.0, no=78901]
此系列学习源码:传送门
spring学习-1的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- MyEclipse Spring 学习总结三 SpringMVC
MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...
- Spring学习 Ioc篇(一 )
一直以来忙于项目的开发,Spring虽然不用,一直想系统地学习一下,想看看它的源码,都没有时间,这段时间比较充裕,就索性先把Spring学习下,熟悉各个功能再去探究它内部的实现.就从Ioc篇开始学习. ...
- Spring学习(三)——Spring中的依赖注入的方式
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...
- Spring学习(二)——Spring中的AOP的初步理解[转]
[前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- Spring学习8-Spring事务管理
http://blog.sina.com.cn/s/blog_7ffb8dd501014e0f.html Spring学习8-Spring事务管理(注解式声明事务管理) 标签: spring注 ...
- Spring学习之Ioc控制反转(1)
开始之前: 1. 本博文为原创,转载请注明出处 2. 作者非计算机科班出身,如有错误,请多指正 ---------------------------------------------------- ...
随机推荐
- python 求下个月的最后一天
[1]根据当前月求上个月.下个月的最后一天 (1)求当前月最后一天 (2)求前一个月的最后一天 (3)求下一个月的最后一天 学习示例与应用实例,代码如下: #!/usr/bin/python3 #-* ...
- 2016 acm香港网络赛 B题. Boxes
原题网址:https://open.kattis.com/problems/boxes Boxes There are N boxes, indexed by a number from 1 to N ...
- Python的Django框架中的Context使用
Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...
- 手机touch事件
touchstart:触摸开始的时候触发 touchmove:手指在屏幕上滑动的时候触发 touchend:触摸结束的时候触发 而每个触摸事件都包括了三个触摸列表,每个列表里包含了对应的一系列触摸点( ...
- JS表单提交
测试一: function submit(){var form1=document.getElementById("form1")form1.action="/manag ...
- Delphi 64与32位的差异
Delphi 64与32位的差异 最近,Delphi推出了64位预览版本, 我做为一个忠实的Delphier, 看到这消息后,第一时间学习,并写下这个做为以后的参考资料. 相同点: 在Delphi ...
- 我的Android进阶之旅------>Android关于TextWatcher的初步了解
首先来看一下TextWatcher的源代码 package android.text; /** * When an object of a type is attached to an Editabl ...
- 代替print输出的PY调试库:PySnooper
PySnooper¶ Github:https://github.com/lotapp/PySnooper pip install pysnooper 使用:分析整个代码 @pysnooper.s ...
- centos 时区正确,时间不对
centos6.5 里面 时区是 Asia/Shanghai ,但是 时间还是不对,在网上收集了如下做法:好像恢复了~~ (主要过程是: 查看各种设置,然后设置时间,最后更新本机时间,最后保持与时间 ...
- 移动端web常见问题解决方案
meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速启动方式,可隐藏地址栏,仅针对i ...