我读过动态 bean 定义更改。我在一个简单的代码示例中尝试了它(请参阅下面的代码),我发现它在我不想停止服务器但添加/更改 bean 定义的情况下非常有吸引力。
问题:
我读到可以在 StaticApplicationContex 或 BeanPostProcessor 或 BeanFactoryPostProcessor< 的帮助下在运行时实现="" bean="">?那么有什么区别呢?
public class Main {
final static String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<beans xmlns=\"http://www.springframework.org/schema/beans\"\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns:context=\"http://www.springframework.org/schema/context\"\n" +
" 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\">\n" +
" <context:annotation-config />\n" +
" <context:component-scan base-package=\"vbah\"/>";
final static String contextA =
"<bean id=\"test\" class=\"java.lang.String\">\n" +
"\t\t<constructor-arg value=\"fromContextA\"/>\n" +
"</bean></beans>";
final static String contextB =
"<bean id=\"test\" class=\"java.lang.String\">\n" +
"\t\t<constructor-arg value=\"fromContextB\"/>\n" +
"</bean></beans>";
public static void main(String[] args) throws IOException {
//create a single context file
final File contextFile = new File("src/resources/spring-config.xml");
//write the first context into it
FileUtils.writeStringToFile(contextFile, header + contextA);
//create a spring context
FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext(
new String[]{contextFile.getPath()}
);
//echo "fromContextA"
System.out.println(context.getBean("test"));
//write the second context into it
FileUtils.writeStringToFile(contextFile, header + contextB);
//refresh the context
context.refresh();
//echo "fromContextB"
System.out.println(context.getBean("test"));
}
}
编辑:
你能回答下面的问题吗:
BeanPostProcess 允许您通过使用代理包装对象来在运行时修改已经存在的 bean 实例。我说得对吗?AbstractApplicationContext#refresh() 删除所有单例 bean 并重新创建它们。
null 依赖项。我说得对吗?StaticApplicationContext 和 BeanFactoryPostProcessor 都允许我在运行时更改 bean 定义。但是有什么区别,优点/缺点?
AbstractApplicationContext#refresh()、StaticApplicationContext 和 BeanFactoryPostProcessor 之间做一个简短的比较(或用例示例)吗?最佳答案
Is it safe do to so (see code below)?
您必须定义安全。
AbstractApplicationContext#refresh()方法 javadoc 状态
As this is a startup method, it should destroy already created singletons if it fails, to avoid dangling resources. In other words, after invocation of that method, either all or no singletons at all should be instantiated.
基本上,上下文中的每个 bean 都将被销毁,所有对它们的引用都将被删除,使它们成为垃圾收集的候选对象。您需要确保这些 bean 有适当的方法来释放它们可能拥有的任何资源。 There are different ways to do that
DisposableBean界面。 destroy-method属性给你的 <bean>或 @Bean定义。@PreDestroy 注释一个方法.请注意 refresh()通常会急切地刷新您的 ApplicationContext , IE。立即重新实例化所有 bean。发生这种情况时,您可能会注意到您的应用程序速度有所下降。
I've read that it is possible to achieve bean definition change in runtime with help of
StaticApplicationContextorBeanPostProcessororBeanFactoryPostProcessor? So what is the difference?
StaticApplicationContext是 ApplicationContext 之一您自己注册 bean 定义的类。在您的示例中,bean 定义是从您的 XML 文件中解析并在后台注册的。用 StaticApplicationContext , 你使用 registerBeanDefinition(..) 或另一个registerXxx()显式注册 bean 定义的方法。
A BeanFactoryPostProcessor可以访问 BeanFactory正在使用,因此所有已注册的 bean 定义。因此,您可以检索任何 BeanDefinition你想要并修改它。作为 BeanFactoryPostProcess#postProcessBeanFactory(..) 的 javadoc州
All bean definitions will have been loaded, but no beans will have been instantiated yet. This allows for overriding or adding properties even to eager-initializing beans.
您可以在 ApplicationContext 之前更改 bean 定义实际使用它。
最后,一个 BeanPostProcessor不会更改 bean 定义。您可以使用 BeanPostProcessor改变 bean 的创建方式,但底层 BeanDefinition将保持不变。
对于您的编辑(比实际答案更大:))
As I understand BeanPostProcess allow you to modify already existed bean instances at runtime by wrapping the object with proxy. Am I right?
它不仅仅用于代理,您可以对对象做任何您想做的事情:修改它的属性,在其他上下文中注册它,将它设为null。等。这围绕着 bean 定义。
AbstractApplicationContext#refresh()drop all singleton beans and recreate them.But If I want to change the definition of prototype/custom scoped bean? If I've got two beans: A and B. A has reference to B. If I change the bean definition in such way that it doesn't contain definition of B. Than B instances will be destroyed, but new instances won't be created. Than A will get a null dependency. Am I right?
在 ApplicationContext 中,您声明您的 bean 定义。如果您要更改 bean 定义,请在 BeanFactoryPostProcessor 中更改它或者在上下文配置中以不同方式声明它。
对于依赖,如果你销毁B bean 定义,将不会有一个 bean 注入(inject) A Spring 会提示,抛出 NoSuchBeanDefinitionException . bean注入(inject)从不注入(inject)null除非你明确告诉它。
StaticApplicationContextandBeanFactoryPostProcessorboth allow me to change a bean definition in runtime. But what are the difference, pros/cons?
两者的用途完全不同。 StaticApplicationContext是一个 ApplicationContext执行。在这里,您声明 bean 定义。 BeanFactoryPostProcessor用于根据您想要实现的任何条件以任何方式修改这些 bean 定义。
Why Spring has 3 mechanism to achieve the same goal. Can you make a brief comparison (or usecases examples) between
AbstractApplicationContext#refresh(),StaticApplicationContextandBeanFactoryPostProcessorplease.
目标不一样。一个ApplicationContext不同于 BeanFactoryPostProcessor并在上下文生命周期的不同时间发挥作用(请参阅您在上一个问题中得到的漂亮图表)。
我没有适合您的用例。了解以上各项的功能,您就会知道在收到特定要求时何时应用它们。
关于java - Spring上下文动态变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21221125/
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在使用Ruby2.1.1和Rails4.1.0.rc1。当执行railsc时,它被锁定了。使用Ctrl-C停止,我得到以下错误日志:~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`gets':Interruptfrom~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.2/lib/spring/client/run.rb:47:in`verify_server_version'from~/.rvm/gems/ruby-2.1.1/gems/spring-1.1.
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
在启用Rack::Deflater来gzip我的响应主体时偶然发现了一些奇怪的东西。也许我遗漏了一些东西,但启用此功能后,响应被压缩,但是资源的ETag在每个请求上都会发生变化。这会强制应用程序每次都响应,而不是发送304。这在没有启用Rack::Deflater的情况下有效,我已经验证页面源没有改变。我正在运行一个使用thin作为Web服务器的Rails应用程序。Gemfile.lockhttps://gist.github.com/2510816有没有什么方法可以让我从Rack中间件获得更多的输出,这样我就可以看到发生了什么?提前致谢。 最佳答案
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/
HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候
遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg
转自:spring.profiles.active和spring.profiles.include的使用及区别说明下文笔者讲述spring.profiles.active和spring.profiles.include的区别简介说明,如下所示我们都知道,在日常开发中,开发|测试|生产环境都拥有不同的配置信息如:jdbc地址、ip、端口等此时为了避免每次都修改全部信息,我们则可以采用以上的属性处理此类异常spring.profiles.active属性例:配置文件,可使用以下方式定义application-${profile}.properties开发环境配置文件:application-dev