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

spring学习笔记七

一、自动装配

1.1、BookDao接口和实现类

public interface BookDao {void save();
}
public class BookDaoImpl implements BookDao {public void save(){System.out.println("book dao save......");}
}

1.2、BookService接口和实现类

public interface BookService {void save();
}
public class BookServiceImpl implements BookService {private BookDao bookDao;public void setBookDao(BookDao bookDao) {this.bookDao = bookDao;}public void save(){bookDao.save();}
}

1.3、配置文件

<?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="com.itheima.dao.impl.BookDaoImpl" ></bean><bean id="bookService" name="service" class="com.itheima.service.impl.BookServiceImpl" autowire="byType"></bean>
</beans>

1.4、使用方法

public static void main(String[] args) {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");BookService bookService = (BookService) ctx.getBean("service");bookService.save();}

1.5、总结

  • 在配置文件中添加autowire属性为byType
  • spring会按照BookDao类型找到对应的Bean实现注入
http://www.lryc.cn/news/100970.html

相关文章:

  • hw技战法整理参考
  • uniapp 全局数据(globalData)的设置,获取,更改
  • Profinet转EtherNet/IP网关连接AB PLC的应用案例
  • Python组合模式介绍、使用方法
  • 生成模型和判别模型工作原理介绍
  • shardingsphere读写分离配置
  • 登录报错 “msg“:“Request method ‘GET‘ not supported“,“code“:500
  • Python 日期和时间
  • pytorch的发展历史,与其他框架的联系
  • Kibana-elastic--Elastic Stack--ELK Stack
  • Docker复杂命令便捷操作
  • Python中的datetime模块
  • Flutter - 微信朋友圈、十字滑动效果(微博/抖音个人中心效果)
  • MySQL检索数据和排序数据
  • 通过STM32内部ADC将烟雾传感器发送的信号值显示在OLED上
  • ZEPHYR 快速开发指南
  • 【FPGA + 串口】功能完备的串口测试模块,三种模式:自发自收、交叉收发、内源
  • 初步了解预训练语言模型BERT
  • Android Hook系统 Handler 消息实现
  • R语言从入门到精通之【R语言的使用】
  • WPF实战学习笔记29-登录数据绑定,编写登录服务
  • c++函数式编程:统计文件字符串,文件流
  • scp命令----跨服务器传输文件
  • React Dva项目中模仿网络请求数据方法
  • 【云原生】Docker容器命令监控+Prometheus监控平台
  • DBA 职责及日常工作职责
  • 如何利用量化接口进行数据分析和计算?
  • electron-egg 加密报错
  • 循环队列的基本操作(3种处理方式,2种实现方式)
  • react的特点