要捕获特定的URL模式并在应用程序范围的bean中执行某些操作,您可以使用Spring MVC的拦截器(Interceptor)。
首先,您需要创建一个实现HandlerInterceptor接口的拦截器类。在这个类中,您可以根据需要执行要执行的操作。下面是一个简单的示例:
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在请求处理之前执行操作,例如日志记录或验证
        String url = request.getRequestURI();
        if (url.contains("/admin")) {
            // 执行特定的操作
        }
        return true; // 返回true继续处理请求,返回false取消请求
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        // 在请求处理之后执行操作,例如修改ModelAndView
    }
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在请求完成之后执行操作,例如清理资源
    }
}
接下来,您需要将拦截器注册到Spring MVC配置文件中。可以通过实现WebMvcConfigurer接口来进行配置。下面是一个简单的示例:
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor())
                .addPathPatterns("/**"); // 指定要拦截的URL模式,例如"/*"表示拦截所有URL
    }
}
在上面的示例中,我们将MyInterceptor拦截器注册到所有URL模式中。
现在,当请求匹配到指定的URL模式时,MyInterceptor中的操作将被执行。
请注意,以上示例假设您正在使用Spring MVC框架。如果您使用的是其他框架,例如Spring Boot或Servlet API,则可能需要进行一些调整。