注解

这里讲的注解有下面几个

  • @Autowired

  • @Qualifier(" ")

  • @Genre(" ")

  • @Offline

  • @Resource(name=" ")

  • @PostConstruct

  • @PreDestroy

    基础的配置文件,要写配置文件可以在下面写,这个文件是通用的。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6. xmlns:jee="http://www.springframework.org/schema/jee"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xmlns:aop="http://www.springframework.org/schema/aop"
  9. xmlns:mvc="http://www.springframework.org/schema/mvc"
  10. xmlns:util="http://www.springframework.org/schema/util"
  11. xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  12. xsi:schemaLocation="
  13. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  14. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  15. http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  16. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  17. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  18. http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  19. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  20. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  21. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  22. <!-- 在这里写配置文件的内容 -->
  23. </beans>

@Autowired

@Autowired 注解

可以用于“传统的”setter 方法

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Autowired
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. // ...
  8. }

用于以属性为参数/多个参数的方法

  1. public class MovieRecommender {
  2. private MovieCatalog movieCatalog;
  3. private CustomerPreferenceDao customerPreferenceDao;
  4. @Autowired
  5. public void prepare(MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
  6. this.movieCatalog = movieCatalog;
  7. this.customerPreferenceDao = customerPreferenceDao;
  8. }
  9. // ...
  10. }

用于构造器与字段

  1. public class MovieRecommender {
  2. @Autowired
  3. private MovieCatalog movieCatalog;
  4. private CustomerPreferenceDao customerPreferenceDao;
  5. @Autowired
  6. public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
  7. this.customerPreferenceDao = customerPreferenceDao;
  8. }
  9. // ...
  10. }

ApplicationContext

  • 也可以是一种提供来自ApplicationContext的特殊类型的所有 beans,注解字段或者方法
  1. public class MovieRecommender {
  2. @Autowired
  3. private MovieCatalog[] movieCatalogs;
  4. // ...
  5. }

- 用于集合类型

  1. public class MovieRecommender {
  2. private Set<MovieCatalog> movieCatalogs;
  3. @Autowired
  4. public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
  5. this.movieCatalogs = movieCatalogs;
  6. }
  7. // ...
  8. }

Maps

  • Maps 也可以这样注解,只要这个 Map 的 key 类型为 String。这个 Map 的 values 应该是已知的类型,并且 keys 应该包含符合 bean 的命名
  1. public class MovieRecommender {
  2. private Map<String, MovieCatalog> movieCatalogs;
  3. @Autowired
  4. public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
  5. this.movieCatalogs = movieCatalogs;
  6. }
  7. // ...
  8. }

缺省情况

  在缺省情况下,当出现0个候选的 beans时自动连接将失败;缺省行为把连接方法,构造器,字段假设为 required 的依赖。

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Autowired(required=false)
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. // ...
  8. }

  虽然当 一个类只有一个连接构造器时它将被标记为 required, 但是还是可以标记多个构造器的。在这种情况下,每一个构造器都有可能被认为是连接构造器, Spring 将会把依赖关系能够满足的构造器认为是greediest 的构造器。

@Qualifier注解

  因为通过类型的自动连接可能会有多个候选,因此经常需要在选择过程中加以控制。一种方法去完成这个控制就是使用@Qualifier注解。

  1. public class MovieRecommender {
  2. @Autowired
  3. @Qualifier("mainCatalog")
  4. private MovieCatalog movieCatalog;
  5. // ...
  6. }

@Qualifier注解也能够被指定为构造器的参数或者方法的参数

  1. public class MovieRecommender {
  2. private MovieCatalog movieCatalog;
  3. private CustomerPreferenceDao customerPreferenceDao;
  4. @Autowired
  5. public void prepare(@Qualifier("mainCatalog") MovieCatalog movieCatalog, CustomerPreferenceDao customerPreferenceDao) {
  6. this.movieCatalog = movieCatalog;
  7. this.customerPreferenceDao = customerPreferenceDao;
  8. }
  9. // ...
  10. }

创建您自定义的限定器注解。

定义一个注解时提供@Qualifier注解

  1. @Target({ElementType.FIELD, ElementType.PARAMETER})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Qualifier
  4. public @interface Genre {
  5. String value();
  6. }

然后将这个自定义的限定器与参数用于自动连接的字段

  1. public class MovieRecommender {
  2. @Autowired
  3. @Genre("Action")
  4. private MovieCatalog actionCatalog;
  5. private MovieCatalog comedyCatalog;
  6. @Autowired
  7. public void setComedyCatalog(@Genre("Comedy") MovieCatalog comedyCatalog) {
  8. this.comedyCatalog = comedyCatalog;
  9. }
  10. // ...
  11. }

提供信息给候选的 bean 的定义

添加标签作为标签的子元素,然后指定'type'还有'value'以匹配您的自定义限定器注解。类型必须匹配注解的全名,或者是一个不危险的、方便一点的名字,您也可以使用“短” 类名。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <context:annotation-config/>
  9. <bean class="example.SimpleMovieCatalog">
  10. <qualifier type="Genre" value="Action"/>
  11. <!-- inject any dependencies required by this bean -->
  12. </bean>
  13. <bean class="example.SimpleMovieCatalog">
  14. <qualifier type="example.Genre" value="Comedy"/>
  15. <!-- inject any dependencies required by this bean -->
  16. </bean>
  17. <bean id="movieRecommender" class="example.MovieRecommender"/>
  18. </beans>

@Resource ##a

  Spring 也提供了使用 JSR-250 bean 属性支持的注射方式。

  对于Spring 托管的对象 Spring 可以以这种方式支持映射

  @Resource有一个‘name’属性,缺省时,Spring 将这个值解释为要注射的 bean 的名字。换句话说,如果遵循by-name的语法

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Resource(name="myMovieFinder")
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. }

  如果没有显式地给出名字,缺省的名字将继承于字段名或者 setter 方法名:如果是字段名,它将简化或者等价于字段名;如果是 setter 方法名,它将等价于 bean 属性名。

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Resource
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. }

注解提供的名字将被BeanFactory解析为 bean 名

@PostConstruct 与 @PreDestroy

  从javaee5规范开始,servlet增加了两个影响servlet生命周期的注解(annotation):@PostConstruct 与 @PreDestroy。这两个注解用来修饰一个非静态的void()方法:而且这个方法不能抛出异常声明。

  当一个方法带有这些注解之一时,将被在其生命周期与 Spring 生命周期接口的方法或者显式声明回调方法同一刻上调用。

  1. public class CachingMovieLister {
  2. @PostConstruct
  3. public void someMethod() {
  4. // populates the movie cache upon initialization...
  5. }
  6. @PreDestroy
  7. public void someMethod() {
  8. // clears the movie cache upon destruction...
  9. }
  10. }

@POSTconstruct说明

被@POSTconstruct修饰的方法会在服务器加载servlet的时候运行,并且被服务器调用一次,类似于Servlet的init()方法。被@POSTconstruct修饰的方法会在构造函数之后,init方法之前调用运行。

@preconstruct说明

被@precontruct修饰的方法会在服务器卸载servlet的时候运行,并且只会被服务器调用一次,类似于servlet的destroy()方法。被@precontruct修饰的的方法会在destroy()方法之后运行,在servlet被彻底卸载之前。

spring(四)之基于注解(Annotation-based)的配置.md的更多相关文章

  1. 使用 Spring 2.5 基于注解驱动的 Spring MVC

    http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/ 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sp ...

  2. 使用 Spring 2.5 基于注解驱动的 Spring MVC--转

    概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...

  3. 基于注解的Dubbo服务配置

      基于注解的Dubbo服务配置可以大大减少dubbo xml配置文件中的Service配置量,主要步骤如下:   一.服务提供方   1. Dubbo配置文件中增加Dubbo注解扫描 <!-- ...

  4. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  5. 07 Spring框架 依赖注入(四)基于注解的依赖注入

    前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...

  6. Spring(七)之基于注解配置

    基于注解的配置 从 Spring 2.5 开始就可以使用注解来配置依赖注入.而不是采用 XML 来描述一个 bean 连线,你可以使用相关类,方法或字段声明的注解,将 bean 配置移动到组件类本身. ...

  7. SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

    本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...

  8. Spring MVC中基于注解的 Controller

         终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...

  9. Spring IOC之基于注解的容器配置

    Spring配置中注解比XML更好吗?基于注解的配置的介绍提出的问题是否这种途径比XML更好.简单来说就是视情况而定. 长一点的答案是每一种方法都有自己的长处也不足,而且这个通常取决于开发者决定哪一种 ...

  10. Spring 使用AOP——基于注解配置

    首先,使用注解实现AOP是基于AspectJ方式的. 创建包含切点方法的类 package cn.ganlixin.test; import org.aspectj.lang.annotation.P ...

随机推荐

  1. Best Reward HDU - 3613(马拉车+枚举+前缀和)

    题意: 给你一串字符串,每个字符都有一个权值,要求把这个字符串在某点分开,使之成为两个单独的字符串 如果这两个子串某一个是回文串,则权值为那一个串所有的字符权值和 若不是回文串,则权值为0 解析: 先 ...

  2. 二维RMQ模板

    int main(){ ; i <= n; i++) ; j <= m; j++) { scanf("%d", &val[i][j]); dp[i][j][][ ...

  3. 【转】Oracle 查询库中所有表名、字段名、表名说明、字段名说明

    转自 :http://gis-conquer.blog.sohu.com/170243422.html 查询所有表名:select t.table_name from user_tables t; 查 ...

  4. 【BZOJ4654】【NOI2016】国王饮水记(动态规划,斜率优化)

    [BZOJ4654][NOI2016]国王饮水记(动态规划,斜率优化) 题面 BZOJ 洛谷 题解 首先肯定是找性质. 明确一点,比\(h_1\)小的没有任何意义. 所以我们按照\(h\)排序,那么\ ...

  5. 【BZOJ2141】排队(树套树)

    [BZOJ2141]排队(树套树) 题面 BZOJ 洛谷 题解 傻逼题啊... 裸的树套树 树状数组套线段树,每次交换的时候,考虑一下前后的贡献,先删掉贡献,再重新算一遍就好了.. #include& ...

  6. Middle of Linked List

    Find the middle node of a linked list. Example Given 1->2->3, return the node with value 2. Gi ...

  7. redis分布式(主从复制)

    Redis主从复制配置和使用都非常简单.通过主从复制可以允许多个slave server拥有和master server相同的数据库副本.    Redis的复制原理:本身就是Master发送数据给s ...

  8. acid(数据库事务正确执行的四个基本要素的缩写)

    ACID,指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consistency).隔离性(Isolation).持久性(Durability).一个支持事务(T ...

  9. Python之旅:装饰器

    装饰器就是闭包函数的一种应用场景 一.为何要用装饰器 开放封闭原则:软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案:能够在不修该 ...

  10. HDU 4946 凸包

    给你n个点,具有速度,一个位置如果有其他点能够先到,则不能继续访问,求出里面这些点哪些点是能够无限移动的. 首先我们考虑到,一个速度小的和一个速度大的,速度小的必定只有固定他周围的一定区域是它先到的, ...