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:

拦截器没有执行:

相关推荐
夏贰四18 小时前
管控数据加载调度如何规避任务冲突?数据加载运维监控体系该如何搭建?
java·大数据·运维
山甫aa18 小时前
JavaWeb后端开发学习手册
java·开发语言·数据库·学习·mysql·springboot·web
zlinear数据采集卡18 小时前
数据采集卡从入门到精通(42):项目实战三——设备预测性维护系统,从布点到预警
开发语言·单片机·嵌入式硬件·安全·fpga开发
csdn_aspnet18 小时前
C# 高效便利的处理数据
开发语言·windows·c#
鱼鳞_18 小时前
热门八股-JVM
java·jvm
ly768918 小时前
XML 从入门到实践:语法、命名空间、XPath、XSD 与 Java 安全解析
xml·java·python
Chester_199918 小时前
CSP202209.B何以包邮?
c语言·开发语言
长谷深风11118 小时前
好的 Tool Schema,不是字段越全越好
java·大数据·人工智能·ai agent·agent工作流·智能体设计·ai产品设计
IT 行者18 小时前
用 HiddenHttpMethodFilter 解决浏览器不支持 PUT/DELETE/PATCH 的问题
java·spring
晚安code18 小时前
Redis 内存淘汰策略实战:什么时候淘汰、怎么选、怎么防缓存击穿
java·redis·缓存