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

发布于:2025-05-29 ⋅ 阅读:(19) ⋅ 点赞:(0)

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/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://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 的缺点是:编写麻烦,效率低,大型项目过于复杂


网站公告

今日签到

点亮在社区的每一天
去签到