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杂记”。
相关文章
|
7天前
|
SQL Java 数据库连接
(自用)Spring常用配置
(自用)Spring常用配置
13 0
|
28天前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
24 1
|
29天前
|
SQL Java 数据库连接
挺详细的spring+springmvc+mybatis配置整合|含源代码
挺详细的spring+springmvc+mybatis配置整合|含源代码
35 1
|
7天前
|
JSON Java 数据库连接
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
属性注入掌握:Spring Boot配置属性的高级技巧与最佳实践
14 1
|
7天前
|
XML Java 数据格式
进阶注解探秘:深入Spring高级注解的精髓与实际运用
进阶注解探秘:深入Spring高级注解的精髓与实际运用
23 2
|
7天前
|
XML Java 数据格式
从入门到精通:Spring基础注解的全面解析
从入门到精通:Spring基础注解的全面解析
23 2
从入门到精通:Spring基础注解的全面解析
|
7天前
|
Java 数据库连接 Spring
简化配置,提高灵活性:Spring中的参数化配置技巧
简化配置,提高灵活性:Spring中的参数化配置技巧
17 0
|
7天前
|
Java Shell 测试技术
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
16 0
一次配置,多场景适用:Spring Boot多套配置文件的深度剖析
|
11天前
|
Java 容器
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
SpringBoot使用配置注解开启自动配置功能&整合spring-boot-configuration-processor
12 0
|
29天前
|
Java 数据库连接 Spring
Spring5深入浅出篇:Spring配置⽂件参数化
该文档介绍了Spring配置文件参数化的概念和步骤。目的是将经常需要修改的配置,如数据库连接参数,从主配置文件中分离到一个单独的`.properties`文件,以便于管理和维护。步骤包括创建小型配置文件(如`db.properties`),在`applicationContext.xml`中引入该文件,并使用`${key}`语法在Spring配置文件中引用这些参数。最终通过测试验证配置文件参数化的成功。