1. @Bean:

1.1 定义

从定义可以看出,@Bean只能用于注解方法和注解的定义。


@Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)


1.2 spring文档中对 @Bean的说明

The @Bean annotation is used to indicate that a method instantiates, configures and initializes a new object to be managed by the Spring IoC container.

For those familiar with Spring’s <beans/> XML configuration the @Bean annotation plays the same role as the <bean/>element.

用@Bean注解的方法:会实例化、配置并初始化一个新的对象,这个对象会由spring IoC 容器管理。

实例:

@Configuration
public class AppConfig {@Beanpublic MyService myService() {return new MyServiceImpl();}}

相当于在 XML 文件中配置

<beans><bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>

1.3 生成对象的名字:默认情况下用@Bean注解的方法名作为对象的名字。但是可以用 name属性定义对象的名字,而且还可以使用name为对象起多个名字。

@Configuration
public class AppConfig {@Bean(name = "myFoo")public Foo foo() {return new Foo();}}

@Configuration
public class AppConfig {@Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })public DataSource dataSource() {// instantiate, configure and return DataSource bean...}}

1.4 @Bean 一般和 @Component或者@Configuration 一起使用。

@Component和@Configuration不同之处

(1)This method of declaring inter-bean dependencies only works when the @Bean method is declared within a @Configurationclass. You cannot declare inter-bean dependencies using plain @Component classes.

在 @Component 注解的类中不能定义 类内依赖的@Bean注解的方法。@Configuration可以。

@Configuration
public class AppConfig {@Beanpublic Foo foo() {return new Foo(bar());}@Beanpublic Bar bar() {return new Bar();}}

2. @Configuration:

2.1 定义

从定义看,用于注解类、接口、枚举、注解的定义。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)

2.2 spring 文档说明

@Configuration is a class-level annotation indicating that an object is a source of bean definitions. @Configuration classes declare beans via public @Bean annotated methods.

@Configuration用于类,表明这个类是beans定义的源。

@Configuration 和 @Bean相关推荐

  1. Springboot@Configuration和@Bean详解

    Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  2. Spring@Configuration、@Bean、@Import 、@ImportResource、@DependsOn 说明

    为什么80%的码农都做不了架构师?>>>    使用spring的这几个配置,可以将旧<xml>配置形式完全使用Java实现,也可以和<xml>嵌套使用. @ ...

  3. 【Spring注解系列01】@Configuration与@Bean

    1. @Configuration与@Bean @Configuration: 告诉Spring这是一个配置类,配置类==配置文件. @Configuration==beans.xml @Bean: ...

  4. java中配置bean_Spring中基于Java的配置@Configuration和@Bean用法

    Spring中为了减少xml中配置,可以声明一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: xmlns:xsi=" ...

  5. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

  6. @Configuration和@Bean注解详解

    本文来详细说下@Configuration和@Bean注解 文章目录 @Configuration注解 @Bean注解 @Configuration注解 @Configuration注解源码 pack ...

  7. Spring学习笔记——@Configuration和@Bean注解

    前言 在Spring框架中,有两种对容器的配置和启动方式,一种是基于XML文档的配置方式,一种则是"零配置",即Java Based Configuration. 而在零配置中,最 ...

  8. Spring中基于Java的配置@Configuration和@Bean用法

    spring中为了减少xml中配置,可以生命一个配置类(例如SpringConfig)来对bean进行配置. 一.首先,需要xml中进行少量的配置来启动Java配置: [java] view plai ...

  9. 1spring注解:@Configuration,@Bean,@ComponentScan(),@Scope

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...

最新文章

  1. 禅道8.2.4 腾讯云迁移至VM
  2. 时间序列预测——深度好文,ARIMA是最难用的(数据预处理过程不适合工业应用),线性回归模型简单适用,预测趋势很不错,xgboost的话,不太适合趋势预测,如果数据平稳也可以使用。...
  3. 关于 Head First SQL 中文版
  4. 设置ComboBox控件的边框颜色.
  5. 360浏览器打不开qq空间_网页突然打不开其他软件却能上网?怎么解决?
  6. JSON.parse()、eval()和JSON.stringify()
  7. Mac OS defaults命令(Access the Mac OS user defaults system)
  8. Python 学习笔记 - 协程
  9. CICD详解(一)——概念和原理
  10. margin:0 auto和text-aglin:center的区别
  11. USB转TTL设备驱动不能使用
  12. linux 下竟有想大白菜软件,大白菜U盘做PE及CDLINUX镜像共存的启动盘.pdf
  13. 敏捷开发中如何进行团队绩效管理
  14. 【爬虫】IP代理池的总结、实现与维护,IP代理池小工具(小框架),自建代理ip池
  15. 单片机中动态数码管闪烁问题
  16. Unity Prefabs 预设体
  17. 接入植物识别功能快速识别万种植物花卉
  18. 百度AI战疫五十天:三场战役与一次胜利
  19. 正则表达式匹配居民身份证
  20. 【解决阿里云服务器提示挖矿程序风险2022】

热门文章

  1. C语言以递归求斐波那契数列(附完整源码)
  2. c语言二进制转十进制(附完整源码)
  3. C++实现插入排序(附完整源码)
  4. python list去重_Python中实用却不常见的小技巧
  5. mysql的基本的查询语句_Mysql的基本查询语句
  6. 30.jvm.gc(GC之详解CMS收集过程和日志分析)
  7. 1线程同步:互斥量,死锁
  8. dynamic-insert=true dynamic-update=true,插入默认值的情况
  9. composer搭建php框架,用 Composer构建自己的 PHP 框架之基础准备
  10. bat怎么获取前一天 的日期_bat脚本 得到前一天的日期