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

相关推荐
rengang663 分钟前
351-Spring AI Alibaba Dashscope 多模型示例
java·人工智能·spring·多模态·spring ai·ai应用编程
小蒜学长30 分钟前
springboot酒店客房管理系统设计与实现(代码+数据库+LW)
java·数据库·spring boot·后端
lang201509281 小时前
Spring MVC配置全解析
java·spring·mvc
せいしゅん青春之我2 小时前
【JavaEE初阶】TCP核心机制10——异常情况的处理
java·网络·笔记·网络协议·tcp/ip·java-ee
摇滚侠2 小时前
Spring Boot3零基础教程,把 Java 程序打包为 Linux 可执行文件,笔记91
java·linux·笔记
mount_myj2 小时前
敏感信息屏蔽(一)【java】
java·算法·极课堂
Zhangzy@2 小时前
Rust 内存对齐与缓存友好设计
spring·缓存·rust
四谎真好看3 小时前
Java 黑马程序员学习笔记(进阶篇21)
java·开发语言·笔记·学习·学习笔记
Dnui_King3 小时前
Kingbase 接口兼容性测试
java
Java&Develop3 小时前
IDEA报错:前言中不允许有内容
java