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.
分享到:
相关推荐
Spring、SpringMVC和Mybatis是Java开发中最常用的三大开源框架,它们的整合使用,通常被称为SSM框架。这个框架组合提供了完整的后端服务解决方案,包括依赖注入(DI)、面向切面编程(AOP)、模型-视图-控制器(MVC...
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件。本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,...
Spring Integration + Spring WS 整合 在 Java 领域中,Spring Integration 和 Spring WS 是两个常用的框架,它们分别负责集成系统和 Web 服务。今天,我们将探讨如何将这两个框架整合在一起,实现一个完整的 Web ...
### Spring Boot、Spring Cloud、Spring Boot Alibaba及其配套组件版本关系详解 #### 一、引言 随着微服务架构的流行,Spring Boot、Spring Cloud、Spring Boot Alibaba等技术栈成为构建现代分布式系统的基石。然而...
包含spring 3.0.5的所有jar文件: org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE...
Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统。Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问和利用企业级服务。Spring Batch可以提供...
Spring框架是Java应用程序开发中的一个核心组件,它提供了一个丰富的IOC(Inversion of Control,控制反转)和AOP(Aspect-Oriented Programming,面向切面编程)功能,使得开发者能够更方便地管理对象和实现模块化...
在Java开发领域,Spring Boot和Spring Batch的整合是构建高效批处理系统的一种常见方式。Spring Boot以其简洁的配置和快速的启动能力深受开发者喜爱,而Spring Batch作为Spring框架的一部分,专注于批量处理任务,...
在构建分布式系统时,Spring Cloud Gateway 作为微服务架构中的边缘服务或 API 网关,扮演着至关重要的角色。它负责路由请求到相应的微服务,并可以提供过滤器功能,如限流、熔断等。而Spring Security 则是 Java ...
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
这篇文章将教你快速地上手使用 Spring 框架. 如果你手上有一本《Spring in Action》, 那么你最好从第三部分"Spring 在 Web 层的应用--建立 Web 层"开始看, 否则那将是一场恶梦! 首先, 我需要在你心里建立起 Spring...
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
Spring 详细讲解 Spring 是一个功能强大且功能齐全的 Java 应用程序框架,提供了一个通用的基础结构来支持开发企业级应用程序。 Spring 框架的核心是控制反转(IoC)和依赖注入(DI)模式,它们使得应用程序更加...
介绍一个基于Spring Boot 3.0、Spring Cloud 2022 & Alibaba的微服务RBAC权限管理系统。该系统可以实现微服务RBAC权限管理,通过RBAC权限管理机制对用户访问系统的权限进行限制,从而提高系统的安全性和可用性。同时...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...
Spring Cloud和Spring Boot是两个非常重要的Java开发框架,它们在微服务架构中扮演着核心角色。Spring Boot简化了创建独立的、生产级别的基于Spring的应用程序的过程,而Spring Cloud则为开发者提供了快速构建分布式...
《Spring AI Core 0.8.1:开启人工智能之旅》 在现代软件开发领域,Spring框架以其强大的功能和灵活性,已经成为Java开发中的首选框架之一。而Spring AI Core则是Spring生态系统中专门为人工智能(AI)和机器学习...
《Spring技术内幕:深入解析Spring架构与设计原理(第2版)》从源代码的角度对Spring的内核和各个主要功能模块的架构、设计和实现原理进行了深入剖析。你不仅能从本书中参透Spring框架的出色架构和设计思想,还能从...
Spring 框架是 Java 开发中的一个核心组件,它为构建企业级应用程序提供了全面的编程和配置模型。Spring 4.3.14 是该框架的最后一个4.x系列正式版,发布于2018年2月24日。这个版本在Spring 5.0发布之前提供了一个...
Spring Cloud系列教程 Spring Boot Spring Cloud Stream 和 Kafka案例教程 springcloud生产者与消费者项目实战案例 Spring Cloud 中断路器 Circuit Breaker的应用 配置 Spring Cloud Config Server Spring Cloud ...