---
title: "spring 注解小结"
author: "java_min"
date: "2013-11-03T16:42:18Z"
tags: []
categories: ["开源软件"]
slug: "spring"
draft: false
lastmod: "2013-11-03T19:42:49Z"
id: 1969362
url: "https://java-min.iteye.com/blog/1969362"
---

Annotation在spring中的使用：

 1. 首先需要修改 xml 文件，参考文档 &lt;context:annotation-config /&gt;

 2. @Autowired （spring标准）

     a) 默认按类型 by type。

     b) 如果想用  by name，使用 @Qulifier。

     c) 写在 private field (第三种注释形式)（不建议，破坏封装性）

     d) 如果写在 set 方法上，@Qualifier 需要写在参数上。

 3. @Resource (重要，java标准)

     a) 加入：j2ee/common-annotations.jar。

     b) 默认按名称，名称找不到，按类型。

     c) 可以指定特定名称。

     d) 推荐使用。

     e) 不足，如果没有源码，就无法运用 annotation，只能使用 xml。

     f) 按类型时遇到多个相同类型时，使用类似 Antowired 也可以使用 @Qualifier

 4. @Component (spring标准)

     a) 初始化的名字默认为类名首字母小写

     b) 可以指定初始化 bean 的名字

 5. @Scope

 6. @PostConstruct = init-method; @PreDestroy = destroy-method;