当前位置:实例文章 » JAVA Web实例» [文章]Spring 6.0官方文档示例(23): singleton类型的bean和prototype类型的bean协同工作的方法(二)

Spring 6.0官方文档示例(23): singleton类型的bean和prototype类型的bean协同工作的方法(二)

发布人:shili8 发布时间:2025-03-01 20:35 阅读次数:0

**Spring6.0官方文档示例(23):singleton类型的bean和prototype类型的bean协同工作的方法(二)**

在前一篇文章中,我们讨论了如何让singleton类型的bean和prototype类型的bean协同工作。我们使用了一个简单的例子,展示了如何注入一个singleton类型的bean到一个prototype类型的bean中。

但是,在实际应用中,我们可能需要更复杂的场景。在本篇文章中,我们将讨论如何让多个singleton类型的bean和多个prototype类型的bean协同工作。

**示例代码**

我们将创建一个简单的示例,展示了如何让多个singleton类型的bean和多个prototype类型的bean协同工作。示例代码如下:

java// SingletonBean.javapackage com.example.spring6;

import org.springframework.stereotype.Component;

@Componentpublic class SingletonBean {
 public String sayHello() {
 return "Hello, I'm a singleton bean!";
 }
}


java// PrototypeBean.javapackage com.example.spring6;

import org.springframework.stereotype.Component;

@Componentpublic class PrototypeBean {
 private final SingletonBean singletonBean;

 @Autowired public PrototypeBean(SingletonBean singletonBean) {
 this.singletonBean = singletonBean;
 }

 public String sayHello() {
 return "Hello, I'm a prototype bean! " + singletonBean.sayHello();
 }
}


java// AnotherSingletonBean.javapackage com.example.spring6;

import org.springframework.stereotype.Component;

@Componentpublic class AnotherSingletonBean {
 public String sayGoodbye() {
 return "Goodbye, I'm another singleton bean!";
 }
}


java// AnotherPrototypeBean.javapackage com.example.spring6;

import org.springframework.stereotype.Component;

@Componentpublic class AnotherPrototypeBean {
 private final SingletonBean singletonBean;
 private final AnotherSingletonBean anotherSingletonBean;

 @Autowired public AnotherPrototypeBean(SingletonBean singletonBean, AnotherSingletonBean anotherSingletonBean) {
 this.singletonBean = singletonBean;
 this.anotherSingletonBean = anotherSingletonBean;
 }

 public String sayGoodbye() {
 return "Goodbye, I'm another prototype bean! " + singletonBean.sayHello() + " " + anotherSingletonBean.sayGoodbye();
 }
}


java// Application.javapackage com.example.spring6;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplicationpublic class Application {
 public static void main(String[] args) {
 SpringApplication.run(Application.class, args);
 }
}


**配置文件**

我们需要在application.properties中配置bean的类型。

propertiesspring.main.allow-circular-references=true# Singleton beanspring.bean-definition-profiles.singleton.enabled=true# Prototype beanspring.bean-definition-profiles.prototype.enabled=true


**测试**

我们可以使用Spring Boot Test来测试我们的示例代码。

java// SpringBootTest.javapackage com.example.spring6;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTestpublic class SpringBootTest {
 @Autowired private PrototypeBean prototypeBean;

 @Test public void testPrototypeBean() {
 System.out.println(prototypeBean.sayHello());
 }
}


java// AnotherSpringBootTest.javapackage com.example.spring6;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTestpublic class AnotherSpringBootTest {
 @Autowired private AnotherPrototypeBean anotherPrototypeBean;

 @Test public void testAnotherPrototypeBean() {
 System.out.println(anotherPrototypeBean.sayGoodbye());
 }
}


**结论**

在本篇文章中,我们讨论了如何让多个singleton类型的bean和多个prototype类型的bean协同工作。我们使用了一个简单的示例,展示了如何注入一个singleton类型的bean到一个prototype类型的bean中,并且测试了我们的示例代码。

通过阅读这两篇文章,你应该能够理解如何让singleton类型的bean和prototype类型的bean协同工作,并且能够应用这种知识来解决实际问题。

其他信息

其他资源

Top