Spring IOC
2016-02-24 16:10
501 查看
IOC 中文名字叫做控制反转,也叫作也有人称作依赖注入。
spring 会根据开发者的配置,对这些对象创建。将这些对象之间依赖进行绑定。
下面这个例子是对一个Person 的Car 对象进行set 注入。
Car.java
Person.java
applicationContext.xml
Person.java
applicationContext.xml
CarFactory.java
Person.java
applicationContext.xml
CarFactory.java
Person.java
applicationContext.xml
spring 会根据开发者的配置,对这些对象创建。将这些对象之间依赖进行绑定。
ioc 的优点
ioc 的优点就是减低了对象之间对象的耦合度。对象之间的依赖不在是代码内部,而在配置文件里面。或者注解里面。缺点
个人比较讨厌xml,所以觉得xml 配置是一个缺点,但是可以有注解替代。还有就是ioc 的对象创建的技术是反射机制。所以创建对象时会比平常更消耗内存。但是这点跟ioc 的优点比起来显得微不足道。ioc 的几种注入方式
ioc 有4 种依赖注入,分别是构造注入、set注入、静态工厂、实例工厂,比较长用的是前两个。set 注入
set 注入就是最一个类里面的私有属性进行初始化。下面这个例子是对一个Person 的Car 对象进行set 注入。
Car.java
/*车类 颜色 品牌 get set 方法 */ package cn.met0.maven.spring.simple; public class Car { private String color; private String brand; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
Person.java
/* 人类 车子 开车方法 设置车方法 */ package cn.met0.maven.spring.simple; public class Person { private Car car; public void drive() { System.out.println("我开着" + car.getColor() + "的" + car.getBrand()); } public void setCar(Car car) { this.car = car; } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="car" class="cn.met0.maven.spring.simple.Car"> <property name="brand" value="劳斯莱斯"></property> <property name="color" value="黑色"></property> </bean> <bean id="person" class="cn.met0.maven.spring.simple.Person"> <property name="car" ref="car"></property> </bean> </beans>
构造注入
Car.java/*车类 颜色 品牌 get set 方法 */ package cn.met0.maven.spring.simple; public class Car { private String color; private String brand; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
Person.java
/* 人类 车子 方法初始化构造方法 开车方法 */ package cn.met0.maven.spring.simple; public class Person { private Car car; public Person(Car car){ this.car = car; } public void drive() { System.out.println("我开着" + car.getColor() + "的" + car.getBrand()); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="car" class="cn.met0.maven.spring.simple.Car"> <property name="brand" value="劳斯莱斯"></property> <property name="color" value="黑色"></property> </bean> <bean id="person" class="cn.met0.maven.spring.simple.Person"> <constructor-arg name="car" ref="car"></constructor-arg> </bean> </beans>
静态工厂注入
Car.java/*车类 颜色 品牌 get set 方法 */ package cn.met0.maven.spring.simple; public class Car { private String color; private String brand; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
CarFactory.java
package cn.met0.maven.spring.simple; /* 车子静态工厂类 */ public class CarFactory { public static Car getBWMCar(){ Car car = new Car(); car.setColor("红色"); car.setBrand("宝马"); return car; } }
Person.java
/* 人类 车子 开车方法 车子set 方法 */ package cn.met0.maven.spring.simple; public class Person { private Car car; public void drive() { System.out.println("我开着" + car.getColor() + "的" + car.getBrand()); } public void setCar(Car car) { this.car = car; } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="bwmCard" class="cn.met0.maven.spring.simple.CarFactory" factory-method="getBWMCar"> </bean> <bean id="person" class="cn.met0.maven.spring.simple.Person"> <property name="car" ref="bwmCard"></property> </bean> </beans>
实例工厂注入
Car.java/*车类 颜色 品牌 get set 方法 */ package cn.met0.maven.spring.simple; public class Car { private String color; private String brand; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } }
CarFactory.java
package cn.met0.maven.spring.simple; /* 车子工厂类 */ public class CarFactory { public Car getBWMCar(){ Car car = new Car(); car.setColor("红色"); car.setBrand("宝马"); return car; } }
Person.java
/* 人类 车子 开车方法 车子set 方法 */ package cn.met0.maven.spring.simple; public class Person { private Car car; public void drive() { System.out.println("我开着" + car.getColor() + "的" + car.getBrand()); } public void setCar(Car car) { this.car = car; } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:cache="http://www.springframework.org/schema/cache" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd "> <bean id="carFactory" class="cn.met0.maven.spring.simple.CarFactory"></bean> <bean id="bwmCard" factory-bean="carFactory" factory-method="getBWMCar"> </bean> <bean id="person" class="cn.met0.maven.spring.simple.Person"> <property name="car" ref="bwmCard"></property> </bean> </beans>
相关文章推荐
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- 模拟Spring的简单实现
- spring+html5实现安全传输随机数字密码键盘
- Spring中属性注入详解
- SpringMVC框架下JQuery传递并解析Json格式的数据是如何实现的
- struts2 spring整合fieldError问题
- spring的jdbctemplate的crud的基类dao
- 读取spring配置文件的方法(spring读取资源文件)
- Spring Bean基本管理实例详解
- java实现简单美女拼图游戏
- 详解Java的Spring框架中的事务管理方式
- 解析Java的Spring框架的BeanPostProcessor发布处理器
- Java开发框架spring实现自定义缓存标签
- java基本教程之线程休眠 java多线程教程
- JSP开发中在spring mvc项目中实现登录账号单浏览器登录
- spring boot实战之内嵌容器tomcat配置