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:

拦截器没有执行:

相关推荐
d***95627 分钟前
springboot接入deepseek深度求索 java
java·spring boot·后端
小白学大数据20 分钟前
基于Splash的搜狗图片动态页面渲染爬取实战指南
开发语言·爬虫·python
xlq2232221 分钟前
22.多态(下)
开发语言·c++·算法
CoderYanger26 分钟前
C.滑动窗口-越短越合法/求最长/最大——2958. 最多 K 个重复元素的最长子数组
java·数据结构·算法·leetcode·哈希算法·1024程序员节
洞窝技术34 分钟前
Redis 4.0 升级至 5.0 实施手册
java·redis
无代码专家1 小时前
设备巡检数字化解决方案:构建高效闭环管理体系
java·大数据·人工智能
未来之窗软件服务1 小时前
操作系统应用(三十三)php版本选择系统—东方仙盟筑基期
开发语言·php·仙盟创梦ide·东方仙盟·服务器推荐
是Dream呀1 小时前
昇腾实战|算子模板库Catlass与CANN生态适配
开发语言·人工智能·python·华为
tanxiaomi1 小时前
Redisson分布式锁 和 乐观锁的使用场景
java·分布式·mysql·面试
零匠学堂20251 小时前
移动学习系统,如何提升企业培训效果?
java·开发语言·spring boot·学习·音视频