Inversion of Control (IoC) is also known as
dependency injection (DI). It is a process whereby objects define their dependencies only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse of the bean itself controlling the instantiation
1. version history
1)
Traditional XML based configuration
2)
Annotation-based configuration:
Spring 2.5 introduced support for annotation-based configuration metadata.
3)
Java-based configuration: Starting with
Spring 3.0, many features provided by the
Spring JavaConfig project became part of the core Spring Framework. Thus, you can define beans external to your application classes by using Java rather than XML files. See
@Configuration, @Bean, @Import, @DependsOn annotations.
2. ApplicationContext is a sub-interface of BeanFactory. It adds:
+ Easier integration with Spring’s AOP features
+ Message resource handling (for use in internationalization)
+ Event publication
+ Application-layer specific contexts such as the WebApplicationContext for use in web applications.
3. Annotation
1) @Configuration & @Bean
@Configuration is a class-level annotation indicating that an object is a source of bean definitions.
Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class.
@Bean is a method-level annotation and a direct analog of the XML <bean/> element. @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container.
You can use the @Bean annotation in a @Configuration-annotated or in a @Component-annotated class.
Any classes defined with the @Bean annotation support the regular lifecycle callbacks and can use the @PostConstruct and @PreDestroy annotations from JSR-250.
2) Autowire
Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean
autowiring.
To autodetect these classes and register the corresponding beans, you need to add @ComponentScan to your @Configuration class, where the basePackages attribute is a common parent package for the two classes.
@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {
// ...
}
Same to below XML:
<beans ...>
<context:component-scan base-package="org.example"/>
</beans>
After enabling annotation injection, we can use autowiring on properties, setters, and constructors.
3) AnnotationConfigApplicationContext
When @Configuration classes are provided as input, the @Configuration class itself is registered as a bean definition and all declared @Bean methods within the class are also registered as bean definitions.
When @Component and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as @Autowired or @Inject are used within those classes where necessary.
3) @Inject & @Name
Starting with
Spring 3.0, Spring offers support for
JSR-330 standard annotations (Dependency Injection). Those annotations are scanned in the same way as the Spring annotations.
Instead of @Component, you can use @javax.inject.Named or javax.annotation.ManagedBean.
4) @Component & @Repository, @Service, @Controller
@Component is a generic stereotype for any Spring-managed component. @Repository, @Service, and @Controller are specializations of @Component for more specific use cases
5) Other
@Resource (since JSR-250) annotation (javax.annotation.Resource) on fields or bean property setter methods.
@Value is typically used to inject externalized properties:
@Import annotation allows for loading @Bean definitions from another configuration class
@Profile annotation lets you indicate that a component is eligible for registration when one or more specified profiles are active
@PropertySource annotation provides a convenient and declarative mechanism for adding a PropertySource to Spring’s Environment.
4. AOP
Within your Spring configurations, all aspect and advisor elements must be placed within an <aop:config> element. An <aop:config> element can contain pointcut, advisor, and aspect elements (note that these must be declared in that order)
A pointcut that represents the execution of any business service
Each advice is a Spring bean. An Advisor is an aspect that contains only a single advice object associated with a pointcut expression.
E.g.
<aop:config>
<aop:advisor
pointcut="com.xyz.myapp.CommonPointcuts.businessService()"
advice-ref="tx-advice"/>
</aop:config>
<tx:advice id="tx-advice">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
The basic way to create an AOP proxy in Spring is to use the org.springframework.aop.framework.ProxyFactoryBean.
One of the most important benefits of using a ProxyFactoryBean or another IoC-aware class to create AOP proxies is that advices and pointcuts can also be managed by IoC.
分享到:
相关推荐
我的第一个C#小程序之简单音乐播放器1731655933.html
练习springboot1 项目 模拟高并发秒杀,实现基本的登录、查看商品列表、秒杀、下单等功能,简单实现了系统缓存、降级和限流。SpringBoot + MyBatis + MySQL+Druid + Redis + RabbitMQ + Bootstrap + jQue….zip
html常规学习.zip资源资料用户手册
【项目资源】:包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。【项目质量】:所有源码都经过严格测试,可以直接运行。功能在确认正常工作后才上传。【适用人群】:适用于希望学习不同技术领域的小白或进阶学习者。可作为毕设项目、课程设计、大作业、工程实训或初期项目立项。【附加价值】:项目具有较高的学习借鉴价值,也可直接拿来修改复刻。对于有一定基础或热衷于研究的人来说,可以在这些基础代码上进行修改和扩展,实现其他功能。【沟通交流】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。鼓励下载和使用,并欢迎大家互相学习,共同进步。
HTML转PDF py脚本
yolo系列算法目标检测数据集,包含标签,可以直接训练模型和验证测试,数据集已经划分好,包含数据集配置文件data.yaml,适用yolov5,yolov8,yolov9,yolov7,yolov10,yolo11算法; 包含两种标签格:yolo格式(txt文件)和voc格式(xml文件),分别保存在两个文件夹中; yolo格式:<class> <x_center> <y_center> <width> <height>, 其中: <class> 是目标的类别索引(从0开始)。 <x_center> 和 <y_center> 是目标框中心点的x和y坐标,这些坐标是相对于图像宽度和高度的比例值,范围在0到1之间。 <width> 和 <height> 是目标框的宽度和高度,也是相对于图像宽度和高度的比例值
西电通院模电大作业课后题电路设计图24年
本文档主要讲述的是sqlserver内存释放;希望本文档会给有需要的朋友带来帮助;感兴趣的朋友可以过来看看
zw
发动机制造厂技术处安全、消防安全手册.docx
生产现场工艺文件执行检查管理流程说明.docx
Spring Boot集成Spring Security,HTTP请求授权配置:包含匿名访问、允许访问、禁止访问配置
通过设置截止频率和带宽来获取对应的滤波器参数
全国月尺度平均风速数据集(1961-2022, 0.25° × 0.25°)是一个高分辨率的网格化平均风速数据集,覆盖了中国大陆及周边地区。 该数据集通过科学方法整合气象观测和再分析数据,为气候研究、生态模型、农业生产、以及水资源管理等领域提供了重要支持。 数据下载后可显示详细信息。
styles
用VHDL语言设计电梯控制器.doc
管道试压报审验表、管道强度、严密性试验记录表.doc
使用springboot实现的旅游网站
所有库函数和源代码
存储介质信息消除工具应用完善的数据消除算法,严格按照BMB21-2007《涉及国家秘密的载体销毁与信息消除安全保密要求》标准,能够灵活的实现对存储介质中的数据进行完全擦除,不留痕迹,是我国各级政府、军工保密信息化建设以及各企业中不可缺少的工具。 数据一旦执行消除操作,专业的数据恢复工具也无法对其进行恢复,彻底解决用户的后顾之忧。同时不损坏存储介质,是国内先进的非暴力信息消除工具,可以有效降低用户的存储成本。可以对各种硬盘、软盘、U 盘、存储卡等进行数据粉碎,并且支持多种的磁盘分区格式,包括FAT 系列、NTFS 系列等磁盘格式进行数据销毁,确保了存储介质数据信息的安全性。 存储介质信息消除工具适用于机密级即以下涉密计算机存储介质上的信息消除,满足分级保护系统要求。 主要功能: 1. 支持单个或多个文件、目录、磁盘信息的消除。 2. 支持单个或多个磁盘剩余空间中残留信息的消除。 3. 支持搜索深度上网痕迹、文件(夹)删除痕迹、深度USB存储设备接入痕迹来确认系统中是否残留涉密信息 4. 支持清除其他多种违规外联痕迹