Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, it can be useful to perform such a context reload while testing your app.</p> <p>You can try the <code>refresh</code> method of one of the <code>AbstractRefreshableApplicationContext</code> class: it won't refresh your previously instanciated beans, but next call on the context will return refreshed beans.</p> <pre><code>import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.context.support.FileSystemXmlApplicationContext; public class ReloadSpringContext { final static String header = "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n" + "&lt;!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\"\n" + " \t\"http://www.springframework.org/dtd/spring-beans.dtd\"&gt;\n"; final static String contextA = "&lt;beans&gt;&lt;bean id=\"test\" class=\"java.lang.String\"&gt;\n" + "\t\t&lt;constructor-arg value=\"fromContextA\"/&gt;\n" + "&lt;/bean&gt;&lt;/beans&gt;"; final static String contextB = "&lt;beans&gt;&lt;bean id=\"test\" class=\"java.lang.String\"&gt;\n" + "\t\t&lt;constructor-arg value=\"fromContextB\"/&gt;\n" + "&lt;/bean&gt;&lt;/beans&gt;"; public static void main(String[] args) throws IOException { //create a single context file final File contextFile = File.createTempFile("testSpringContext", ".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 the bean 'test' on stdout System.out.println(context.getBean("test")); //write the second context into it FileUtils.writeStringToFile(contextFile, header + contextB); //refresh the context context.refresh(); //echo the bean 'test' on stdout System.out.println(context.getBean("test")); } } </code></pre> <p>And you get this result</p> <pre><code>fromContextA fromContextB </code></pre> <p>Another way to achieve this (and maybe a more simple one) is to use the Refreshable Bean feature of Spring 2.5+ With dynamic language (groovy, etc) and spring you can even change your bean behavior. Have a look to the <a href="http://static.springsource.org/spring/docs/2.5.x/reference/dynamic-language.html" rel="nofollow noreferrer">spring reference for dynamic language</a>:</p> <blockquote> <p>24.3.1.2. Refreshable beans</p> <p>One of the (if not the) most compelling value adds of the dynamic language support in Spring is the 'refreshable bean' feature.</p> <p>A refreshable bean is a dynamic-language-backed bean that with a small amount of configuration, a dynamic-language-backed bean can monitor changes in its underlying source file resource, and then reload itself when the dynamic language source file is changed (for example when a developer edits and saves changes to the file on the filesystem).</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload