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

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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.3.xsdhttp://www.springframework.org/schema/contexthttps://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>

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • GitLab 迁移并推送代码仓库
  • 2024永久激活版 Studio One 6 Pro for mac 音乐创作编辑软件 完美兼容
  • FortiGate硬件高级测试指南
  • 数据结构——“二叉搜索树”
  • 条件编译代码记录
  • React 项目中,如何实现有效的内存管理和防止内存泄漏?
  • 二叉树(下)
  • 【CSS in Depth 2 精译_030】5.2 Grid 网格布局中的网格结构剖析(下)
  • 解决服务器首次请求异常耗时问题
  • 滚雪球学SpringCloud[6.1讲]: Spring Cloud Sleuth详解
  • CSS 布局技巧实现元素左右排列
  • 速戳!普通人利用AI商业变现的5种方式!
  • 从《中国数据库前世今生》看中国数据库技术的发展与挑战
  • AI教你学Python 第3天:函数和模块
  • 【Qt | QAction】Qt 的 QAction 类介绍
  • Apache Pulsar 2.1 重磅发布
  • Github访问慢解决办法
  • Gradle 5.0 正式版发布
  • Laravel Mix运行时关于es2015报错解决方案
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • quasar-framework cnodejs社区
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 复习Javascript专题(四):js中的深浅拷贝
  • 我与Jetbrains的这些年
  • 系统认识JavaScript正则表达式
  • 移动端唤起键盘时取消position:fixed定位
  • JavaScript 新语法详解:Class 的私有属性与私有方法 ...
  • 数据库巡检项
  • !!【OpenCV学习】计算两幅图像的重叠区域
  • #、%和$符号在OGNL表达式中经常出现
  • (12)Hive调优——count distinct去重优化
  • (16)Reactor的测试——响应式Spring的道法术器
  • (C)一些题4
  • (C语言)输入一个序列,判断是否为奇偶交叉数
  • (编程语言界的丐帮 C#).NET MD5 HASH 哈希 加密 与JAVA 互通
  • (编译到47%失败)to be deleted
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (汇总)os模块以及shutil模块对文件的操作
  • (接口封装)
  • (四)JPA - JQPL 实现增删改查
  • (算法)区间调度问题
  • (转)AS3正则:元子符,元序列,标志,数量表达符
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • (轉貼)《OOD启思录》:61条面向对象设计的经验原则 (OO)
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .chm格式文件如何阅读
  • .NET 8 中引入新的 IHostedLifecycleService 接口 实现定时任务
  • .NET CF命令行调试器MDbg入门(二) 设备模拟器
  • .Net Core和.Net Standard直观理解
  • .net refrector
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .net获取当前url各种属性(文件名、参数、域名 等)的方法
  • ??如何把JavaScript脚本中的参数传到java代码段中
  • @基于大模型的旅游路线推荐方案