IOC容器复习(基于注解)

本文详细介绍Spring框架中IOC容器的注解管理机制,包括基于注解的对象创建、属性注入及配置类的使用,帮助读者掌握Spring注解式开发的核心技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IOC容器(基于注解方式)

一、Bean管理之创建
①Spring提供的创建对象提供的注解

​ (1)@Component;

​ (2)@Controller;

​ (3)@Service;

​ (4)@Repository;

​ 这四个注解都是创建对象的注解,功能完全相同,只是推荐应用的地方不同,如:Controller推荐用在Web层,Service推荐用在Service层,Repository推荐使用在Dao层;

②创建对象的过程

​ 第一步,导入依赖:

在这里插入图片描述

​ 第二步,在配置配置文件中打开注解扫描:

​ 首先要配置一个命名空间:

<?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:cotext="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

​ 再打开注解扫描:

<!--  开启注解扫描  -->
    <cotext:component-scan base-package="com.lazy.annotation.anno1"></cotext:component-scan>

​ 第三步,创建javaBean,并添加注解:

@Component
public class Person {

    private String name;
    private Integer id;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", id=" + id +
                '}';
    }
}

​ 要注意的是没有给@Component设置名称值类似于bean标签中的id属性时,默认的值为,将类名的首字母小写作为该名称值;

​ 第四步,调用:

    public void test(){

        //加载配置文件
        ApplicationContext context = new ClassPathXmlApplicationContext("anno1.xml");

        //获取对象
        Person person = context.getBean("person", Person.class);

        //调用对象
        System.out.println(person);

    }

​ 此处仍有配置文件,所以下面介绍完全注解开发

③完全注解开发

​ 完全注解开发时用一个配置类来替代配置文件,并且使用@Configuration注解来标识;

​ 使用@ComponentScan注解来开启注解扫描;

@Configuration
@ComponentScan
public class Config {
}

​ 再调用时,也要注意加载文件的步骤修改为加载配置类的类型类;

    public void test2(){

        //加载配置文件
        ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

        //获取对象
        Person person = context.getBean("person", Person.class);

        //调用对象
        System.out.println(person);

    }
④开启注解扫描时要注意的点
<!--示例 1
 use-default-filters="false" 表示现在不使用默认 filter,自己配置 filter
 context:include-filter ,设置扫描哪些内容
--><context:component-scan base-package="com.atguigu" use-defaultfilters="false">
 <context:include-filter type="annotation" 
 
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--示例 2
 下面配置扫描包所有内容
 context:exclude-filter: 设置哪些内容不进行扫描
--><context:component-scan base-package="com.atguigu">
 <context:exclude-filter type="annotation" 
 
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
二、Bean管理之属性注入
①Spring提供的创建属性注入的注解(使用自动装配)

​ (1)@AutoWired:根据属性自动装配

​ (2)@Qualifier:根据名称自动装配(但是在使用时要和AutoWired一同使用)

​ (3)@Value:配置普通数据类型;

​ JavaBean:

@Component
public class Person {

    @Value(value = "lazy")
    private String name;

    @Value(value = "1001")
    private Integer id;

    @Autowired
    @Qualifier(value = "subject1")
    private Subject subject;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", subject=" + subject +
                '}';
    }
}

​ 外部bean:

@Component(value = "subject1")
public class Subject {

    @Value(value = "Chinese1")
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Subject{" +
                "name='" + name + '\'' +
                '}';
    }
}

​ 使用的配置类同上;

(4)@Resource即可根据属性又可以根据名称自动装配,但是是java内部提供的不推荐使用;

### 关于Spring框架期末考试复习资料 #### Spring核心概念理解 深入掌握Spring的核心特性对于应对期末考试至关重要。重点在于理解和应用IoC容器、AOP编程以及Bean管理机制等内容[^1]。 #### IoC/AOP原理剖析 - **控制反转(IoC)** 是指对象创建和依赖关系由外部容器负责,而非程序内部自行实例化。这有助于降低组件间的耦合度并提高灵活性。 - **面向切面(AOP)** 利用横切关注点分离业务逻辑与其他功能(如日志记录),通过代理模式增强目标对象行为而不修改其源码。 #### 动态代理实现细节 Java中的动态代理主要分为两种形式:基于接口的JDK Proxy 和 基于类字节码操作的 CGLIB 。前者适用于实现了特定接口的对象;后者则用于那些没有具体接口定义的情况,在不改变原有结构的基础上扩展新能力。 #### 循环依赖解决方案——三级缓存策略 当两个或多个bean之间形成相互引用时就会发生循环依赖问题。为了处理这种情况,Spring采用了一套复杂的三级缓存体系来确保即使存在这样的关联也能正常加载beans。具体来说就是利用singletonObjects, earlySingletonObjects 及 singletonFactories三个层次存储不同状态下的单例bean实例,从而有效解决了这一难题。 #### 后置处理器(Post Processor) 此类工具允许开发者自定义初始化过程前后的行为动作,比如注册额外的拦截器或是调整某些属性设置等。常见的有`BeanPostProcessor`,它可以在任何普通的bean被实际注入之前对其进行加工改造。 #### `@Transactional`注解解析及其工作流程 此注解用来声明式地开启事务边界,并自动管理和回滚异常情况下的数据库更改。其实现涉及到平台TransactionManager的选择、传播特性的设定等多个方面。了解这些可以帮助更好地设计可靠的数据访问层服务。 #### 事务传播级别的意义 不同的场景可能需要不一样的事务隔离级别以满足一致性需求。例如,默认情况下会继承上级已有的事务上下文继续执行;而在其他情形下或许更倾向于新开独立单元来进行保护性提交等等。 #### BeanFactory vs ApplicationContext对比分析 两者都是用来获取Spring容器内托管的各种资源和服务的关键入口之一。不过相较于前者较为基础的功能集而言,后者提供了更为丰富的API支持,包括但不限于事件发布订阅机制、国际化消息转换等功能模块。 ```java // 示例代码展示如何使用@Autowired进行依赖注入 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { private final AnotherService anotherService; @Autowired public MyService(AnotherService anotherService){ this.anotherService = anotherService; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值