当前位置: 首页 > news >正文

Spring框架学习day2--Bean管理(IOC)

  • Spring如何进行Bean管理(IOC)
    • 方式1:基于xml(spring.xml)
      • 依赖注入
    • 方式2:基于注释(常用)
      • 方法1:**@Autowired**
      • 方法2: **JDK 注解@Resource 自动注入**
      • **注解与 XML 的对比**

Spring如何进行Bean管理(IOC)

文件结构:

image-20250529224342397

方式1:基于xml(spring.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"><!--配置哪些类需要spring框架进行管理id:指定对象的唯一标识符name:指定对象的别名class:指定对象的全类名scope:指定对象的作用域,默认是singleton,表示对象在整个spring容器中只有一个实例,并且是在spring容器启动时创建的,还可以设置为prototype,表示每次请求时都会创建一个新的实例-->
<bean id="admin" class="org.example.model.Admin" scope="singleton"></bean>
</beans>

依赖注入

在spring创建对象时,为对象中的属性进行赋值,有的属性值可能来源于另一个对象。

1.set方法注入

<bean id="admin" class="org.example.model.Admin" scope="singleton"><property name="id" value="1"/><property name="account" value="admin"/><property name="password" value="123456"/></bean>

image-20250529182331258

2.构造方法(有参构造)

<bean id="admin" class="org.example.model.Admin" scope="singleton"><constructor-arg value="1"/><constructor-arg value="admin"/><constructor-arg value="123456"/></bean>

image-20250529182411882

案例1:

<bean id="adminDao" class="org.example.Dao.AdminDao" scope="singleton"></bean><bean id="adminSeverlet" class="org.example.web.AdminSeverlet"><property name="adminDao" ref="adminDao"/>
<!--        ref:表示引用其他bean的id--></bean>

ref表示引用其他bean的id

image-20250529183822905

方式2:基于注释(常用)

开启注解扫描

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx
">
<!--        开启注解扫描  对指定包下的注解进行扫描 ,检查添加spring类注解标签的类-->
<context:component-scan base-package="org.example.model" ></context:component-scan></beans>

在对应类上面加注解标签

image-20250529185313885

加上命名相当于 id="adminSeverlet"

image-20250529185404958

当然还有其他属性

image-20250529185523828

image-20250529190326554

@Repository					数据访问相关的类
@Component					模型类的注解标签

在类里面引用其他类

案例:在AdminSeverlet里面调用AdminDao

image-20250529190512383

注解创建对象

@Component(value=“user”)等同于 < bean id=“user” class=“”>< /bean>

@Service

@Repository 以上注解都可以实现创建对象功能,只是为了后续扩展功能,在不同的层使用不 同的注解标记

@Scope(value=“prototype”) 原型

@Scope(value=“ singleton ”) 单例

注解方式注入属性【DI:DependencyInjection】

方法1:@Autowired

@Autowired 是Spring 提供的注解,可以写在字段和setter方法上。如果写在 字段上,那么就不需要再写setter方法。默认情况下它要求依赖对象必须存在, 如果允许null值,可以设置它的required属性为false。

byType 自动注入

该注解默认使用按类型自动装配 Bean 的方式。

byName

自动注入 如果我们想使用按照名称(byName)来装配,可以结合@Qualifier注解一起 使用。

需要在引用属性上联合使用注解@Autowired 与@Qualifier。@Qualifier 的 value 属性用于指定要匹配的 Bean 的 id 值。

方法2: JDK 注解@Resource 自动注入

Spring 提供了对 jdk中@Resource注解的支持。@Resource 注解既可以按名 称匹配Bean,也可以按类型匹配 Bean。默认按照ByName自动注入

byName 注入引用类型属性

@Resource 注解指定其 name 属性,则 name 的值即为按照名称进行匹配 的 Bean 的 id。

注解方式的注入*   方式一:使用@Autowired注解,根据类型自动装配*           可以添加到要注释的属性上面,或者属性的set方法上,如果直接在属性上,那么set方法可以省略*         默认情况下,@Autowired(required=true)注解,如果没有找到bean,会抛出异常*         注入时,查找bean方式有两种*           1.通过对象名字直接查找,需要使用@Qualifier("name")*           2.通过类型查找,如果有多个相同类型的bean,会抛出异常,需要使用@Primary注解指定一个bean*            @Repository 不需要()里面命名*  方式二:使用@Resource注解,根据名称自动装配,如果没有找到bean,会抛出异常*         注入时,查找bean方式有两种*           1.通过对象名字直接查找,需要使用@Name注解*           2.通过类型查找,如果有多个相同类型的bean,会抛出异常,需要使用@Priority注解指定一个bean

注解与 XML 的对比

注解优点: 方便,直观,高效(代码少,没有配置文件的书写那么复杂)。

注解缺点:以硬编码的方式写入到 Java 代码中,修改是需要重新编译代码的。

xml 优点是: 配置和代码是分离的,在 xml 中做修改,无需编译代码,只需重 启服务器即可将新的配置加载。

xml 的缺点是:编写麻烦,效率低,大型项目过于复杂

相关文章:

  • 智能穿戴新标杆:SD NAND (贴片式SD卡)与 SOC 如何定义 AI 眼镜未来技术路径
  • 华为OD机试真题——报文回路(2025A卷:100分)Java/python/JavaScript/C/C++/GO最佳实现
  • 回调函数的理解
  • 【LangChain】框架解析
  • ASP.NET TreeView控件使用指南
  • 深入了解linux系统—— 库的链接和加载
  • 【Linux】shell脚本的变量与运算
  • OpenAI o3安全危机:AI“抗命”背后的技术暗战与产业变局
  • 代码随想录算法训练营第五十三天
  • 什么是node.js、npm、vue
  • DeepSeek进阶教程:实时数据分析与自动化决策系统
  • IDEA项目推送到远程仓库
  • Intellij IDEA 查找接口实现类的快捷键
  • 全志F1c200开发笔记——移植Debian文件系统
  • 【Rust模式与匹配】Rust模式与匹配深入探索与应用实战
  • 力扣面试150题--二叉树的右视图
  • 高速连接器设计的真相
  • 由enctype-引出post与get的关系,最后深究至请求/响应报文
  • windows系统下通过visual studio使用clang tooling
  • 【HarmonyOS Next之旅】DevEco Studio使用指南(二十八) -> 开发云对象
  • 西数网站管理助手/五行seo博客
  • 网站设计就业怎么样/seo网站推广是什么
  • 威海微网站建设/sem和seo是什么
  • 做英文网站2014/武汉网站提升排名
  • 做培训网站哪家好/搜索推广竞价托管哪家好
  • 做网站的基本步骤/深圳公关公司