Spring @Bean注解与示例

x33g5p2x  于2022-10-06 转载在 Spring  
字(3.8k)|赞(0)|评价(0)|浏览(1057)

在这篇文章中,我们将讨论基于Spring Java配置的@Bean注解和例子。我们还将讨论如何以及何时使用@Bean注解的不同场景。

@Bean注解概述

  • @Bean是一个方法级注解,是XML元素**<bean />的直接类似物。该注解支持一些所提供的属性,如init-method, destroy-method, autowiring 和 name**。
    *你可以在@Configuration-annotated或@Component-annotated类中使用@Bean注解。
    下图显示了@Bean注解的一个内部实现。

声明一个Bean和例子

要声明一个Bean,只需用@Bean注解来注解一个方法。你可以用这个方法在**应用环境中注册一个Bean定义,该类型被指定为该方法的返回值。 

默认情况下,豆的名字将与方法的名字相同。 
下面是一个@Bean方法声明的简单例子。

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import com.companyname.projectname.customer.CustomerService;
  4. import com.companyname.projectname.order.OrderService;
  5. @Configuration
  6. public class Application {
  7. @Bean
  8. public CustomerService customerService() {
  9. return new CustomerService();
  10. }
  11. @Bean
  12. public OrderService orderService() {
  13. return new OrderService();
  14. }
  15. }

前面的配置完全等同于下面的Spring XML。

  1. <beans>
  2. <bean id="customerService" class="com.companyname.projectname.CustomerService"/>
  3. <bean id="orderService" class="com.companyname.projectname.OrderService"/>
  4. </beans>

注意,XML中的方法名和Bean名是完全一样的。

让我们来看看使用@Bean注解的不同场景。

Bean的依赖性

一个@Bean注解的方法可以有任意数量的参数,描述构建该Bean所需的依赖关系。例如,如果我们的CustomerController需要一个CustomerServicew,我们可以通过一个方法参数来实现这一依赖关系。

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import com.companyname.projectname.customer.CustomerController;
  4. import com.companyname.projectname.customer.CustomerService;
  5. @Configuration
  6. public class Application {
  7. private CustomerService customerService;
  8. @Bean
  9. public CustomerService customerService() {
  10. customerService = new CustomerService();
  11. return customerService;
  12. }
  13. @Bean
  14. public CustomerController customerController(CustomerService customerService) {
  15. return new CustomerController(customerService);
  16. }
  17. }

解析机制与基于构造器的依赖注入基本相同。

Bean生命周期方法

@Bean注解支持指定任意的初始化销毁回调方法,就像Spring XML在bean元素上的init-methoddestroy-method属性。

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

###使用@Scope注解来指定Bean范围

你可以指定你用@Bean注解定义的Bean应该有一个特定的范围。你可以使用Bean Scopes中指定的任何标准作用域。默认的作用域是singleton,但你可以用@Scope注解来覆盖它。

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.context.annotation.Scope;
  4. import com.companyname.projectname.customer.CustomerService;
  5. import com.companyname.projectname.order.OrderService;
  6. @Configuration
  7. public class Application {
  8. @Bean
  9. @Scope("prototype")
  10. public CustomerService customerService() {
  11. return new CustomerService();
  12. }
  13. @Bean
  14. @Scope("prototype")
  15. public OrderService orderService() {
  16. return new OrderService();
  17. }
  18. }

###自定义Bean命名

默认情况下,配置类使用@Bean方法的名称作为结果Bean的名称。然而,这个功能可以通过name属性来重写。

  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import com.companyname.projectname.customer.CustomerService;
  4. import com.companyname.projectname.order.OrderService;
  5. @Configuration
  6. public class Application {
  7. @Bean(name = "cService")
  8. public CustomerService customerService() {
  9. return new CustomerService();
  10. }
  11. @Bean(name = "oService")
  12. public OrderService orderService() {
  13. return new OrderService();
  14. }
  15. }

###Bean aliasing

正如在命名Bean中所讨论的那样,有时需要给一个Bean起多个名字,也就是所谓的bean aliasing@Bean注解的name属性接受一个String数组来实现这一目的。

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

注入bean间的依赖关系

@Bean相互依赖时,表达这种依赖就像让一个bean方法调用另一个一样简单。

  1. @Configuration
  2. public class AppConfig {
  3. @Bean
  4. public Foo foo() {
  5. return new Foo(bar());
  6. }
  7. @Bean
  8. public Bar bar() {
  9. return new Bar();
  10. }
  11. }

这就是@Bean注解的全部内容。

相关文章