Spring 模拟管理Web应用程序

MVC:Model View Controller

1)controller:控制层(Servlet是运行服务器端,处理请求响应java语言编写技术)

2)service:业务层(事务,异常)

3)dao:持久层(CRUD)

Spring :IOC 和 DI

准备工作

在java文件夹的com.xja下创建

Dao包:

StudentDao.java

StudentDaoImpl.java

Service包:

StudentService.java

StudentServiceImpl.java

Controller包:

StudentController.java

StudentControllerImpl.java

在Spring的配置文件中添加相应实体类的注入

自动装配(autowire):

1.按名称;byName

只要对象对应属性名与xml中实例化对象id一致可以实现自动装配

2.按照类型;byType

只要对象对应属性类型与xml中实例化对象类型一致可以实现自动装配

3.constructor

默认不会自动装配

<bean id="studentController" class="com.xja.controller.StudentController" autowire="byName">

// 等价于 <property name="studentService" ref="studnetService"/>

</bean>
<bean id="studentService" class="com.xja.service.impl.StudentServiceImpl" autowire="constructor">

// 等价于 <constructor-arg name="studentDao" ref="studnetDao"/>

</bean>
<bean id="studentDao" class="com.xja.dao.impl.StudentDaoImpl" />

全局设置autowire:

注意:byType方式自动装配:

要装配的实现类实现接口,还有别的实现类也实现了接口,

这时只能使用byName的方式实现装配。

相关推荐
王磊鑫1 小时前
重返JAVA之路-初识JAVA
java·开发语言
半兽先生1 小时前
WebRtc 视频流卡顿黑屏解决方案
java·前端·webrtc
南星沐2 小时前
Spring Boot 常用依赖介绍
java·前端·spring boot
代码不停3 小时前
Java中的异常
java·开发语言
何似在人间5754 小时前
多级缓存模型设计
java·jvm·redis·缓存
多云的夏天4 小时前
ubuntu24.04-MyEclipse的项目导入到 IDEA中
java·intellij-idea·myeclipse
Fanxt_Ja4 小时前
【数据结构】红黑树超详解 ---一篇通关红黑树原理(含源码解析+动态构建红黑树)
java·数据结构·算法·红黑树
Aphelios3804 小时前
TaskFlow开发日记 #1 - 原生JS实现智能Todo组件
java·开发语言·前端·javascript·ecmascript·todo
weixin_448771724 小时前
使用xml模板导出excel
xml·java·excel
烁3475 小时前
每日一题(小白)模拟娱乐篇27
java·数据结构·算法·娱乐