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

Spring IoC 注解 总结

1. 简介

本文介绍了Spring的IoC通过注解的方式实现。

2. 具体步骤

用注解的方式向IoC声明Bean总共分为两步:

  • 在对应的class前加上注解
  • xml中添加扫描范围

2.1 在对应的class前加上注解:

@Component //默认id为commonComponent
public class CommonComponent {
}

@Component("myid") //指定id为myid,
public class CommonComponent {
}

@Controller //和@Component等效,用于Controller层,默认id为myController
public class MyController {
}

@Service //和@Component等效,用于Service层,默认id为myService
public class MyService {
}

@Repository  //和@Component等效,用于Dao层,默认id为myDao
public class MyDao {
}

2.2 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 1.指定在哪里扫描包,包之间用逗号隔开-->
    <context:component-scan base-package="com.jojo.ioc"/>
     
     <!-- 2.指定在哪里扫描包,但也排除包-->
    <context:component-scan base-package="com.jojo.ioc">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
</beans>

     <!-- 3.指定在哪里扫描包,只包含指定包-->
    <context:component-scan base-package="com.jojo.ioc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>

2.3 在配置类中添加扫描范围

使用java配置类代替xml的配置工作

@ComponentScan("com.jojo.ioc_01")//扫描
@PropertySource(value = "classpath:jdbc.properties")
@Configuration
public class JavaConfiguration {
}

2.4 使用

下面介绍如何使用bean

//1.指定xml文件
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("xxx.xml");

//2.得到bean对象
UserController bean = applicationContext.getBean(UserController.class);

System.out.println("bean" + bean);
//3.关闭容器
applicationContext.close();

使用配置类的方式:

//方式1创建
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(JavaConfiguration.class);

//方式2创建
AnnotationConfigApplicationContext applicationContext1 =new AnnotationConfigApplicationContext();
applicationContext1.register(JavaConfiguration.class);
applicationContext1.refresh();

//2.获取Bean
StudentController bean = applicationContext.getBean(StudentController.class);
System.out.println(bean);

3. 周期方法

如果想在类初始化时和销毁时执行函数,可以执行的函数前加上注解即可。

@Component
public class MyClass {

    @PostConstruct //初始化注解,以下函数在bean对象初始化时执行
    public void init(){
        System.out.println("MyClass.init");
    }

    @PreDestroy //销毁注解,以下函数在bean对象销毁时执行:单例模式会调用,多例不会调用
    public void destroy(){
        System.out.println("MyClass.destroy");
    }
}

4. 指定为单例还是多例

可以通过注解指定Bean类为单例还是多例。

  • 单例:多次创建对象指向同一个对象。
  • 多例:每次创建对象都new新对象。

单例代码:

@Scope(scopeName = ConfigurableBeanFactory.SCOPE_SINGLETON)//单例模式
@Component
public class MyClass {
}

多例代码:

@Scope(scopeName = ConfigurableBeanFactory.SCOPE_PROTOTYPE)//多例模式
@Component
public class MyClass {
}

5. 自动装配

如果在Bean类中要使用其他Bean类,可以使用注解来实现自动装配。

@Controller
public class MyController {
    //自动装配注解
    @Autowired //指定装配类为MyService的bean类
    @Qualifier(value = "myServiceImpl")//如果有多个满足,则指定多个中的一个
    private MyService myService;//对应类型的bean装配
}

也可使用Resource关键字:

import jakarta.annotation.Resource;
@Controller
public class MyController {
    //自动装配注解
   	//@Resource 等同于@Autowired
    @Resource(name = "myServiceImpl")//等同于@Autowired + @Qualifier(value = "myServiceImpl")
    private MyService myService;//对应类型的bean装配
}

使用Resource注解的话,需要在xml中导入以下依赖:

<dependency>
    <groupId>jakarta.annotation</groupId>
    <artifactId>jakarta.annotation-api</artifactId>
    <version>2.1.1</version>
</dependency>

http://www.kler.cn/news/316557.html

相关文章:

  • vue是如何优化
  • 【C++算法】分治——快排
  • 力扣(leetcode)每日一题 2374 边积分最高的节点
  • 神经生物学精解【2】
  • LeetCode[中等]
  • 迷雾大陆免费辅助:强流派推荐攻略!VMOS云手机自动辅助挂机教程!
  • [JavaEE] TCP协议
  • hql杂谈一
  • 黑马智数Day3
  • C#设计模式之备忘录模式
  • CMake 构建Qt程序弹出黑色控制台
  • vue3+ant design vue 中弹窗自定义按钮设置及以冒号为基准布局
  • IM项目-----语音识别子服务
  • Java笔试面试题AI答之设计模式(4)
  • 进击J7:对于ResNeXt-50算法的思考
  • [深度学习]Pytorch框架
  • 猿大师办公助手在线编辑Office为什么要在客户端电脑安装插件微软Office或金山WPS?
  • 政务安全体系构建中的挑战
  • 使用思科搭建企业网规划训练,让网络全部互通,使用规则提高工作效率。
  • 深入解析数据库DQL语言:查询的艺术
  • 如何在SpringCloud中使用Consul进行服务发现与配置管理
  • Redis的主从模式、哨兵模式、集群模式
  • 电子电气架构 --- 基于ISO 26262的车载电子软件开发流程
  • 基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
  • 快速下载Imagenet数据集
  • Python模块和包:标准库模块(os, sys, datetime, math等)②
  • CVE-2024-2389 未经身份验证的命令注入
  • LeetCode --- 139双周赛
  • STM32篇:开发环境安装
  • 基于微信小程序的科创微应用平台设计与实现+ssm(lw+演示+源码+运行)