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

相关推荐
晚霞的不甘3 分钟前
CANN 编译器深度解析:UB、L1 与 Global Memory 的协同调度机制
java·后端·spring·架构·音视频
SunnyDays10115 分钟前
使用 Java 冻结 Excel 行和列:完整指南
java·冻结excel行和列
摇滚侠16 分钟前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
云姜.21 分钟前
java多态
java·开发语言·c++
李堇24 分钟前
android滚动列表VerticalRollingTextView
android·java
泉-java43 分钟前
第56条:为所有导出的API元素编写文档注释 《Effective Java》
java·开发语言
寄存器漫游者1 小时前
Linux 软件编程 - IO 编程
linux·运维·spring
zfoo-framework1 小时前
帧同步和状态同步
java
charlotte102410241 小时前
高并发:关于在等待学校教务系统选课时的碎碎念
java·运维·网络
亓才孓1 小时前
[JDBC]PreparedStatement替代Statement
java·数据库