spring day01-go1
1.复制xml到container/ioc下
2.B类实现序列化接口,构造函数和f1函数
A类实现序列化接口,将B类作为其成员变量,且get/set方法,执行execute方法去调用b.f1()
3.修改xml,在bean A中添加<property name="b" ref="b1"/>
4.测试使用set注入方法实例化A,调用a的execute方法,如果不执行第3步,则报空指针异常。
5.若此时添加C类,实现序列化,构造函数以及f1函数,要求A在execute方法去调用c.f1(),则A中必须修改代码
故写一个接口Service,定义f1方法
6.B和C分别实现Service接口
7.将A中的成员变量改为接口类型private Service service,execute方法则调用service.f1(),这样,只需要修改xml中
<property name="service" ref="c1"/>即为execute方法则调用了c.f1()
<property name="service" ref="b1"/>即为execute方法则调用了b.f1()
此处考察依赖注入,即在一个类中需要调用其他类的方法,那么我们需呀在xml中使用ref属性指定我们引入的其他类。当然,在xml中必须提前声明这个被引入的类。

B.class:
package container.ioc;
public class B implements Service{
public B(){
System.out.println("B的无参构造器");
}
public void f1(){
System.out.println("B的f1方法");
}
}
A.class:
package container.ioc;
public class A {
private B b;
public A() {
System.out.println("A的无参构造器");
}
public B getB() {
return b;
}
public void setB(B b) {
System.out.println("A的setB方法...");
this.b = b;
}
public void execute(){
System.out.println("A的execute方法...");
b.f1();
//service.f1();
}
}
C.class:
package container.ioc;
public class C implements Service{
public C() {
System.out.println("C的无参构造器");
}
public void f1(){
System.out.println("C的f1方法");
}
}
xml核心代码:
<!-- 容器启动之后,会检查整个配置文件,
然后将a1,b1两个对象创建好,通过调用a1的set方法来建立其与b1的依赖关系 -->
<bean id="b1" class="container.ioc.B"/>
<bean id="c1" class="container.ioc.C"/>
<bean id="a1" class="container.ioc.A">
<!-- 容器会调用a1的setB方法 -->
<property name="b" ref="b1"/>
</bean>
要理解为什么使用service接口:
Service.java:
package container.ioc;
public interface Service {
public void f1();
}
TestCase代码:
package container.ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCase {
@Test
public void test1(){
String cfg = "container/ioc/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
A a = ac.getBean("a1",A.class);
a.execute();
}
}
运行如下:

若将xml中<property name="b" ref="b1"/>这行代码去掉,则报错,空指针异常。A这个类中的属性使用到了B类,因此必须在xml中声明A的属性和B这个bean的依赖关系。

将A中的属性B改为Service接口:
package container.ioc;
public class A {
//private B b;
//尽量使用接口类型
private Service service;
public A() {
System.out.println("A的无参构造器");
}
// public B getB() {
// return b;
// }
//
// public void setB(B b) {
// System.out.println("A的setB方法...");
// this.b = b;
// }
public Service getService() {
return service;
}
public void setService(Service service) {
this.service = service;
}
public void execute(){
System.out.println("A的execute方法...");
//b.f1();
service.f1();
}
}
xml中修改如下:
<!-- <property name="b" ref="b1"/> -->
<property name="service" ref="c1"/>
运行结果如下:

若将xml改为:
<!-- <property name="b" ref="b1"/> -->
<property name="service" ref="b1"/>

即为接口的应用。要熟练掌握。
spring day01-go1的更多相关文章
- Spring day01笔记
struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解] sprin ...
- Spring day01
1 实例化Spring容器 新建springday01项目1.F盘jar/Spring/first/五个jar包拷贝到lib下,复制xml文件到项目first包下2.First.java测试如何启动容 ...
- 开放源代码的设计层面框架Spring——day01
spring第一天 一.Spring概述 1.1spring概述 1.1.1spring介绍 Spring是分层的Jav ...
- spring框架入门day01
struts:web层,比较简单(ValueStack值栈,拦截器) hibernate:dao层,知识点杂 spring:service层,重要,讲多少用多少 --> [了解] spring ...
- Spring day02笔记
spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...
- 05_ssm基础(三)之Spring基础
11.spring入门引导 12.spring_HelloWord程序 实现步骤: 0.找到spring压缩包,并解压 1.拷贝jar包 2.添加主配置文件(官方文档约28页) 3.在测试中使用 13 ...
- spring框架入门day02
0. 绪论 在第二天的学习中的学习JdbcTemplate之前,想要下载最新版本的mysql server8.0.11 结果在将所有的东西配置好后,发现报错, · Establishing SSL c ...
- 05_ssm基础(六)之SpringMVC
36.springMVC之入门 1.springMVC简介 Spring MVC是基于MVC模式的一个Web框架,它解决WEB开发中常见的问题(参数接收.文件上传.表单验证.国际化.等等),而且使用简 ...
- [刘阳Java]_Spring相关配置介绍_第5讲
这一节我们介绍一下Spring框架的相关常用配置 Spring依赖注入的两种方式(构造方法注入和setter方式注入) p-namespace方式配置 properties属性文件配置方式 集合对象配 ...
- [刘阳Java]_Spring IOC程序代码如何编写_第3讲
第2讲我们介绍了Spring IOC的基本原理,这篇文章告诉大家Spring IOC程序代码是如何编写的,从而可以更好的理解IOC和DI的概念(所有的Java类的初始化工作扔给Spring框架,一个J ...
随机推荐
- Android.mk学习 笔记
感谢: 原创作品 转载请注明出处:http://www.cnblogs.com/langlang/ 作者email: dayhappyhappy@163.com LOCAL_PATH := $(cal ...
- (leetcode)Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- oracle入门-%的用法
vempno emp.empno%type; 例如上面的这句话,你的vempno就是你定义的变量,和面的那个emp是你数据库里面存在的表,他的表里面有意个empno字段,然后%type就是empno的 ...
- JS给swf传参数
不仅可以用flashvars ="name=12&age=23" 还可以在指定swf地址时传参数src="test.swf?name=12&age=23& ...
- UILabel 添加图片
//设置显示图片 NSMutableAttributedString * cellAttributeStr = [[NSMutableAttributedString alloc]initWithSt ...
- Java学习-013-文本文件读取实例源代码(两种数据返回格式)
此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 1.读取的文本文件内容以一维数组[LinkedList<String>]的形式返回,源代 ...
- Ioc-Autofac实现自动的注入
在开发过程中,最郁闷的莫过于当你新增一个Service时,你需要对该Service进行注册,有的是使用代码注入,有的是XML配置注入,不管是哪种类型的注入,经常会出现开发人员忘记注入的情况. 于是我试 ...
- js运算之比较大小
1.大于>和小于< var box = 3 > 2;//关系运算符大多返回的是一boolean值. alert(box); //true 1.2不同类型的数据比较 var box = ...
- 我就是认真:Linux SWAP 深度解读(必须收藏)
我就是认真:Linux SWAP 深度解读(必须收藏) http://mp.weixin.qq.com/s?__biz=MzA4Nzg5Nzc5OA==&mid=2651660097& ...
- Ubuntu 安装 Brother MFC7470D 驱动
Ubuntu 安装 Brother MFC7470D 驱动 办公室的打印机是 Brother MFC7470D ,在 Ubuntu 中安装打印机驱动时,发现没有这个型号的驱动.只有 MFC7450 的 ...