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杂记”。
相关文章
|
4天前
|
Java 开发者 Spring
深入理解Spring Boot的@ComponentScan注解
【4月更文挑战第22天】在构建 Spring Boot 应用时,@ComponentScan 是一个不可或缺的工具,它使得组件发现变得自动化和高效。这篇博客将详细介绍 @ComponentScan 的基本概念、关键属性及其在实际开发中的应用。
21 4
|
6天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
29 6
|
14天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
16 0
|
7天前
|
存储 安全 Java
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(下)
16 0
|
7天前
|
安全 Java 数据库
第2章 Spring Security 的环境设置与基础配置(2024 最新版)(上)
第2章 Spring Security 的环境设置与基础配置(2024 最新版)
29 0
|
8天前
|
安全 Java Spring
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
Spring Security 5.7 最新配置细节(直接就能用),WebSecurityConfigurerAdapter 已废弃
19 0
|
8天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
24 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
|
14天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
23 1
|
14天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
26 2
|
14天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
30 2
从入门到精通:Spring基础注解的全面解析