1.@Autowired注释用法

1.@Autowired 应用于构造函数(多个构造方法时必须有一个构造方法被@Autowired注释)
public class MovieRecommender {private final CustomerPreferenceDao customerPreferenceDao;@Autowiredpublic MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {this.customerPreferenceDao = customerPreferenceDao;}}2.@autowired 应用于Setter方法
public class SimpleMovieLister {private MovieFinder movieFinder;@Autowiredpublic void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder;}
}3.@Autowired注释应用于具有任意名称和/或多个参数的方法
public class MovieRecommender {private MovieCatalog movieCatalog;private CustomerPreferenceDao customerPreferenceDao;@Autowiredpublic void prepare(MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao) {this.movieCatalog = movieCatalog;this.customerPreferenceDao = customerPreferenceDao;}}
4.@Autowired注释运用于字段,甚至与构造函数混合:
public class MovieRecommender {private final CustomerPreferenceDao customerPreferenceDao;@Autowiredprivate MovieCatalog movieCatalog;@Autowiredpublic MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {this.customerPreferenceDao = customerPreferenceDao;}}5.将@Autowired注释添加到需要该类型数组的字段或方法中,还可以从applicationContext中提供特定类型的所有bean。
public class MovieRecommender {@Autowiredprivate MovieCatalog[] movieCatalogs;}6.@Autowired注释运用集合
public class MovieRecommender {private Set<MovieCatalog> movieCatalogs;@Autowiredpublic void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {this.movieCatalogs = movieCatalogs;}}

2.@Qualifier注释用法

1.当你创建多个具有相同类型的 bean 时,并且想要用一个属性只为它们其中的一个进行装配,在这种情况下你可以使用@Qualifier 注释和 @Autowired 注释通过指定哪一个真正的 bean 将会被装配来消除混乱public class MovieRecommender {@Autowired@Qualifier("main")private MovieCatalog movieCatalog;}2.@Qualifier注解也可以在单独的构造函数参数或方法参数上指定public class MovieRecommender {private MovieCatalog movieCatalog;private CustomerPreferenceDao customerPreferenceDao;@Autowiredpublic void prepare(@Qualifier("main")MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao) {this.movieCatalog = movieCatalog;this.customerPreferenceDao = customerPreferenceDao;}}3.您可以创建自己的自定义限定符注释。只需定义一个注释并在定义中提供@Qualifier注释
@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Genre {String value();
}

3.@Required注释用法

1.@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配 置文件中,
否则容器就会抛出一个 BeanInitializationException 异常.public class SimpleMovieLister {private MovieFinder movieFinder;@Requiredpublic void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder;}}

4.@Resource注释用法

1.@Resource 注释可以在字段中或者 setter 方法中使用.@Resource 注释使用一个 ‘name’ 属性,该属性以
一个 bean 名称的形式被注入。你可以说,它遵循 by-name 自动连接语义
public class SimpleMovieLister {private MovieFinder movieFinder;@Resource(name="myMovieFinder")public void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder;}
}2.@Resource 注释可以在字段中或者 setter 方法中使用.如果不使用name属性,将采用默认的字段名,所以下面的示例将
把名为“moviefinder”的bean注入其setter方法中
public class SimpleMovieLister {private MovieFinder movieFinder;@Resourcepublic void setMovieFinder(MovieFinder movieFinder) {this.movieFinder = movieFinder;}
}

5.@ComponentScan和@Configuration注释用法

1.要自动检测这些类并注册相应的bean,需要将@componentscan添加到@configuration类中,其中basepackages属性是
这两个类的通用父包。(或者,可以指定包含每个类的父包的逗号/分号/空格分隔列表。)带@Bean注释的方法可以有任意数量的参数来描述构建该bean所需的依赖关系。例如,如果我们的TransferService需要
一个AccountRepository,我们可以通过一个方法参数来实现依赖关系:@Configuration
@ComponentScan(basePackages = "org.example")
public class AppConfig {@Beanpublic TransferService transferService(AccountRepository accountRepository) {return new TransferServiceImpl(accountRepository);}
}2.@Bean注释支持指定任意初始化和销毁回调方法,很像Spring XML在bean元素上的init-method和destroy-method属性
public class Foo {public void init() {// initialization logic}
}public class Bar {public void cleanup() {// destruction logic}
}@Configuration
public class AppConfig {@Bean(initMethod = "init")public Foo foo() {return new Foo();}@Bean(destroyMethod = "cleanup")public Bar bar() {return new Bar();}
}3.您可以指定使用@Bean注释定义的bean应该具有特定的范围。您可以使用Bean作用域部分中指定的任何标准作用域。默认范围是单例的,但是您可以使用@Scope注释覆盖它
@Configuration
public class MyConfiguration {@Bean@Scope("prototype")public Encryptor encryptor() {// ...}
}

6.@Pointcut

1.切入点表达式可以使用“&&”、“||”和“!”组合
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {}@Pointcut("within(com.xyz.someapp.trading..*)")
private void inTrading() {}@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {}

Spring注释详解相关推荐

  1. Spring JDBC详解

    <Spring JDBC详解> 本文旨在讲述Spring JDBC模块的用法.Spring JDBC模块是Spring框架的基础模块之一. 一.概述 在Spring JDBC模块中,所有的 ...

  2. [转载]Spring配置文件详解一:

    2019独角兽企业重金招聘Python工程师标准>>> 原文地址:Spring配置文件详解一:<context:annotation-config/>与<conte ...

  3. Spring入门详解

    typora-copy-images-to: upload Spring入门详解 Spring框架是Java开发中最常用的框架,功能非常强大 源码下载:Spring Framework jar包.文档 ...

  4. Spring AOP详解(转载)所需要的包

    上一篇文章中,<Spring Aop详解(转载)>里的代码都可以运行,只是包比较多,中间缺少了几个相应的包,根据报错,几经百度搜索,终于补全了所有包. 截图如下: 在主测试类里面,有人怀疑 ...

  5. Spring 体系结构详解

    Spring 体系结构详解 核心容器(Core Container) Core和Beans模块提供了Spring最基础的功能,提供IOC和依赖注入特性.这里的基础概念是BeanFactory,它提供对 ...

  6. struts2+hibernate+spring配置详解

    #struts2+hibernate+spring配置详解 struts2+hibernate+spring配置详解 哎 ,当初一个人做好难,现在终于弄好了,希望自学这个的能少走些弯路. 以下是自己配 ...

  7. Java代码中的注释详解

    2019独角兽企业重金招聘Python工程师标准>>> java注释详解 声明:本文系JavaEye网站发布的原创博客文章,未经作者书面许可,严禁任何网站转载本文,否则必将追究法律责 ...

  8. JScript中的条件注释详解(转载自网络)

    JScript中的条件注释详解-转载 这篇文章主要介绍了JScript中的条件注释详解,本文讲解了@cc_on.@if.@set.@_win32.@_win16.@_mac等条件注释语句及可用于条件编 ...

  9. 史上最详细的Pytorch版yolov3代码中文注释详解(四)

    史上最详细的Pytorch版yolov3代码中文注释详解(一):https://blog.csdn.net/qq_34199326/article/details/84072505 史上最详细的Pyt ...

最新文章

  1. VC CListCtrl 第一列列宽自适应
  2. tcpdump + wireshark 抓包组合
  3. C++之多态性与虚函数
  4. 改善ASP.NET2.0性能的五件法宝
  5. 数据结构——从叶子结点到根节点的全部路径
  6. 解决mybatis generator无法覆盖XML
  7. 《01》ECMAScript 6 简介
  8. 关于我的代码在课上第一时间没有运行出来这件事
  9. python+selenium 爬携程机票价格
  10. 堆排序c语言6,C语言:十种排序(七) - 堆排序
  11. 回炉重造-数据结构之数组列表
  12. pb 执行存储过程带参数_SQL高级知识——存储过程
  13. JVM虚拟机之二 堆内存
  14. ArcGIS道路网拓扑检查
  15. OP-TEE 编译流程
  16. Redis布隆过滤器和布谷鸟过滤器
  17. Classic Shell 4.2.4 中文版已经发布
  18. 2021跨年夜表白脱单情话句子 零点跨年夜表白成功文案说说
  19. 家谱只能记家族好事吗?家丑不可外扬,后人读谱一脸懵?
  20. Ordering类-greatestOf

热门文章

  1. 深度学习voc数据集图片resize
  2. cookie版购物车
  3. SQLite B+树实现代码
  4. Linux实验报告常用工具开发,Linux基础入门第六节实验报告
  5. web服务器网站网速慢的原因,apache配置优化 - 解决apache环境下网站访问速度慢的问题...
  6. mysql查阅某个日期的语句_mysql查询指定日期时间内的sql语句及原理
  7. php中empty功能,在php中empty函数起什么作用呢?
  8. android自定义模态框,安卓开发自定义弹出框的简单方式(纯代码布局)
  9. access如何查询两张表的内容_为什么可以的话,不要使用星号 *,而是相应的字段名来进行查询 MySQL内连接如何选择驱动表
  10. 如何利用计算机网络辅助自己,如何借助计算机网络辅助大学英语写作教学.doc...