spring ioc浅谈

搞清楚3个问题:1 什么是ioc?2 ioc的优点?3 spring ioc在工程中的具体实现?
1什么是ioc
ioc,控制翻转的意思,说白了就是在代码里不用想以前那样去new一个对象了,而是把控制权交给第三方的ioc容器去帮我们创建对象。
2ioc的优点
ioc的优点就是降低了代码的耦合度。
3spring ioc在工程中的具体实现
spring ioc是spring的核心和基础,spring ioc容器实际上就是BeanFactory,所以它应该具备BeanFactory的基本特征,BeanFactory一共有5个基本方法,了解下就可以了,比如,getbean(String beanname),通过bean的名称获取bean的实例.Spring将bean注入到ioc容器是通过xml配置文件去配置的,注入主要通过两种方式:set注入和构造器注入.还可以注入List,map等集合对象。注入是动态注入的,所以注入的过程是DI依赖注入的过程,主要是通过java的反射机制去实现的。注入bean到Spring的配置文件后,通过ClassPathXmlApplicationContext去获取ApplicationContext资源文件,也就获取是一个ioc的实例。再通过ioc的实例去获取具体的bean.
java代码:
先创建具体的bean.
package com.hjy.beans;

public interface Rest {
public void eat();

public void sleep();

}
package com.hjy.beans;

public class Arest implements Rest {
private Rest a;
private String name;
private String age;

public void eat() {
// TODO Auto-generated method stub
System.out.println("A每天吃3两");

}

public void sleep() {
// TODO Auto-generated method stub
System.out.println("A每天睡8个小时");
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public Rest getA() {
return a;
}

public void setA(Rest a) {
this.a = a;
}

}
package com.hjy.beans;

public class Brest implements Rest {
private Rest b;
private String name;
private String age;

public void eat() {
// TODO Auto-generated method stub
System.out.println("B每天吃4两");

}

public void sleep() {
// TODO Auto-generated method stub
System.out.println("B每天睡6小时");

}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public Rest getB() {
return b;
}

public void setB(Rest b) {
this.b = b;
}

}


通过spring的xml配置文件,将bean注入到spring ioc容器中.这里只实现了最常用的set注入方式,还有构造器注入方式以及list,map等集合类的注入并没有给出。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="Arest" class="com.hjy.beans.Arest">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
</bean>

<bean id="Brest" class="com.hjy.beans.Brest">
<property name="name" value="李四"></property>
<property name="age" value="25"></property>
</bean>

</beans>

在具体的资源文件里获取具体的bean.
package com.hjy.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.hjy.beans.Arest;
import com.hjy.beans.Brest;
import com.hjy.beans.Rest;

public class Testioc {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
Arest arest=(Arest)ac.getBean("Arest");
System.out.println(arest.getName());
arest.eat();
Brest brest=(Brest)ac.getBean("Brest");
System.out.println(brest.getName());
brest.eat();






}

}


张三
A每天吃3两
李四
B每天吃4两
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值