• @Bean 的用法


    @Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名

    定义bean

    下面是@Configuration里的一个例子

    1 @Configuration
    2 public class AppConfig {
    3 
    4     @Bean
    5     public TransferService transferService() {
    6         return new TransferServiceImpl();
    7     }
    8 }

    这个配置就等同于之前在xml里的配置

    1 <beans>
    2     <bean id="transferService" class="com.acme.TransferServiceImpl"/>
    3 </beans>

    bean的依赖

    @bean 也可以依赖其他任意数量的bean,如果TransferService 依赖 AccountRepository,我们可以通过方法参数实现这个依赖

    1 @Configuration
    2 public class AppConfig {
    3     @Bean
    4     public TransferService transferService(AccountRepository accountRepository) {
    5         return new TransferServiceImpl(accountRepository);
    6     }
    7 }

    接受生命周期的回调

    任何使用@Bean定义的bean,也可以执行生命周期的回调函数,类似@PostConstruct and @PreDestroy的方法。用法如下

     1 public class Foo {
     2     public void init() {
     3         // initialization logic
     4     }
     5 }
     6 
     7 public class Bar {
     8     public void cleanup() {
     9         // destruction logic
    10     }
    11 }
    12 
    13 @Configuration
    14 public class AppConfig {
    15 
    16     @Bean(initMethod = "init")
    17     public Foo foo() {
    18         return new Foo();
    19     }
    20 
    21     @Bean(destroyMethod = "cleanup")
    22     public Bar bar() {
    23         return new Bar();
    24     }
    25 }

    默认使用javaConfig配置的bean,如果存在close或者shutdown方法,则在bean销毁时会自动执行该方法,如果你不想执行该方法,则添加@Bean(destroyMethod="")来防止出发销毁方法

    指定bean的scope

    使用@Scope注解

    你能够使用@Scope注解来指定使用@Bean定义的bean

    1 @Configuration
    2 public class MyConfiguration {
    3 
    4     @Bean
    5     @Scope("prototype")
    6     public Encryptor encryptor() {
    7         // ...
    8     }
    9 }

    自定义bean的命名

    默认情况下bean的名称和方法名称相同,你也可以使用name属性来指定

    1 @Configuration
    2 public class AppConfig {
    3 
    4     @Bean(name = "myFoo")
    5     public Foo foo() {
    6         return new Foo();
    7     }
    8 }

    bean的别名

    bean的命名支持别名,使用方法如下

    1 @Configuration
    2 public class AppConfig {
    3 
    4     @Bean(name = { "dataSource", "subsystemA-dataSource", "subsystemB-dataSource" })
    5     public DataSource dataSource() {
    6         // instantiate, configure and return DataSource bean...
    7     }
    8 }

    bean的描述

    有时候提供bean的详细信息也是很有用的,bean的描述可以使用 @Description来提供

    1 @Configuration
    2 public class AppConfig {
    3 
    4     @Bean
    5     @Description("Provides a basic example of a bean")
    6     public Foo foo() {
    7         return new Foo();
    8     }
    9 }
  • 相关阅读:
    1096 Consecutive Factors (20分)
    js-实现省市区地址选择器三级联动
    js-面向对象编程基础
    js-滑块拼图登录验证
    js-promise以及async、await实现简易红绿灯
    js-前端分页效果的实现
    js-ajax方法详解以及封装
    js-jsonp跨域请求原理以及jsonp的封装
    js-实现常见的拖拽效果(表单滑块验证)
    js本地存储-localStorage和cookie详解以及区别
  • 原文地址:https://www.cnblogs.com/fpqi/p/9633877.html
Copyright © 2020-2023  润新知