spring知识回顾

文章目录
  1. 1. 拦截器利用了反射对请求进行自定义的操作可以访问action上下文和栈的对象,保证数据
  2. 2. 过滤器利用原始servlet来进行操作主要可用于鉴权和
  3. 3. AOP
  4. 4. 不加载特定的bean
  5. 5. stream 自定义function

spring的利用的面向切面编程(AOP),动态代理(反射)和filter,intercept,

ApplicationContent

1
2
3
Qualifier
Roucese
Autowired

拦截器利用了反射对请求进行自定义的操作可以访问action上下文和栈的对象,保证数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
public class MyInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

System.out.println("Interceptor 前置");
return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

System.out.println("Interceptor 处理中");
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

System.out.println("Interceptor 后置");
}
}

过滤器利用原始servlet来进行操作主要可用于鉴权和

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Component
public class MyFilter implements Filter {

@Override
public void init(FilterConfig filterConfig) throws ServletException {

System.out.println("Filter 前置");
}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

System.out.println("Filter 处理中");
filterChain.doFilter(servletRequest, servletResponse);
}

@Override
public void destroy() {

System.out.println("Filter 后置");
}
}

AOP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
@Aspect
@Component
public class Aspect {
@Pointcut("@annotation(com.HasAuth)")
public void pointCut() {

}

@Around("pointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
Object[] args = point.getArgs();
/* if (StringUtil.IsEmpty(args[0])) {
args[0] = "default";
}*/
return point.proceed(args);
}
//标注该方法体为后置通知,当目标方法执行成功后执行该方法体
@AfterReturning("within(com.abchina.irms..*) && @annotation(rl)")
public void addLogSuccess(JoinPoint jp, rmpfLog rl){
Object[] parames = jp.getArgs();//获取目标方法体参数
String params = parseParames(parames); //解析目标方法体的参数
String className = jp.getTarget().getClass().toString();//获取目标类名
className = className.substring(className.indexOf("com"));
String signature = jp.getSignature().toString();//获取目标方法签名
String methodName = signature.substring(signature.lastIndexOf(".")+1, signature.indexOf("("));
String modelName = getModelName(className); //根据类名获取所属的模块

...
}

//标注该方法体为异常通知,当目标方法出现异常时,执行该方法体
@AfterThrowing(pointcut="within(com.abchina.irms..*) && @annotation(rl)", throwing="ex")
public void addLog(JoinPoint jp, rmpfLog rl, BusinessException ex){
...
}
}

不加载特定的bean

1
2
3
4
5
@Configuration @EnableSpringConfigured
@ComponentScan(basePackages = {"com.example"},
excludeFilters={@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.xxx.yyy.*"),
@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value=Foo.class)})
public class MySpringConfiguration {}

stream 自定义function