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的方式实现装配。

相关推荐
karry_k4 分钟前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k11 分钟前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking4 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩7 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码8 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev10 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波18 小时前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯20 小时前
GoF设计模式——备忘录模式
java·后端·spring·设计模式