ComnandLineRunner接口, ApplcationRunner接口

ComnandLineRunner接口, ApplcationRunner接口

介绍:

这两个接口都有一个run方法,执行时间在容器对象创建好后,自动执行run ( )方法。 创建容器的同时会创建容器中的对象,同时会把容器中的对象的属性赋上值

举例:
java 复制代码
@SpringBootApplication
public class Application implements CommandLineRunner {
    @Resource
    private HelloService helloService;

    public static void main(String[] args){
        System.out.println("准备创建容器对象1111111");
        SpringApplication.run(Application.class,args);
        System.out.println("容器对象创建好之后33333");
    }
    @Override
    public void run(String... args) throws Exception {
        String str = helloService.hello("李四");
        System.out.println("调用容器中的对象:"+str);
        //可以自定义操作,如读取文件、数据库等
        System.out.println("在容器对象创建好后,执行run方法222222");
    }
}
java 复制代码
public interface HelloService {
    String hello(String name);
}
java 复制代码
@Service
public class HelloServiceImpl implements HelloService {
    @Override
    public String hello(String name) {
        return "您好:"+name;
    }
}
执行结果:

拦截器

拦截器是SpringMVC中一种对象,能拦截器对Controller的请求。

拦截器框架中有系统的拦截器,还可以自定义拦截器。实现对请求预先处理。

拦截器的使用步骤:

1、创建类实现Handlerlnterceptor接口:根据需要重写其中的preHandle()、postHandle()、afterCompletion()

2、自定义类来实现WebMvcConfigurer接口,在配置类中声明拦截器对象以及要拦截的请求路径

我们以前是写xml文件,现在是把springmvc配置文件中的配置移到了这个接口的方法中,很多和springmvc有关的功能都是在这个接口中实现的:

举例:
java 复制代码
//handLer被拦截器的控制器对象
//true:请求能被Controller处理
//false:请求被截断
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("HandlerInterceptor接口中的preHandle()方法");
        return true;
    }
}
java 复制代码
@Configuration //一定要加这个注解!
public class MyConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        HandlerInterceptor interceptor = new LoginInterceptor();
        String[] path = {"/user/**"};
        String[] excludePath = {"/user/login"};
        registry.addInterceptor(interceptor)
                .addPathPatterns(path)
                .excludePathPatterns(excludePath);
    }
}
java 复制代码
@Controller
public class InterceptorController {
    @RequestMapping("/user/account")
    @ResponseBody
    public String userAccount(){
        return "访问的是/user/account地址";
    }

    @ResponseBody
    @RequestMapping("/user/login")
    public String userLogin(){
        return "访问的是/user/login地址";
    }
}

http://localhost:8081/mydev/user/account

拦截器执行了:

http://localhost:8081/mydev/user/login:

拦截器没有执行:

相关推荐
清水白石0081 分钟前
深入解析 LRU 缓存:从 `@lru_cache` 到手动实现的完整指南
java·python·spring·缓存
林开落L2 分钟前
从零开始学习Protobuf(C++实战版)
开发语言·c++·学习·protobuffer·结构化数据序列化机制
牛奔6 分钟前
Go 是如何做抢占式调度的?
开发语言·后端·golang
符哥200814 分钟前
C++ 进阶知识点整理
java·开发语言·jvm
小猪咪piggy14 分钟前
【Python】(4) 列表和元组
开发语言·python
Sayuanni%326 分钟前
初阶_多线程1(线程含义与关键属性)
java
程序媛徐师姐27 分钟前
Java基于微信小程序的模拟考试系统,附源码+文档说明
java·微信小程序·java模拟考试系统小程序·模拟考试微信小程序·模拟考试系统小程序·模拟考试小程序·java模拟考试小程序
難釋懷27 分钟前
Lua脚本解决多条命令原子性问题
开发语言·lua
CoderCodingNo35 分钟前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法
疯狂敲代码的老刘36 分钟前
JDK 1.6到25 全版本网盘合集 (Windows + Mac + Linux)
java·linux·windows·macos·jdk