我继续学习Spring,很难弄清楚ApplicationContext的实现方式.我是独立的J2EE应用程序,我对Web *或Portlet *实现不感兴趣.
您能否向我提供可能性的简要列表(如果不清楚,请参阅我的问题的P.S.部分)以及下面每个实现的目的:
> ResourceAdapterApplicationContext
> StaticApplicationContext
> ClassPathXmlApplicationContext
> FileSystemApplicationContext
附:
A不要求你提供给我的文档参考.例如:
ClassPathXmlApplicationContext Standalone XML application context,
taking the context definition files from the class path, interpreting
plain paths as class path resource names that include the package path
但是从该定义来看,不清楚ClassPathXmlApplicationContext是否也实现了AbstractRefreshableApplicationContext,并且可以在不停止服务器的情况下用于更改bean定义.
解决方法:
对不起,您不希望引用文档,但这就是所有信息的所在.
StaticApplicationContext说明
org.springframework.context.ApplicationContext implementation which
supports programmatic registration of beans and messages, rather than
reading bean definitions from external onfiguration sources. Mainly
useful for testing.
所以你用它来直接注册bean定义
StaticApplicationContext context = new StaticApplicationContext();
context.registerBeanDefinition(beanName, beanDefinition);
这可以在需要动态更改ApplicationContext的情况下使用.请注意,如果您需要这两种行为,则可以将父ApplicationContext传递给StaticApplicationContext,即.从XML / Java配置读取并动态注册.
在我看来,ClassPathXmlApplicationContext是更常见的ApplicationContext实现之一.您只需将其指向类路径上的XML(bean定义)资源,然后将其加载即可. javadoc说
Useful for test harnesses as well as for application contexts embedded
within JARs.
因此,您可以简单地指向来自JAR的类路径上的资源并加载它.以这种方式设置测试环境就足够了.
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("some-context.xml");
// boom you're ready to go
请注意,Spring的JUnit支持类提供了其他(更好)的方法来设置测试环境.
But from that definition its not clear that
ClassPathXmlApplicationContext also implements
AbstractRefreshableApplicationContext and can be used to change beans
definition without stopping server.
这就是javadoc的用途.
FileSystemXmlApplicationContext类似于上面的ClasspathXmlApplicationContext,但它从文件系统获取配置文件,而不是从类路径中读取资源.
ResourceAdapterApplicationContext指出
org.springframework.context.ApplicationContext implementation for a
JCA ResourceAdapter. Needs to be initialized with the JCA
javax.resource.spi.BootstrapContext, passing it on to Spring-managed
beans that implement BootstrapContextAware.
我根本没有使用过这个,我不知道资源适配器在哪里有用,但是here are some more docs.
标签:java,spring,java-ee
来源: https://codeday.me/bug/20190722/1507669.html