您的位置:首页 > 编程语言 > Java开发

Spring IOC

2016-02-24 16:10 501 查看
IOC 中文名字叫做控制反转,也叫作也有人称作依赖注入。

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring