当前位置: 首页 > news >正文

Spring IOC控制反转、DI注入以及配置

1.使用xml的方式进行配置IOC容器,首先引入依赖

在Resource资源下配置,applicationContext.xml ,刷新mevan后可以直接选择配置spring.xml文件

<!--    spring核心用来管理bean    --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency>

 applicationContext.xml 

配置bean: 

id:实现标识给bean起名字

class属性标识给这个bean对象定义类型

name:属性是可以给当前bean起别名(使用“ ”,“,”,“;”分隔开来)

singleton:单例(默认)

prototype:非单例

<?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="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/><bean id="bookService" class="org.itheima.service.impl.BookServiceImpl"/>
</beans>

现在配置DAO层:

public interface BookDAO {public void book();
}
public class BookDAOImpl implements BookDAO {@Overridepublic void book() {System.out.println("BookDAO……");}
}

Service层:

public interface BookService {public void write();
}
public class BookServiceImpl implements BookService {private BookDAO bookDAO = new BookDAOImpl();@Overridepublic void write() {System.out.println("BookService……");}
}

测试文件1:

public class BookDAOTest {@Testpublic void book() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookDAO bookDAO = (BookDAO) ctx.getBean("bookDAO");bookDAO.book();}
}

打印结果: 

测试文件2: 

public class BookServiceTest {@Testpublic void write() {ClassPathXmlApplicationContext txc = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) txc.getBean("bookService");bookService.write();}
}

打印结果:  

 

依赖  new ClassPathXmlApplicationContext("applicationContext.xml")的方式不太合适,同样是创建对象的形式,那不如直接new一个实体DAO,因此此方法需要改进!!

2.使用dao与Service的关系

与以上不同的是BookServiceImpl与测试类不同: 

public class BookServiceImpl implements BookService {
private BookDAO bookDAO = new BookDAOImpl();@Overridepublic void write() {bookDAO.book();System.out.println("BookService……");}
}

 测试类:

public class BookServiceImplTest {@Testpublic void testWrite() {ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) cax.getBean("bookService");bookService.write();}
}

测试结果:
 

3.使用DI注入 bean实例,并且使用service 调用dao层方法

首先配置Service的依赖关系properties

 properties:配置当前bean的属性

name属性标识配置哪一个具体的属性

ref属性表示参照哪个bean对象

<?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="bookDAO" class="org.itheima.dao.impl.BookDAOImpl"/><bean id="bookService" class="org.itheima.service.impl.BookServiceImpl"><property name="bookDAO" ref="bookDAO"/></bean>
</beans>

dao层:

public interface BookDAO {public void book();
}
public class BookDAOImpl implements BookDAO {@Overridepublic void book() {System.out.println("BookDAO……");}
}

 service层:

public interface BookService {public void write();
}

通过容器将bookDAO对象传入进去 

public class BookServiceImpl implements BookService {private BookDAO bookDAO;@Overridepublic void write() {bookDAO.book();System.out.println("BookService……");}public void setBookDAO(BookDAO bookDAO) {this.bookDAO = bookDAO;}
}

测试类:

public class BookServiceImplTest {
private BookService bookService;@Testpublic void write() {ClassPathXmlApplicationContext cax = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) cax.getBean("bookService");bookService.write();}
}

测试结果:

http://www.lryc.cn/news/330187.html

相关文章:

  • RabbitMQ的部分模式
  • 提取单选框的值,并通过ajax传值到后台
  • Django创建多app应用
  • 如何反反爬虫
  • wireshark抓包之DNS协议
  • 升级到 Java 21 是值得的
  • C# 多线程
  • 快速安装sudachipy日语包
  • 蓝桥杯刷题day13——乘飞机【算法赛】
  • 大模型量化技术-BitsAndBytes
  • EasyExcel 复杂表头的导出(动态表头和静态表头)
  • centos7 fatal error: curl/curl.h: No such file or directory
  • 【Linux】自定义协议+序列化+反序列化
  • 常见故障排查和优化
  • 选择华为HCIE培训机构有哪些注意事项
  • python怎么处理txt
  • SAMRTFORMS 转换PDF 发送邮件
  • 探讨在大数据体系中API的通信机制与工作原理
  • 算法打卡day23
  • 每天五分钟深度学习:神经网络和深度学习有什么样的关系?
  • 基于PSO优化的CNN-LSTM-Attention的时间序列回归预测matlab仿真
  • 物联网监控可视化是什么?部署物联网监控可视化大屏有什么作用?
  • 设计一个Rust线程安全栈结构 Stack<T>
  • Docker Desktop 在 Windows 上的安装和使用
  • 2024年最受欢迎的 19 个 VS Code 主题排行榜
  • 突破编程_C++_网络编程(OSI 七层模型(物理层与数据链路层))
  • Spring boot如何使用redis缓存
  • 红蓝色WordPress外贸建站模板
  • python爬虫----了解爬虫(十一天)
  • 碳素光线疗法与宠物健康