简体   繁体   English

在Spring中是否有任何内置的线程在操作DefaultNamespaceHandlerResolver?

[英]Is there any built-in thread manipulating DefaultNamespaceHandlerResolver in Spring?

I am debugging an application using Spring. 我正在使用Spring调试应用程序。 I found something strange When I step into below constructor in DefaultNamespaceHandlerResolver which is called by XmlBeanDefinitionReader. 当我进入由XmlBeanDefinitionReader调用的DefaultNamespaceHandlerResolver中的以下构造函数时,发现了一些奇怪的东西。 createReaderContext(Resource resource) : createReaderContext(资源):

public DefaultNamespaceHandlerResolver(ClassLoader classLoader, String handlerMappingsLocation) {
        Assert.notNull(handlerMappingsLocation, "Handler mappings location must not be null");
        this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());
        this.handlerMappingsLocation = handlerMappingsLocation;
    }

There's a field named handlerMappings in DefaultNamespaceHandlerResolver as below: 在DefaultNamespaceHandlerResolver中有一个名为handlerMappings的字段,如下所示:

private volatile Map<String, Object> handlerMappings;

After I run through above constructor, this field's value will be changed, however,I can't find any clue how its value change, because only classloader and handlerMappingLocation are set in above construtor. 在运行完上面的构造函数之后,该字段的值将被更改,但是,我找不到任何线索来更改其值,因为在上面的构造函数中仅设置了classloader和handlerMappingLocation。 I noticed that handlerMappings has been declared as volatile. 我注意到handlerMappings已声明为volatile。 So my question is ,is there any built-in thread or other thread manipulate this filed in Spring? 所以我的问题是,在Spring中是否有任何内置线程或其他线程操纵此文件?

Also, this confuses me, I think this is no built-in thread or other thread manipulate this field in Spring. 另外,这让我感到困惑,我认为这不是内置线程或其他线程在Spring中操纵此字段。

I find that there is toString() method as follows( here ) which will execute the getHandlerMappings() method. 我发现有如下的toString()方法( 在此 )将执行getHandlerMappings()方法。

@Override
public String toString() {
    return "NamespaceHandlerResolver using mappings " + getHandlerMappings();
}

When we debug in IntelliJ or Eclipse, I think the IDE will execute the toString() method in another thread to show the human readable value of the object. 当我们在IntelliJ或Eclipse中进行调试时,我认为IDE将在另一个线程中执行toString()方法以显示该对象的人类可读值。

Here is the test code: 这是测试代码:

public class ToStringTest {

    public static void main(String[] args) {

        WilltoStringInvoked will = new WilltoStringInvoked();

        // #breakpoint1
        System.out.println("If breakpoint here value will be 1");

        /**
         * If we set an breakpoint before this method the output will 
         * be 1, otherwise the output will be 0
         */
        System.out.println(will.getValue());

        // #breakpoint2
        System.out.println("If breakpoint here value will be 0");
    }

    static class WilltoStringInvoked {

        private volatile  int value = 0;

        private int setValue() {
           if (value == 0) {
               synchronized (this) {
                   if (value == 0) {
                       value = 1;
                   }
               }
           }

           return value;
        }

        public int getValue() {
            return value;
        }

        @Override
        public String toString() {
            return "This value is: " + setValue();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM