Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring 基于注解的配置
Spring 基于注解的配置
作者:--    发布时间:2019-11-20

从 spring 2.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>

一旦 被配置后,你就可以开始注解你的代码,表明 spring 应该自动连接值到属性,方法和构造函数。让我们来看看几个重要的注解,并且了解它们是如何工作的:

序号注解 & 描述
1@required

@required 注解应用于 bean 属性的 setter 方法。

2@autowired

@autowired 注解可以应用到 bean 属性的 setter 方法,非 setter 方法,构造函数和属性。

3@qualifier

通过指定确切的将被连线的 bean,@autowired 和 @qualifier 注解可以用来删除混乱。

4jsr-250 annotations

spring 支持 jsr-250 的基础的注解,其中包括了 @resource,@postconstruct 和 @predestroy 注解。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册