Spring是一个基于IOC和AOP的结构J2EE系统的框架 
IOC 反转控制 是Spring的基础,Inversion Of Control 
简单说就是创建对象由以前的程序员自己new 构造方法来调用,变成了交由Spring创建对象 
DI 依赖注入 Dependency Inject. 简单地说就是拿到的对象的属性,已经被注入好相关值了,直接使用即可。

目标:用Spring获取一个对象,并打印其name。

第一步:新建项目(java project类型)

第二步:下载lib.rar,并解压到 Spring/lib 目录下

第三步:导入包

把jar包导入到项目中,导包办法:右键 project->properties->java build path->libaries->add external jars

第四步:创建Category,用来演示IOC和DI

 package com.spring.cate;

 public class Category {
private int id;
private String name; public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

第五步:在src目录下新建applicationContext.xml文件
applicationContext.xml是Spring的核心配置文件,通过关键字category即可获取Category对象,该对象获取的时候,即被注入了字符串"category 333“到name属性

 <?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="category" class="com.spring.cate.Category">
<property name="name" value="category 3333" />
</bean> </beans>

第六步:测试

测试代码,演示通过spring获取Category对象,以及该对象被注入的name属性。
如图所示,可以打印出通过Spring拿到的Category对象的name属性

 package com.spring.test;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.cate.Category; public class TestSpring { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
Category c = (Category) context.getBean("category");
System.out.println(c.getName());
} }

总结:

对象的生命周期由Spring来管理,直接从Spring那里去获取一个对象。 IOC是反转控制 (Inversion Of Control)的缩写,就像控制权从本来在自己手里,交给了Spring。

不需要自己再去new对象,直接由Spring生产。对象的属性通过在applicationContext.xml中以注入的方式传递给对象。

最新文章

  1. 关于Unity3D自定义编辑器的学习
  2. ppt - 常规策划
  3. selenium python 安装
  4. pc端页面在移动端显示问题
  5. Java xml object 互转
  6. xming + putty 搭建远程图形化ssh访问ubuntu 14.04
  7. 做一些Spring AOP做过的事,封装 jdk动态代理成为一个黑盒子
  8. Java中IO流中所涉及到的各类方法介绍
  9. Dragons
  10. 代码编写横屏的UIView
  11. &lt;转载&gt;僵尸进程
  12. PreparedStatement与Statement
  13. CSS BFC(Block Formatting Context)
  14. Spring事务配置的五种方式和spring里面事务的传播属性和事务隔离级别
  15. RunLoop已入门?赶紧来应用一下
  16. Hadoop入门
  17. python tuple的函数
  18. Html 页面载入内容前,显示 loading 效果。
  19. 高级OPENGL, 利用uniform块接口
  20. opencv: Rotate image by 90, 180 or 270 degrees

热门文章

  1. python django的ManyToMany简述
  2. Connect Appium Server Fail.A new session could not be created
  3. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)
  4. 使用JavaScript实现一个俄罗斯方块
  5. Python内置函数(15)——memoryview
  6. Linux将端口设置进防火墙的白名单
  7. python多进程--------linux系统中python的os.fork()方法
  8. Java-Maven(二):Maven常用命令
  9. CWMP开源代码研究——cwmp移植
  10. re模块中的compile函数