简体   繁体   English

非实用静态方法

[英]non utility static methods

I have a method which extract the loggedin user details from springsecuritycontext object. 我有一个从springsecuritycontext对象中提取登录用户详细信息的方法。

I have read that only utility methods(which does certain calculation) should be static . 我已经读过,只有实用程序方法(进行某些计算)才应该是static。

Here is my method , it doesn't seems to be a utility method but I don't find any reason why I shouldn't make it static as I am using it in multiple beans 这是我的方法,它似乎不是实用程序方法,但是我找不到任何原因,因为我在多个bean中使用它时不应该使其变为静态

public static int getSignedUpUser()
    {
        final SecurityContext ctx = SecurityContextHolder.getContext();

        if(ctx != null)
        {
            final Authentication auth = ctx.getAuthentication();

            if(auth != null)
            {
                final Object principal = auth.getPrincipal();


                if(principal instanceof AUser)
                {
                    final AUser au = (AUser)principal;
                    return au.getId();
                }
            }
        }

        return 0;
    }
}

For short: use static method is OK. 简而言之:使用静态方法就可以了。

When we say static methods should be a utility method, we are talking about that a static method should be thread-safe. 当我们说静态方法应该是一种实用方法时,我们在说静态方法应该是线程安全的。

Let's see the SecurityContextHelper.getContext() method. 让我们看看SecurityContextHelper.getContext()方法。 It is implemented like this: 它是这样实现的:

private static SecurityContextHolderStrategy strategy;

public static SecurityContext getContext() {
    return strategy.getContext();
}

Notice that it returns context from a static variable strategy . 注意,它从静态变量strategy返回上下文。 So the strategy must keep thread-safe. 因此,该strategy必须保持线程安全。

SecurityContextHolderStrategy interface have three implementations: SecurityContextHolderStrategy接口具有三个实现:

在此处输入图片说明

two of them are thread local, the other one has a private static SecurityContext contextHolder; 其中两个是线程本地的,另一个是private static SecurityContext contextHolder;

Then let's see SecurityContextHolder.initialize() method: 然后让我们看看SecurityContextHolder.initialize()方法:

private static void initialize() {
    if ((strategyName == null) || "".equals(strategyName)) {
        // Set default
        strategyName = MODE_THREADLOCAL;
    }

    if (strategyName.equals(MODE_THREADLOCAL)) {
        strategy = new ThreadLocalSecurityContextHolderStrategy();
    } else if (strategyName.equals(MODE_INHERITABLETHREADLOCAL)) {
        strategy = new InheritableThreadLocalSecurityContextHolderStrategy();
    } else if (strategyName.equals(MODE_GLOBAL)) {
        strategy = new GlobalSecurityContextHolderStrategy();
    } else {
        // Try to load a custom strategy
        try {
            Class<?> clazz = Class.forName(strategyName);
            Constructor<?> customStrategy = clazz.getConstructor();
            strategy = (SecurityContextHolderStrategy) customStrategy.newInstance();
        } catch (Exception ex) {
            ReflectionUtils.handleReflectionException(ex);
        }
    }

    initializeCount++;
}

This shows that MODE_THREADLOCAL is the default strategy. 这表明MODE_THREADLOCAL是默认策略。 And even GlobalSecurityContextHolderStrategy uses a static context holder too. 甚至GlobalSecurityContextHolderStrategy也使用静态上下文持有者。 So you can use them in static method. 因此,您可以在静态方法中使用它们。

conceptually the method impl shown is a part of instance of a class. 从概念上讲,所示的impl方法是类实例的一部分。 that's why manipulations can be static. 这就是为什么操纵可以是静态的。 so i prefer to use the instane rather tahn static methhod. 所以我更喜欢使用instane而不是tahn静态方法。

You are not asking the question the right way. 您不是以正确的方式问这个问题。 If you look at the method of getSignedUpUser , you see the class it is part of. 如果查看getSignedUpUser的方法, getSignedUpUser看到它所属的类。 Your question should be this: 您的问题应该是这样的:

Who gets the signed up user? 谁获得注册用户? An instance, or all the instances collectively (the class)? 一个实例,还是所有实例共同(类)?

static members or methods are collective members or methods. static成员或方法是集体成员或方法。 For example, the number of birds is a collective data about birds, it is static . 例如,鸟类数量是关于鸟类的集体数据,它是static Non-static members or methods are individual. 非静态成员或方法是个体的。 For example, a bird has a weight, which is associated individually to the bird. 例如,鸟具有一个重量,该重量分别与该鸟相关联。

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

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