一、spring的IOC-就是怎样使用spring来创建对象

二、springDI(依赖注入)-就是怎样给属性赋值

- 通过set方式赋值
以下我们只需要记住两点的赋值,基本类型和引用类型的赋值
- 基本类型都可以用value来赋值(包装类型和string类型)


还可以这样写




2. 通过构造函数赋值
看看我们的person对象

这里面有两个构造函数
下面就是我们的配置文件,该怎样给person对象进行赋值呢?

总结:
上面是spring的IOC和DI,说白了神秘的spring就是获取对象和给对象中的属性进行赋值,接下来,我们正式的说神秘的面向接口编程,其实说白了上面的对象和赋值都是给AOP做铺垫的。。。
三、真正意义上的面向接口编程
可以在一个类中引入一个接口,而给接口赋值的工作交给spring容器来做,程序员只需要在配置文件做一些配置就可以了,这样在客户端做到了完全的面向接口编程。
是不是我在讲这句话的时候大家都是一头雾水啊。。当初我在接触面向接口编程的时候也是这样的,主不过,在研究生阶段里,学习了设计模式,让我对接口有了很好的了解和学习,这样我就对spring有很好的理解能力了。
下面一个例子来说明我们的面向接口AOP编程:

案例一
说明:
Document类是接口,DocumentManager类是我们要引入的Document接口的类,DocumentTest类是我们的测试类,下面的ExcelDocument、PDFDocument、WordDocument类分别是实现Document接口的类。
Document类
package cn.itcast.spring0909.document.spring;
public interface Document {
public void read();
public void write();
}
DocumentManager类
package cn.itcast.spring0909.document.spring;
/**
* 利用spring的ioc和di做到了完全的面向接口编程
*@author Administrator
*
*/
public class DocumentManager {
private Document document;
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public void read(){
this.document.read();
}
public void write(){
this.document.write();
}
}
ExcelDocument、PDFDocument、WordDocument类
package cn.itcast.spring0909.document.spring;
public class ExcelDocument implements Document{
@Override
public void read() {
// TODO Auto-generated method stub
System.out.println("excel read");
}
@Override
public void write() {
// TODO Auto-generated method stub
System.out.println("excel writer");
}
}
DocumentTest类
package cn.itcast.spring0909.document.spring;
import org.junit.Test;
import cn.itcast.spring0909.utils.SpringHelper;
public class DocumentTest extends SpringHelper{
static{
path = "cn/itcast/spring0909/document/spring/applicationContext.xml";
}
@Test
public void test(){
DocumentManager documentManager = (DocumentManager)context.getBean("documentManager");
documentManager.read();
}
}
配置文件applicationContext.xml
<?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">
<!-- 把documentManager,worddocument,exceldocument,pdfdocument -->
<bean id="documentManager" class="cn.itcast.spring0909.document.spring.DocumentManager">
<!--
document是一个接口
-->
<property name="document">
<!--
通过ref给接口赋值
-->
<ref bean="wordDocument"/>
</property>
</bean>
<bean id="wordDocument" class="cn.itcast.spring0909.document.spring.WordDocument"></bean>
<bean id="excelDocument" class="cn.itcast.spring0909.document.spring.ExcelDocument"></bean>
<bean id="pdfDocument" class="cn.itcast.spring0909.document.spring.PDFDocument"></bean>
</beans>
我们看见代码才知道真正意义上的面向接口编程
我们使用DocumentManager类引用接口Document,使用配置文件来获取DocumentManager对象以及给此对象的属性document来复制,这样我们在test中就没有见到使用wordDocument,仅仅见到DocumentManager,但是我们却得到了wordDocument,这就是所谓的AOP编程。
当然DocumentManager中我们亦可以对类进行引用,虽然说是面向接口来引用,当然对类来引用的话,也不会错误。
案例二
双重的面向接口编程-一般我们使用action去调用service,用service调用dao

作者:少帅