简体   繁体   中英

non utility static methods

I have a method which extract the loggedin user details from springsecuritycontext object.

I have read that only utility methods(which does certain calculation) should be 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

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. 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 . So the strategy must keep thread-safe.

SecurityContextHolderStrategy interface have three implementations:

在此处输入图片说明

two of them are thread local, the other one has a private static SecurityContext contextHolder;

Then let's see SecurityContextHolder.initialize() method:

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. And even GlobalSecurityContextHolderStrategy uses a static context holder too. So you can use them in static method.

conceptually the method impl shown is a part of instance of a class. that's why manipulations can be static. so i prefer to use the instane rather tahn static methhod.

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. 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. For example, the number of birds is a collective data about birds, it is static . Non-static members or methods are individual. For example, a bird has a weight, which is associated individually to the bird.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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