spring的bean注释_Spring @Bean注释

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 {

	@Override
	public 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 {

	@Bean
	public 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 {

	@Override
	public 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注释

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值