Spring自动装配和注解配置

简介:

前面已经学会如何使用的<bean>元素来声明bean和注入<bean>,通过使用在XML配置文件<constructor-arg>和<property>元素。

Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的<constructor-arg>和<property>元素。

自动装配模式:

有下列自动装配模式,可用于指示Spring容器使用自动装配依赖注入。使用<bean/>元素的autowire属性为一个bean定义中指定自动装配模式。

模式 描述
no This is default setting which means no autowiring and you should use explicit bean reference for wiring. You have nothing to do special for this wiring. This is what you already have seen in Dependency Injection chapter.
byName Autowiring by property name. Spring container looks at the properties of the beans on which autowire attribute is set to byName in the XML configuration file. It then tries to match and wire its properties with the beans defined by the same names in the configuration file.
byType Autowiring by property datatype. Spring container looks at the properties of the beans on which autowire attribute is set to byType in the XML configuration file. It then tries to match and wire a property if its typematches with exactly one of the beans name in configuration file. If more than one such beans exists, a fatal exception is thrown.
constructor Similar to byType, but type applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised.
autodetect Spring first tries to wire using autowire by constructor, if it does not work, Spring tries to autowire by byType.

可以使用类型和constructor自动装配模式来连接数组和其他类型化的集合。

自动装配的局限性:

自动装配最好效果是它始终在一个项目中使用。如果自动装配不一般的使用,它可能会被混淆为开发人员可以使用它来连接只有一个或两个bean定义。不过,自动装配可以显著减少需要指定属性或构造器参数,但你应该使用它们之前考虑自动装配的局限性和缺点。

限制 描述
压倒一切的可能性 可以使用<constructor-arg>和<property>设置总是覆盖自动装配还指定依赖关系。
原始数据类型 不能自动装配所谓的简单类型包括基本类型,字符串和类。
混乱的本质 自动装配比显式装配确切的少,所以如果可能的话可以使用显式的连接。

从Spring2.5开始就有可能使用注释来配置依赖注入。而是采用XML来描述一个bean接线,你可以使用注解的相关类,方法或字段声明将bean配置到组件类本身。

注释注入在XML注入之前进行,因此后者的配置将覆盖前者通过两种方式连接的属性。

注释接线默认情况下不开启在Spring容器。所以,我们才可以使用基于注解的接线,我们将需要启用它在我们的Spring配置文件。因此,考虑到已在下列情况下,配置文件要使用的任何注释在Spring应用程序。

 
<? 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
<context:annotation-config/>
<!-- bean definitions go here -->
</beans>

当<context:annotation-config/>配置后,您就可以开始注释代码表明,Spring自动连线的值到属性,方法和构造函数。让我们来看看几个重要的注解,以了解它们是如何工作的:

S.N. 注释与说明
1 @Required
@Required注释适用于bean属性的setter方法。
2 @Autowired
@Autowired 注释可以应用到bean属性的setter方法,非setter方法,构造函数和属性。
3 @Qualifier
@ Autowired随着@ Qualifier注释可以用来通过指定确切的bean将有线,除去混乱。
4 JSR-250 Annotations
Spring支持JSR-250的基础的注解,其中包括了@Resource,@PostConstruct和@PreDestroy注解。


原文发布时间为:2018-10-23
本文来自云栖社区合作伙伴“ Java杂记”,了解相关信息可以关注“ Java杂记”。
相关文章
|
5月前
|
Java 关系型数据库 MySQL
Spring Boot自动配置:魔法背后的秘密
Spring Boot 自动配置揭秘:只需简单配置即可启动项目,背后依赖“约定大于配置”与条件化装配。核心在于 `@EnableAutoConfiguration` 注解与 `@Conditional` 系列条件判断,通过 `spring.factories` 或 `AutoConfiguration.imports` 加载配置类,实现按需自动装配 Bean。
|
5月前
|
缓存 监控 Java
SpringBoot @Scheduled 注解详解
使用`@Scheduled`注解实现方法周期性执行,支持固定间隔、延迟或Cron表达式触发,基于Spring Task,适用于日志清理、数据同步等定时任务场景。需启用`@EnableScheduling`,注意线程阻塞与分布式重复问题,推荐结合`@Async`异步处理,提升任务调度效率。
877 128
|
4月前
|
安全 Java 决策智能
Spring Boot自动装配
Spring Boot自动装配基于“约定优于配置”理念,通过条件化配置与Starters机制,智能推断并加载所需组件,大幅简化开发流程。它实现配置自动化,提升效率,降低维护成本,支持自定义扩展,推动微服务快速构建,是Java生态中开发范式的革新之作。(238字)
|
4月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
4月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
568 2
|
5月前
|
XML Java 数据格式
常用SpringBoot注解汇总与用法说明
这些注解的使用和组合是Spring Boot快速开发和微服务实现的基础,通过它们,可以有效地指导Spring容器进行类发现、自动装配、配置、代理和管理等核心功能。开发者应当根据项目实际需求,运用这些注解来优化代码结构和服务逻辑。
425 12
|
5月前
|
缓存 Java 应用服务中间件
Spring Boot配置优化:Tomcat+数据库+缓存+日志,全场景教程
本文详解Spring Boot十大核心配置优化技巧,涵盖Tomcat连接池、数据库连接池、Jackson时区、日志管理、缓存策略、异步线程池等关键配置,结合代码示例与通俗解释,助你轻松掌握高并发场景下的性能调优方法,适用于实际项目落地。
939 5
|
Java API Spring
Spring容器如何使用一个注解来指定一个类型为配置类型
Spring容器如何使用一个注解来指定一个类型为配置类型
222 0
|
XML Java 测试技术
Spring IOC—基于注解配置和管理Bean 万字详解(通俗易懂)
Spring 第三节 IOC——基于注解配置和管理Bean 万字详解!
870 26
|
5月前
|
传感器 Java 数据库
探索Spring Boot的@Conditional注解的上下文配置
Spring Boot 的 `@Conditional` 注解可根据不同条件动态控制 Bean 的加载,提升应用的灵活性与可配置性。本文深入解析其用法与优势,并结合实例展示如何通过自定义条件类实现环境适配的智能配置。
308 0
探索Spring Boot的@Conditional注解的上下文配置