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();
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