java - Spring 与 Guice 实例化对象

标签 java spring dependency-injection guice

我最近开始学习Spring。作为 Spring 的新手,我想到了几个问题。其中之一是:

如此处所述“一旦容器加载 spring 配置,所有 bean 都会被实例化。org.springframework.context.ApplicationContext 容器遵循预加载方法。” LINK

1 - 这是否意味着使用 Spring ApplicationContext 创建的所有对象都是单例?

我创建了这个简单的测试

@Component
public class HelloService {

 private ApplicationContext context;


 public HelloService() {
 }

 @Autowired
 public HelloService(ApplicationContext context) {
  this.context = context;
 }

 public String sayHello() {

  return "Hi";
 }

}

public class HelloApp {
 public static void main(String[] args) {


  ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  Injector injector = Guice.createInjector(new AbstractModule() {
   @Override
   protected void configure() {
   }
  });
  HelloService helloService1 = context.getBean(HelloService.class);
  System.out.println(helloService1);
  HelloService helloService2 = context.getBean(HelloService.class);
  System.out.println(helloService2);

  HelloService helloService3 = injector.getInstance(HelloService.class);
  System.out.println(helloService3);
  HelloService helloService4 = injector.getInstance(HelloService.class);
  System.out.println(helloService4);

 }
}

输出是

foo.bar.HelloService@191e8b08 // same instance 
foo.bar.HelloService@191e8b08 // same instance

foo.bar.HelloService@6ba67ab5 // different instance 
foo.bar.HelloService@7ec23849 // different instance

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  <context:annotation-config/>
  <context:component-scan base-package="foo.bar"/>
</beans> // this is mu config bean. 

在 Guice 中,您必须明确表示您希望将此对象实例化为单例。

2- 当 Spring 创建保持某种状态的对象时,这不会产生一些问题吗?

3-如何告诉Spring创建一个新对象?

最佳答案

1 - Does this mean that all objects created with Spring ApplicationContext are Singletons ?

没有。但 Spring 的默认范围是 singleton 。如果您想要不同的范围,则必须在 bean 配置中显式声明它。在 Java 配置中,您可以使用 @Scope 来完成此操作。注解。通过 XML 配置,您可以使用 <bean> 来完成此操作。 scope属性。除其他方式外...

2- Doesn't this create some problems when Spring create objects that keep some state ?

您始终可以声明不同的范围,所以不需要。

3- How to tell Spring to create a new object ?

这取决于范围。如果范围是prototype ,然后调用ApplicationContext#getBean(String)每次都会给你一个新的实例。如果你的bean是singleton ,那么您将始终获得相同的实例。

请注意,您可以拥有多个相同类型但作用域不同的 bean。例如,

<bean name="my-proto" class="com.example.Example" scope="prototype" />
<bean name="my-singleton" class="com.example.Example" /> <!-- defaults to singleton -->

及以后

(Example) context.getBean("my-proto"); // new instance every time
(Example) context.getBean("my-singleton"); // same instance every time

因此,您可以在某些情况下使用单例,而在其他情况下使用不同的范围。另外,您不必在任何地方都使用 Spring。

关于java - Spring 与 Guice 实例化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24318385/

相关文章:

java - 使用 Spring RestTemplate 对对象进行 POST 参数

http - 如何使用 angular2/http 进行 ajax 调用?

javascript - Angular 模块注入(inject)错误

asp.net-core - ASP .NET Entity Framework Core 无法访问已释放的对象

Java CDI @Inject 抛出 NullPointerException

java - JSF + primefaces 多次调用 getter

java - JPasswordField 转为字符串但无法比较

java - ThreadPoolExecutor 的 getActiveCount()

java - 打印机在服务器而不是客户端打印

java - 运行junit时出现空指针异常