Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring引导-在IOC容器中创建作为单例的Bean_Java_Spring_Spring Boot_Ioc Container - Fatal编程技术网

Java Spring引导-在IOC容器中创建作为单例的Bean

Java Spring引导-在IOC容器中创建作为单例的Bean,java,spring,spring-boot,ioc-container,Java,Spring,Spring Boot,Ioc Container,Bean创建应该是spring容器中的一个单例,对吗?我正在将一个Spring配置文件迁移到带有注释的Spring引导。我有下面的代码,但它似乎每次在另一个bean创建中使用mySingletonBean时都会调用它。我的理解是@Bean注释在默认情况下应该是单例的。我创建的bean是否正确 @Bean public SomeBean mySingletonBean() { SomeBean mybean = new SomeBean(); mybean.setName = "Name"

Bean创建应该是spring容器中的一个单例,对吗?我正在将一个Spring配置文件迁移到带有注释的Spring引导。我有下面的代码,但它似乎每次在另一个bean创建中使用mySingletonBean时都会调用它。我的理解是@Bean注释在默认情况下应该是单例的。我创建的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;
}



SpringBoot是一个智能框架,mySingletonBean方法只会启动一次,不用担心。您可以启动调试模式并查看。

Spring正在代理您的应用程序上下文类,并负责所有与上下文相关的内容,如bean的实例化、缓存等

运行此小测试,随时调试以查看配置类变成了什么类:

package stackoverflow;

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;
        }
    }

}

方法调用将在@Configuration中代理。类和结果在同一个实例中。然而,这只适用于@Configuration类,而不适用于`@组件类。你的问题让我有点困惑。您是否观察到正在创建的SomeBean的两个不同实例,或者您是否担心调用该方法可能/将导致重复的bean创建?