11 Must‑Know Spring Extension Points to Supercharge Your Java Backend
Explore the 11 most commonly used Spring extension points—from custom interceptors and bean factories to global exception handling, type converters, import mechanisms, startup runners, bean lifecycle processors, and custom scopes—complete with clear code examples and practical guidance for enhancing Java backend applications.
11. Custom Scope
Define a custom scope by implementing Scope (e.g., thread‑local scope) and register it via a BeanFactoryPostProcessor.
public class ThreadLocalScope implements Scope {
private static final ThreadLocal<Object> THREAD_LOCAL_SCOPE = new ThreadLocal<>();
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
Object value = THREAD_LOCAL_SCOPE.get();
if (value != null) {
return value;
}
Object object = objectFactory.getObject();
THREAD_LOCAL_SCOPE.set(object);
return object;
}
@Override
public Object remove(String name) {
THREAD_LOCAL_SCOPE.remove();
return null;
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {}
@Override
public Object resolveContextualObject(String key) { return null; }
@Override
public String getConversationId() { return null; }
} @Component
public class ThreadLocalBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
beanFactory.registerScope("threadLocalScope", new ThreadLocalScope());
}
} @Scope("threadLocalScope")
@Service
public class CService {
public void add() {}
}Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Su San Talks Tech
Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
