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

相关推荐
CodeToGym20 分钟前
【Java 办公自动化】Apache POI 入门:手把手教你实现 Excel 导入与导出
java·apache·excel
凡人叶枫31 分钟前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
JMchen1231 小时前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
阔皮大师1 小时前
INote轻量文本编辑器
java·javascript·python·c#
小法师爱分享1 小时前
StickyNotes,简单便签超实用
java·python
qq_297574671 小时前
Linux 服务器 Java 开发环境搭建保姆级教程
java·linux·服务器
金牌归来发现妻女流落街头1 小时前
【从SpringBoot到SpringCloud】
java·spring boot·spring cloud
毅炼1 小时前
Java 基础常见问题总结(4)
java·后端
张3蜂1 小时前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
GR2342342 小时前
2025年影视仓TV+手机官方版 内置地址源支持高清直播
java·智能手机·软件