Spring @Bean注解与示例

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

在这篇文章中,我们将讨论基于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方法声明的简单例子。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;

@Configuration
public class Application {

 @Bean
 public CustomerService customerService() {
  return new CustomerService();
 }
 
 @Bean
 public OrderService orderService() {
  return new OrderService();
 }
}

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

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

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

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

Bean的依赖性

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.companyname.projectname.customer.CustomerController;
import com.companyname.projectname.customer.CustomerService;

@Configuration
public class Application {

    private CustomerService customerService;  
    @Bean
    public CustomerService customerService() {
        customerService = new CustomerService();
        return customerService;
    }
 
    @Bean
    public CustomerController customerController(CustomerService customerService) {
        return new CustomerController(customerService);
    }
}

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

Bean生命周期方法

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

public class Foo {
        public void init() {
                // initialization logic via xml config
        }
}

public class Bar {
        public void cleanup() {
                // destruction logic via xml config
        }
}

@Configuration
public class AppConfig {

        @Bean(initMethod = "init")
        public Foo foo() {
                return new Foo();
        }

        @Bean(destroyMethod = "cleanup")
        public Bar bar() {
                return new Bar();
        }

}

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

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;

@Configuration
public class Application {

 @Bean
 @Scope("prototype")
 public CustomerService customerService() {
  return new CustomerService();
 }
 
 @Bean
 @Scope("prototype")
 public OrderService orderService() {
  return new OrderService();
 }
}

###自定义Bean命名

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;

@Configuration
public class Application {

 @Bean(name = "cService")
 public CustomerService customerService() {
  return new CustomerService();
 }
 
 @Bean(name = "oService")
 public OrderService orderService() {
  return new OrderService();
 }
}

###Bean aliasing

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

@Configuration
public class AppConfig {

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

}

注入bean间的依赖关系

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

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

        @Bean
        public Bar bar() {
                return new Bar();
        }
}

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

相关文章

微信公众号

最新文章

更多