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 { @Bean
public 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 @Configuration class. You cannot declare inter-bean dependencies using plain @Component classes.

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

@Configuration
public class AppConfig { @Bean
public Foo foo() {
return new Foo(bar());
} @Bean
public 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的Java配置方式—@Configuration和@Bean实现Java配置

    Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...

  3. @Configuration与@Bean作用

    Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...

  4. Spring @Configuration 和 @Bean 注解

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

  5. @Configuration和@Bean的用法和理解

    spring Boot提倡约定优于配置,如何将类的生命周期交给spring 1.第一种自己写的类,Controller,Service. 用@controller @service即可 2.第二种,集 ...

  6. @Configuration和@Bean

    @Configuration可理解为用spring的时候xml里面的标签 @Bean可理解为用spring的时候xml里面的标签 Spring Boot不是spring的加强版,所以@Configur ...

  7. 0006SpringBoot中@Configuration与@Bean联合使用

    需求:将某个普通类做为组件注册到容器中,可通过如下办法 1.定义HelloService类 package springboot_test.springboot_test.service; publi ...

  8. @Configuration与@Bean

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

  9. @Configuration结合@Bean实现对象的配置

    @Configuration结合@Bean实现对象的配置 前提:最近项目中需要做支付接口,支付宝以及微信支付,本文并不介绍如何写支付接口,而是通过这个示例讲解配置应该怎么写,项目中使用的是Kotlin ...

随机推荐

  1. MSP430学习笔记:UART

    串通可以两种方法其现 一.USART硬件直接实现 二.通过定时器软件实现 该模块可现现: UART异步串行通讯 SPI同步串行通讯 I2C同步串行通讯 UxCTL控制寄存器 7:PENA 6:PEV ...

  2. Ubuntu 系统密码相关问题

    第一个问题: Ubuntu 密码失效解决办法 拷贝:http://www.myexception.cn/operating-system/1707766.html ubuntu14.04突然不能登录, ...

  3. .net framework 4.0 从 GAC 卸载 程序集

    .net framework 4.0 的 GAC 目录: C:\Windows\Microsoft.NET\assembly\GAC_MSIL 要卸载,仍然使用 gacutil 命令,不要带扩展名: ...

  4. 如果在代码中使用JS

    protected void Page_Load(object sender, EventArgs e)    {        //获得.js文件        string myscript = ...

  5. Mysql逻辑模块组成

    总的来说,MySQL可以看成是二层架构,第一层我们通常叫做SQL Layer,在MySQL数据库系统处理底层数据之前的所有工作都是在这一层完成的,包括权限判断,sql解析,执行计划优化,query c ...

  6. java对象序列化byte[] and byte[]反序列化对象--转

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  7. twig一些常用的用法总结【原创】

    在使用Symphony项目时,需要一些常用的twig,经过自己做的几个项目,自己的总结如下: 一.twig-数据判断 有时候在使用后台传给前台数据时需要判断是否有这个值,(是否为空(”或null)或是 ...

  8. Redis部分数据结构方法小结

    package com.practice.util; import java.util.HashMap; import java.util.List; import java.util.Map; im ...

  9. Makefile中的特殊宏定义以及实用选项

    Makefile中的一些特殊宏定义的名字跟shell中的位置变量挺相似的. $?    当前目标所依赖的文件列表中比当前目标文件还要新的文件 $@   当前目标我名字 $<   当前依赖文件的名 ...

  10. cocos2dx loading界面 预加载资源 与 资源释放

    预加载图片: 1.CCTextureCache::sharedTextureCache()->addImage("icon.png"); 2.CCTextureCache:: ...