Spring 引导 - 在 IOC 容器中将 Bean 创建为单例
Spring Boot - creating Bean as Singleton in IOC container
Bean 创建应该是 spring 容器中的单例,对吗?我正在将 Spring 配置文件迁移到 Spring Boot with Annotations。我有下面的代码,但它似乎每次在另一个 bean 创建中使用它时都会调用 "mySingletonBean()" 。据我了解,@Bean 注释默认情况下应该是 Singleton。我是否正确地创建了我的 Bean?
@Bean
public SomeBean mySingletonBean() {
SomeBean mybean = new SomeBean();
mybean.setName = "Name";
return mybean;
}
@Bean
public Bean1 bean1() {
Bean1 bean1 = new Bean1();
bean1.setBean(mySingletonBean());
return bean1;
}
@Bean
public Bean2 bean2() {
Bean2 bean2 = new Bean2();
bean2.setBean(mySingletonBean());
return bean2;
}
Spring Boot 是智能框架和方法 mySingletonBean()
只会启动一次,不用担心。你可以启动调试模式并查看。
Spring 正在代理您的应用程序上下文 class,并负责所有与上下文相关的事情,例如 bean 的实例化、缓存等。
运行这个小测试,随意调试看看class配置class变成了什么:
package Whosebug;
import java.util.Arrays;
import java.util.Date;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Test.MyContext.class)
public class Test {
@Autowired
private ApplicationContext applicationContext;
@Autowired
@Qualifier("myStringBean")
private String myStringBean;
@Autowired
private SomeBean someBean;
@Configuration
static class MyContext {
@Bean
public String myStringBean() {
return String.valueOf(new Date().getTime());
}
@Bean
public SomeBean mySomeBean() {
return new SomeBean(myStringBean());
}
}
@org.junit.Test
public void test() {
assertTrue(myStringBean == applicationContext.getBean("myStringBean"));
assertTrue(myStringBean == someBean.getValue());
System.out.println(Arrays.asList(applicationContext.getBean("myStringBean"), myStringBean, someBean.getValue()));
}
static class SomeBean {
private String value;
public SomeBean(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}
Bean 创建应该是 spring 容器中的单例,对吗?我正在将 Spring 配置文件迁移到 Spring Boot with Annotations。我有下面的代码,但它似乎每次在另一个 bean 创建中使用它时都会调用 "mySingletonBean()" 。据我了解,@Bean 注释默认情况下应该是 Singleton。我是否正确地创建了我的 Bean?
@Bean
public SomeBean mySingletonBean() {
SomeBean mybean = new SomeBean();
mybean.setName = "Name";
return mybean;
}
@Bean
public Bean1 bean1() {
Bean1 bean1 = new Bean1();
bean1.setBean(mySingletonBean());
return bean1;
}
@Bean
public Bean2 bean2() {
Bean2 bean2 = new Bean2();
bean2.setBean(mySingletonBean());
return bean2;
}
Spring Boot 是智能框架和方法 mySingletonBean()
只会启动一次,不用担心。你可以启动调试模式并查看。
Spring 正在代理您的应用程序上下文 class,并负责所有与上下文相关的事情,例如 bean 的实例化、缓存等。
运行这个小测试,随意调试看看class配置class变成了什么:
package Whosebug;
import java.util.Arrays;
import java.util.Date;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertTrue;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Test.MyContext.class)
public class Test {
@Autowired
private ApplicationContext applicationContext;
@Autowired
@Qualifier("myStringBean")
private String myStringBean;
@Autowired
private SomeBean someBean;
@Configuration
static class MyContext {
@Bean
public String myStringBean() {
return String.valueOf(new Date().getTime());
}
@Bean
public SomeBean mySomeBean() {
return new SomeBean(myStringBean());
}
}
@org.junit.Test
public void test() {
assertTrue(myStringBean == applicationContext.getBean("myStringBean"));
assertTrue(myStringBean == someBean.getValue());
System.out.println(Arrays.asList(applicationContext.getBean("myStringBean"), myStringBean, someBean.getValue()));
}
static class SomeBean {
private String value;
public SomeBean(String value) {
this.value = value;
}
public String getValue() {
return value;
}
}
}