java 去掉 if else_Java 通过注解消除if else

半夜睡醒出门吃宵夜回家锁坏了,被逼无奈去了网吧正好想起之前构思的消除ifelse的方案正好就试了试,经过几小时奋斗修修改改终于实现了效果,特此分享.

具体的流程如上

大概的场景是 在 web项目中有很多方法是要携带token或其他的操作才可以访问的,一开始只有一个token还好业务繁忙的时候每次都要修改拦截器中的代码十分麻烦.

首先还是新建一个web项目然后自己再写一个拦截器(这里用的是springboot构建的项目)

配置拦截器

@Configuration

public class WebMvcConfig implements WebMvcConfigurer {

@Override

public void addInterceptors(InterceptorRegistry registry) {

// 添加全局 拦截器 interceptor registry.addInterceptor(authenticationInterceptor())

.addPathPatterns("/**");

}

@Bean

public AuthenticationInterceptor authenticationInterceptor() {

return new AuthenticationInterceptor();

}

}

拦截器

public class AuthenticationInterceptor implements HandlerInterceptor {

@Override

public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

HandlerMethod handlerMethod = (HandlerMethod) handler;

Method method = handlerMethod.getMethod();

BaseAnnotation methodAnno = method.getAnnotation(BaseAnnotation.class);

String value = methodAnno.value();

System.out.println(value);

BaseBean bean = SpringUtil.getBean(value,BaseBean.class);

System.out.println(request.getRequestURI());

bean.say();

return true;

}

@Override

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,

ModelAndView modelAndView) {

}

}

测试用的注解

@Target({ElementType.METHOD,ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

public @interface BaseAnnotation {

public String value() default "";

}

接口

public interface BaseBean {

public void say();

}

接口实现类(两个直接写一起了)

@Component

public class BeanA implements BaseBean{

public void say() {

System.out.println( "这是BeanA的say");

}

}

@Component

public class BeanB implements BaseBean{

public void say() {

System.out.println( "这是BeanB的say");

}

}

测试用的控制器

@RestController

@RequestMapping("/test")

public class TestController {

@RequestMapping("/a")

@BaseAnnotation("beanA")

public String testA() {

return "a";

}

@RequestMapping("/b")

@BaseAnnotation("beanB")

public String testB() {

return "b";

}

}

运行结果

localhost:8080/test/a

localhost:8080/test/b

拦截器中用到了一个工具类 SpringUtil 主要作用是用来获取Spring容器中的bean

工具类

@Component

public class SpringUtil implements ApplicationContextAware {

private static ApplicationContext applicationContext;

@Override

public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

if(SpringUtil.applicationContext == null) {

SpringUtil.applicationContext = applicationContext;

}

}

//获取applicationContext public static ApplicationContext getApplicationContext() {

return applicationContext;

}

//通过name获取 Bean. public static Object getBean(String name){

return getApplicationContext().getBean(name);

}

//通过class获取Bean. public static T getBean(Class clazz){

return getApplicationContext().getBean(clazz);

}

//通过name,以及Clazz返回指定的Bean public static T getBean(String name,Class clazz){

return getApplicationContext().getBean(name, clazz);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值