Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Spring中的新功能:加载应用程序上下文_Spring_Applicationcontext - Fatal编程技术网

Spring中的新功能:加载应用程序上下文

Spring中的新功能:加载应用程序上下文,spring,applicationcontext,Spring,Applicationcontext,我是春天的新手 到目前为止,在我的应用程序中,每次需要使用bean时,我都会加载XML ApplicationContext上下文= 新的ClassPathXmlApplicationContext(“applicationContext.xml”) 因此,在需要加载特定bean的每个类中,我都使用上面的代码行 在效率或正确使用方面,我想知道这是否是正确的使用(我怀疑不是),或者上下文是否应该在每个类需要时作为参数传递 谢谢我假设您正在非web应用程序中使用Spring 如果每次需要检索bean

我是春天的新手

到目前为止,在我的应用程序中,每次需要使用bean时,我都会加载XML

ApplicationContext上下文= 新的ClassPathXmlApplicationContext(“applicationContext.xml”)

因此,在需要加载特定bean的每个类中,我都使用上面的代码行

在效率或正确使用方面,我想知道这是否是正确的使用(我怀疑不是),或者上下文是否应该在每个类需要时作为参数传递


谢谢

我假设您正在非web应用程序中使用Spring

如果每次需要检索bean时都要创建新的应用程序上下文,那么这确实不是正确的解决方案。您应该为每个应用程序创建一次应用程序上下文

因此,解决方案是将应用程序上下文实例传递给需要它的类,或者确保在应用程序中使用相同的实例


当前设置可能遇到的许多问题之一是bean范围问题。Spring有单例bean,但这些bean仅在一个应用程序上下文中是单例的。因此,如果您从两个不同的应用程序上下文中检索一个单独的bean,那么它们将不是同一个实例。其他问题将涉及性能,因为创建应用程序上下文将是一项昂贵的操作。

如果您使用spring,那么您应该在任何地方都使用它。因此,不要到处传递应用程序上下文,而是将每个bean放在其中,让Spring为您连接这些点

简而言之,不要自己调用
new
。取而代之的是向春天要豆子。如果bean具有依赖项,则使用构造函数注入

这样,Spring就可以为您创建所有的bean,将它们连接起来并返回完全工作的实例,而无需担心什么东西是从哪里来的

你还应该阅读有关的文章

相关文章:


只需加载XML文件并创建应用程序上下文对象。只要加载了XML,Spring就会注入所有对象依赖项

所以,您不需要一次又一次地加载或创建应用程序上下文对象。只要用下面的例子检查一下你的控制台,你就会明白了

示例:在main方法中,您只编写这行代码

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

interface IParent {
    public void whoAreYou();
}

class ChildA implements IParent {

    public ChildA (IParent ChildB) {
        super();
        System.err.println("Constructor ChildA ");
        ChildB.whoAreYou();
    }

    public ChildA (){
        System.err.println("Constructor ChildA ");
    }

    @Override
    public void whoAreYou() {
        System.err.println("ChildA ");
    }

}

class ChildB implements IParent {

    public ChildB (){
        System.err.println("Constructor ChildB");
    }

    @Override
    public void whoAreYou() {
        System.err.println("ChildB");

    }
}
XML文件:

<?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-4.3.xsd">


   <context:annotation-config/>

   <bean id="childA" class="com.spring.xmlbased.config.ChildA">
       <constructor-arg>
           <bean id="ChildB" class="com.spring.xmlbased.config.ChildB"/>
       </constructor-arg>
   </bean>
</beans>


如果需要进一步澄清,请告诉我。

显然不是。除非您想遇到性能问题、内存问题、奇怪的事务问题等。如果您需要使用依赖项注入的bean,则只需构造一次它……除非您有特定的理由使用Spring的XML配置,我建议您按照《入门指南》来尝试了解Spring应用程序的外观:在基于Spring的web应用程序环境中,Aaron Digulla是绝对正确的