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应用程序中。
翻译自: https://www.journaldev.com/21577/spring-bean-annotation
spring的bean注释