第2讲我们介绍了Spring IOC的基本原理,这篇文章告诉大家Spring IOC程序代码是如何编写的,从而可以更好的理解IOC和DI的概念(所有的Java类的初始化工作扔给Spring框架,一个Java类如果依赖其他Java类,则此类依赖另一个Java类的初始化工作也扔给Spring框架来完成

下面我们通过在MVC开发中业务逻辑层和Dao层之间的关系来讲解利用Spring IOC和DI来实现依赖注入的关系

1. 创建一个业务逻辑层的类(先声明一下这里的业务逻辑层我们就没有再去定义接口,所以业务逻辑层的实现类就是一个普通的Java类),PetServiceImpl。此类需要依赖于两个Dao层对象

package com.gxa.spring.day01;

public class PetServiceImpl {

    private PetDaoImplpetDao; //依赖对象
private ItemDaoImplitemDao; //依赖对象 public void setPetDao(PetDaoImplpetDao) {
this.petDao = petDao;
} public void setItemDao(ItemDaoImplitemDao) {
this.itemDao = itemDao;
} public void selectPet() {
petDao.selectPet();
itemDao.selectItem();
}
}

2. 我们再创建两个Dao层的类(这里我们也再声明一下Dao层的也没有定义接口,只是一个普通的Java类),PetDaoImpl和ItemDaoImpl

package com.gxa.spring.day01;

public class PetDaoImpl {

    public void selectPet() {
/**
* 完成宠物数据查询
*/
System.out.println("==宠物数据查询==");
}
}
package com.gxa.spring.day01;

public class ItemDaoImpl {

    public void selectItem() {
/**
* 完成宠物分类数据查询
*/
System.out.println("==宠物分类的数据查询==");
}
}

大家注意一下PetServiceImpl中的两个Dao类,我们并没有在PetServiceImpl类使用new关键字来初始化。而是分别给PetDaoImpl和ItemDaoImpl追加两个setter方法。这两个setter方法非常关键,Spring框架就是通过setter方法来代替PetServiceImpl类给两个Dao层的类进行初始化的

3. 编写一个Spring的配置文件,将PetServiceImpl, PetDaoImpl, ItemDaoImpl 注入到 Spring框架中。同时还配置了PetServiceImpl的依赖关系(DI)

<?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.xsd"> <bean id="person" class="com.gxa.spring.day01.Person"></bean> <bean id="petService" class="com.gxa.spring.day01.PetServiceImpl">
<property name="petDao" ref="petDao"></property>
<property name="itemDao" ref="itemDao"></property>
</bean> <bean id="petDao" class="com.gxa.spring.day01.PetDaoImpl"></bean> <bean id="itemDao" class="com.gxa.spring.day01.ItemDaoImpl"></bean>
</beans>

注意上面的<bean id="petService" .../>中的<property .../>,此标签就是在给PetServiceImpl设置依赖关系

4. 编写一个测试类

package com.gxa.spring.test01;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.gxa.spring.day01.Person;
import com.gxa.spring.day01.PetServiceImpl; public class Test01 { @Test
public void m02() {
BeanFactorybeanFactory = new ClassPathXmlApplicationContext("spring.xml");
PetServiceImplpetService = (PetServiceImpl) beanFactory.getBean("petService");
petService.selectPet();
}
}

最新文章

  1. Galaxy Classification
  2. 点云匹配和ICP算法概述
  3. 第一百九十二天 how can I 坚持
  4. java Junit 测试中异常处理
  5. Apple-Watch开发2 APPIcon设置
  6. Beauty of Array(思维)
  7. MySQL-python 1.2.3 for Windows and Python 2.7, 32bit and 64bit versions -(亲测可用)
  8. MVC5 DB FIRST
  9. 图像检索:FCTH(Fuzzy Color and Texture Histogram)算法
  10. 在vue-cli3 中import引入一个没有export default{}的js文件
  11. Java 容器 &amp; 泛型:二、ArrayList 、LinkedList和Vector比较
  12. 在 PHP 7 中不要做的 10 件事
  13. Javascript - ExtJs - TabPanel组件
  14. spring用注解配置,不用XML
  15. Google浏览器设置变更默认搜索引擎为百度
  16. Perl的调试模式熟悉和应用
  17. XCode9的新变化
  18. Python中被双下划线包围的魔法方法
  19. 前端开发 - JavaScript - 下
  20. HDU 5869 Different GCD Subarray Query 树状数组+离线

热门文章

  1. AIFramework基本概念整理
  2. OFRecord 图片文件制数据集
  3. H.264 Video Codec速度和质量
  4. Redis系列(一):安装
  5. Java期末考试编程题复习
  6. springmvc自定义的拦截器以及拦截器的配置
  7. MySQL索引简介(转)
  8. MySQL的启动选项和系统变量该如何配置?
  9. 入门实践丨如何在K3s上部署Web应用程序
  10. AliCloudDenoise 语音增强算法:助力实时会议系统进入超清音质时代