spring的bean注释

Spring @Bean Annotation is applied on a method to specify that it returns a bean to be managed by Spring context. Spring Bean annotation is usually declared in Configuration classes methods. In this case, bean methods may reference other @Bean methods in the same class by calling them directly.

Spring @Bean Annotation应用于方法,以指定它返回要由Spring上下文管理的bean。 Spring Bean批注通常在Configuration类方法中声明。 在这种情况下,bean方法可以通过直接调用它们来引用同一类中的其他@Bean方法。

Spring @Bean示例 (Spring @Bean Example)

Let’s say we have a simple class as below.

假设我们有一个简单的类,如下所示。

package com.journaldev.spring;public class MyDAOBean {@Overridepublic String toString() {return "MyDAOBean"+this.hashCode();}
}

Here is a Configuration class where we have defined a @Bean method for MyDAOBean class.

这是一个Configuration类,其中我们为MyDAOBean类定义了@Bean方法。

package com.journaldev.spring;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class MyAppConfiguration {@Beanpublic MyDAOBean getMyDAOBean() {return new MyDAOBean();}
}

We can get MyDAOBean bean from Spring context using below code snippet.

我们可以使用下面的代码片段从Spring上下文中获取MyDAOBean bean。

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.scan("com.journaldev.spring");
context.refresh();//Getting Bean by Class
MyDAOBean myDAOBean = context.getBean(MyDAOBean.class);

Spring Bean名称 (Spring Bean Name)

We can specify the @Bean name and use it to get them from spring context. Let’s say we have MyFileSystemBean class defined as:

我们可以指定@Bean名称,并使用它从spring上下文中获取它们。 假设我们将MyFileSystemBean类定义为:

package com.journaldev.spring;public class MyFileSystemBean {@Overridepublic String toString() {return "MyFileSystemBean"+this.hashCode();}public void init() {System.out.println("init method called");}public void destroy() {System.out.println("destroy method called");}
}

Now define a @Bean method in the configuration class:

现在在配置类中定义一个@Bean方法:

@Bean(name= {"getMyFileSystemBean","MyFileSystemBean"})
public MyFileSystemBean getMyFileSystemBean() {return new MyFileSystemBean();
}

We can get this bean from context by using the bean name.

我们可以使用bean名称从上下文中获取此bean。

MyFileSystemBean myFileSystemBean = (MyFileSystemBean) context.getBean("getMyFileSystemBean");MyFileSystemBean myFileSystemBean1 = (MyFileSystemBean) context.getBean("MyFileSystemBean");

Spring @Bean initMethod和destroyMethod (Spring @Bean initMethod and destroyMethod)

We can also specify spring bean init method and destroy method. These methods are called when spring bean is being created and when the context is being closed respectively.

我们还可以指定spring bean的init方法和destroy方法。 当创建spring bean和关闭上下文时分别调用这些方法。

@Bean(name= {"getMyFileSystemBean","MyFileSystemBean"}, initMethod="init", destroyMethod="destroy")
public MyFileSystemBean getMyFileSystemBean() {return new MyFileSystemBean();
}

You will notice that “init” method is being called when we invoke the context refresh method and “destroy” method is called when we invoke context close method.

您会注意到,当我们调用上下文refresh方法时,将调用“ init”方法,而当我们调用上下文close方法时,将调用“ destroy”方法。

摘要 (Summary)

Spring @Bean annotation is widely used in annotation-driven spring applications.

Spring @Bean注释被广泛用于注释驱动的spring应用程序中。

GitHub Repository.GitHub Repository下载完整的spring项目。

翻译自: https://www.journaldev.com/21577/spring-bean-annotation

spring的bean注释

spring的bean注释_Spring @Bean注释相关推荐

  1. spring 注释_Spring核心注释

    spring 注释 介绍: org.springframework.beans.factory.annotation和org.springframework.context.annotation包中存 ...

  2. spring 注释_Spring@主要注释

    spring 注释 介绍: 当存在多个相同类型的bean时,使用Spring @Primary批注为标记的bean提供更高的优先级. 默认情况下,Spring按类型自动连线. 因此,当Spring尝试 ...

  3. spring 注释_Spring@懒惰注释

    spring 注释 介绍: 默认情况下, Spring框架在应用程序启动时加载并热切初始化所有bean. 在我们的应用程序中,我们可能有一些非常消耗资源的bean. 我们宁愿根据需要加载此类bean. ...

  4. spring的bean范围_Spring Bean范围

    spring的bean范围 介绍: Spring核心容器实例化bean并管理其生命周期. 在定义bean时,我们可以提供其范围. 除非明确提供,否则单例是Spring容器中Bean的默认范围. Spr ...

  5. spring 注释_Spring @Value注释

    spring 注释 介绍: Spring @Value批注用于将值注入变量和方法参数. 我们可以读取spring环境变量或系统变量. 它还支持SpEL. 在本快速教程中,我们将探讨如何使用Spring ...

  6. spring注释_Spring注释

    spring注释 Spring Annotations allows us to configure dependencies and implement dependency injection t ...

  7. spring bean作用域_Spring面试知识点,这是我见过最全面的 - 知识铺

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停地来唤醒记忆深处的知识点. Q1.什么是Spring框架? Spring是最流行的企业应用程序框架之一.Spring ...

  8. java spring bean配置文件_Spring基于xml文件配置Bean过程详解

    这篇文章主要介绍了spring基于xml文件配置Bean过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 通过全类名来配置: class:be ...

  9. Spring之使用注解实例化Bean并注入属性

    1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...

最新文章

  1. knn闽南语是什么意思_小丑竟是我自己是什么意思梗 小丑竟是我自己bgm是什么...
  2. 使用Docker搭建Elasticsearch集群环境
  3. SDK用DIAPI连接SBO的实现
  4. 搭建K8s集群(平台规划和部署方式介绍)
  5. PowerTip of the Day from powershell.com上周汇总(六)
  6. MaxCompute非事务表如何更新数据
  7. 山东大学 2020级数据库系统 实验六
  8. 说三件切身利益的大事!一定要看 减少损失
  9. Hadoop集群部署权限总结
  10. smartdns使用指南_SmartDNS配合某插件进阶上网使用教程(基于N1盒子Op系统)
  11. 微信开发者工具的tabBar和数据双向绑定
  12. 用树莓派制作智能小车
  13. 自学c语言要下载什么软件下载,你学c语言用的什么app?
  14. Matlab 并行代码
  15. 极路由2(HC5761)免云平台开启SSH
  16. dxdiag是什么 dxdiag命令怎么用
  17. 对CentOS服务器上正使用MRTG进行移机操作
  18. Gate 7.2的安装与部署(一)
  19. 社区版emqx安装后修改登入到dashboard密码 http://ip:18083/
  20. 11126 *输出a与b中的较大值

热门文章

  1. [Regionals 2012 :: Asia - Tokyo ]
  2. WPF--ContextMenu绑定命令的一个问题
  3. warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
  4. [译] 通过官网 Go 语言学习笔记 | How to Write Go Code
  5. mysql cmmand not found
  6. IntelliJ IDEA(三、各种工程的创建 -- 之二 -- 创建一个JavaWeb工程)
  7. React Native常用第三方汇总
  8. sublime基本命令和使用
  9. JavaScript事件冒泡和事件委托
  10. 国内一些SCM相关论坛站点