- 浏览: 329037 次
- 性别:
- 来自: 天津
文章分类
最新评论
-
xing0029:
这样配置的话 事物不成功啊 还有什么地方需要注意的么 可以加我 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
mengqingyu:
liuxiaolu 写道jotm的我的没有成功,楼主能否帮助一 ...
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
liuxiaolu:
jotm的我的没有成功,楼主能否帮助一下
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器) -
aptech406328627:
求解救,没弄好QQ:1053942353
Spring邮件发送(可带附件,模板,群发,异步发送等功能) -
ghpaas:
web可视化自定义表单推荐使用GForms开发平台(http: ...
在线表单设计器设计原理
Spring扩展点
1.IOC生成类全名
2.转换生成映射的URL大小写
3.参数自定义类型转换
4.自定义异常拦截器
1.IOC生成类全名
<!-- 自动扫描且只扫描@Controller --> <context:component-scan base-package="cn.com.demo.**.controller" name-generator="cn.com.demo.framework.spring.context.annotation.FullNameBeanNameGenerator" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan>
public class FullNameBeanNameGenerator extends AnnotationBeanNameGenerator{ @Override protected String buildDefaultBeanName(BeanDefinition definition) { return definition.getBeanClassName(); } }
2.转换生成映射的URL大小写
<!-- 通过 @Controller 和 @RequestMapping 从url定位到类的方法, 自定义CoC实现, 不要再配置annotation-driven --> <bean class="cn.com.demo.framework.web.springmvc.support.CocRequestMappingHandlerMapping"> <property name="basePackage" value="cn.com.demo" /> </bean>
public class CocRequestMappingHandlerMapping extends RequestMappingHandlerMapping{ /** Controller类的后缀 */ private static final String CONTROLLER_SUFFIX = "Controller"; /** 映射url路径的根 */ private String basePackage; //父类的属性 private boolean useSuffixPatternMatch = true; private boolean useTrailingSlashMatch = true; private final List<String> fileExtensions = new ArrayList<String>(); /** * Look for handler methods in a handler. * @param handler the bean name of a handler or a handler instance */ protected void detectHandlerMethods(final Object handler) { //Assert.notNull(this.basePackage, "must config basePackage!"); super.detectHandlerMethods(handler); } /** * 这个只改了createRequestMappingInfo的调用,增加了参数 * Uses method and type-level @{@link RequestMapping} annotations to create * the RequestMappingInfo. * * @return the created RequestMappingInfo, or {@code null} if the method * does not have a {@code @RequestMapping} annotation. * * @see #getCustomMethodCondition(Method) * @see #getCustomTypeCondition(Class) */ @Override protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) { RequestMappingInfo info = null; RequestMapping methodAnnotation = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (methodAnnotation != null) { RequestCondition<?> methodCondition = getCustomMethodCondition(method); info = createRequestMappingInfo(methodAnnotation, methodCondition, method, handlerType); RequestMapping typeAnnotation = AnnotationUtils.findAnnotation(handlerType, RequestMapping.class); if (typeAnnotation != null) { RequestCondition<?> typeCondition = getCustomTypeCondition(handlerType); info = createRequestMappingInfo(typeAnnotation, typeCondition, method, handlerType).combine(info); } } return info; } /** * 将父类同名方法加入controller的类型和方法参数 */ private RequestMappingInfo createRequestMappingInfo(RequestMapping annotation, RequestCondition<?> customCondition, Method method, Class<?> handlerType) { String[] patterns = resolveEmbeddedValuesInPatterns(annotation.value()); //@RequestMapping没有配置value, 加入coc部分 if( patterns.length==0 ){ StringBuilder p = new StringBuilder(); if (this.basePackage != null) { String packageName = ClassUtils.getPackageName(handlerType); if (packageName.startsWith(this.basePackage)) { String subPackage = packageName.substring(this.basePackage.length()).replace('.', '/'); if( subPackage.endsWith("/controller") ){ //去掉/backup/controller的 /controller subPackage = subPackage.substring(0,subPackage.indexOf("/controller")); } p.append(subPackage.toLowerCase()); p.append("/"); } } //类名去掉后缀小写 String className = handlerType.getSimpleName(); className = (className.endsWith(CONTROLLER_SUFFIX) ? className.substring(0, className.lastIndexOf(CONTROLLER_SUFFIX)) : className); p.append(className.toLowerCase()).append("/");//类名小写 p.append(method.getName().toLowerCase());//方法名小写 patterns = new String[]{p.toString()}; } return new RequestMappingInfo( new PatternsRequestCondition(patterns, getUrlPathHelper(), getPathMatcher(), this.useSuffixPatternMatch, this.useTrailingSlashMatch, this.fileExtensions), new RequestMethodsRequestCondition(annotation.method()), new ParamsRequestCondition(annotation.params()), new HeadersRequestCondition(annotation.headers()), new ConsumesRequestCondition(annotation.consumes(), annotation.headers()), new ProducesRequestCondition(annotation.produces(), annotation.headers(), getContentNegotiationManager()), customCondition); } //----------------------set get public void setBasePackage(String basePackage) { this.basePackage = basePackage; } public void setUseSuffixPatternMatch(boolean useSuffixPatternMatch) { this.useSuffixPatternMatch = useSuffixPatternMatch; } public void setUseTrailingSlashMatch(boolean useTrailingSlashMatch) { this.useTrailingSlashMatch = useTrailingSlashMatch; } }
3.参数自定义类型转换
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="webBindingInitializer"> <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> <property name="conversionService"> <bean class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="cn.com.demo.framework.spring.core.convert.converter.StringToDateConverter"/> </list> </property> </bean> </property> </bean> </property> <property name="customArgumentResolvers"> <list> <bean class="cn.com.demo.framework.web.springmvc.support.method.support.crud.CrudHandlerMethodArgumentResolver"/> </list> </property> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"> <property name="serializers"> <array> <bean class="cn.com.demo.canyin.common.springmvc.converter.json.LongToStringJsonSerializer" /> </array> </property> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd'T'HH:mm:ss.SSS" /> </bean> </property> </bean> </property> </bean> <bean class="org.springframework.http.converter.ResourceHttpMessageConverter" /> </list> </property> </bean>
public class StringToDateConverter implements Converter<String, Date>{ @Override public Date convert(String source) { if( source==null || source.length()==0 ) return null; int length = source.length(); switch (length) { case 5: return parse("HH:mm", source); case 10: return parse("yyyy-MM-dd", source); case 19: case 21: case 22: return parse("yyyy-MM-dd HH:mm:ss", source); case 23: return parse("yyyy-MM-dd'T'HH:mm:ss.SSS", source); case 28: try { return new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US).parse(source); } catch (ParseException e) { throw new RuntimeException("时间转换格式异常,datestr:" + source, e); } default: throw new RuntimeException("Unsupport Date Format: " + source); } } /** * 解析date */ private Date parse(String format, String date){ try { return new SimpleDateFormat(format).parse(date); } catch (ParseException e) { throw new RuntimeException(e); } } } public class CrudHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver{ @Override public boolean supportsParameter(MethodParameter parameter) { Class<?> paramType = parameter.getParameterType(); return QueryParam.class.isAssignableFrom(paramType); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { Class<?> paramType = parameter.getParameterType(); //查询参数 QueryParam p = parseQueryParam(parameter, webRequest); return p; } /** * <b>功能描述:</b>解析查询参数<br> */ @SuppressWarnings("serial") private QueryParam parseQueryParam(MethodParameter parameter, NativeWebRequest webRequest){ QueryParam p = new QueryParam(); //分页参数 p.setStart(getInteger(webRequest, "start")); p.setLimit(getInteger(webRequest, "limit")); String sort = webRequest.getParameter("sort"); if(StringUtils.isNotBlank(sort)){ SortExpression[] orderBy = JsonConverterUtils.getJc().fromJson(sort, SortExpression[].class); DefaultConvertName dcn = new DefaultConvertName(); orderBy[0].setProperty(dcn.convertToColumnName(orderBy[0].getProperty())); p.setSort(orderBy[0].toSqlString()); } return p; } /** * 获取整数参数 */ private Integer getInteger(NativeWebRequest webRequest, String name){ String p = webRequest.getParameter(name); if( p==null || p.length()==0 ) return null; return Integer.valueOf(p); } /** * 如果是get方法, 编码参数 */ private String encodeParameter(NativeWebRequest webRequest, String paramName){ HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class); String p = webRequest.getParameter(paramName); if( "GET".equalsIgnoreCase(request.getMethod())){ return RequestUtil.encodeParameter(p); } return p; } } public class LongToStringJsonSerializer extends JsonSerializer<Long> { @Override public Class<Long> handledType() { return Long.class; } @Override public void serialize(Long value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { jgen.writeString(value.toString()); } }
4.自定义异常拦截器
<!-- mvc exception handler --> <bean id="handlerExceptionResolver" class="cn.com.demo.framework.enterprise.spring.web.ProjectHandlerExceptionResolver"></bean>
public class ProjectHandlerExceptionResolver implements HandlerExceptionResolver{ private final Logger logger = LoggerFactory.getLogger(getClass()); /** json 转换 */ @Resource private JsonConverter jsonConverter; @Resource private LogManageService logManageService; @Override public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { return null; } /** * <b>功能描述:</b> * 是否ExtForm的StandardSubmit模式<br> * 前台需要额外传入X-Requested-With的参数来说明本次提交为StandardSubmit */ private boolean isExtFormStandardSubmit(HttpServletRequest request){ return request.getParameter("X-Requested-With")!=null; } }
发表评论
-
Spring AOP动态代理
2015-01-25 22:23 757package com.test.dynamicproxy; ... -
Spring加载资源并装配对象的过程
2015-01-25 22:20 10301. 定义好Spring的配置文件。 2. 通过Resourc ... -
(转载)Spring Bean 初始化过程
2013-05-10 13:10 1128Spring 的几个接口 1.InitializingBea ... -
(转载)浅谈Spring事务隔离级别
2013-04-15 14:45 956一、Propagation : key属 ... -
AOP+LOG4J日志框架(自定义注解)
2013-04-11 15:00 2703工作中用到日志功能,参考网上一些资料,写了个比较通用的日志框架 ... -
(转载)spring AOP获得session
2013-02-20 17:01 2370由于Spring 的AOP面向切面编程,与Servlet容器没 ... -
Quartz(三)原理及源码分析
2012-10-30 14:56 14016quartz配置文件中可以通过以下两种配置读取方式 org.q ... -
spring-security3(二)源码分析
2012-04-06 17:08 8160利用断点走了一遍spring-security源码的核心部分, ... -
spring-security3(一)配置详解及API扩展(包含ajax返回)
2012-04-06 17:05 3782最近对spring-security3做了一些初步了解,搜集了 ... -
AOP+Ehcache 缓存框架
2011-12-14 16:49 1642AOP+Ehcache 实现缓存功能 设计思路:查询数据,通 ... -
JTA集成JOTM或Atomikos配置分布式事务(Tomcat应用服务器)
2010-01-15 21:01 6634一.以下介绍Spring中直接集成JOTM提供JTA事务管理、 ... -
Quartz(二)整合Spring容器中bean及动态调度任务
2010-01-13 10:01 7688Quartz 是开源任务调度框架中的翘首,它提供了强大任务 ... -
Quartz(一)在Spring中设置动态定时任务
2009-05-17 07:09 7759什么是动态定时任务: 是由客户制定生成的,服务端只知道该去执行 ... -
Spring邮件发送(可带附件,模板,群发,异步发送等功能)
2009-05-17 06:27 8685以下是我对spring发送邮件的总结: 分别使用了两种方法:单 ...
相关推荐
这是我的博客spring之扩展点的示例测试代码,不包含相关依赖包
Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点,Spring常用注解和扩展点
主要介绍了如何利用Spring的@Import扩展点与spring进行无缝整合的实例代码,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
1.3 Spring开闭原则的表现-BeanPostProcessor的扩展点-1 1.4 我对IoC/DI的理解 1.5 SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常见问题总结 1.6 »Spring 之AOP AspectJ切入点语法详解(最全了,不需要再去...
- Spring 的扩展点主要有哪些? - Spring如何解决循环依赖? - 事务的传播行为是什么?有哪些? - 什么是AOP? - AOP的组成元素和概念有哪些? - AOP实现方式有哪些? - AspectJ AOP 和 Spring AOP的区别? - cglib...
NULL 博文链接:https://wangxinchun.iteye.com/blog/2301263
主要是讲spring自定义xml配置的扩展相关的内容,东西不算新的,都是比较基础的,想看就看一下 我也是为了搞点积分才上传的
--Spring Boot内置的监控点、自定义的监控状况检查、自定义度量统计,输出等等 24 Spring Boot 测试54:13 --实战演示在Spring Boot里面的测试案例,以及如何测试Controller 25 Spring Boot 构建微服务实战01:01:50...
主要介绍了Springboot启动扩展点超详细教程小结,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
Spring Beans之上将横切关注点模块化 2. spring-aspects-4.1.1.RELEASE.jar 提供的对AspectJ框架的整合,也是A面向切面编程。 AspectJ可用于基于普通Java对象的模块化 注意:aop 和 aspects区别: ...
本次课程以SpringData为中心,重点讲解了其JPA组件,扩展讲解了redis,mongDB,ES组件,并且对部分组件做了必要的源码分析。而且在课程的最后部分加入了一个综合案例,可以将前面章节所学知识点应用到一个项目中,帮助...
12.2.3.Spring.NET提供的切入点实现类 12.2.3.1.静态切入点 12.2.3.2.动态切入点 12.2.4.自定义切入点 12.3.Spring.NET的通知类型 12.3.1.通知的生命周期 12.3.2.通知类型 12.3.2.1.拦截环绕通知 12.3.2.2.前置通知 ...
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.1.1. 使用BeanPostProcessor的Hello World示例 3.7.1.2. RequiredAnnotationBeanPostProcessor示例 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7....
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 ...
提取了Spring的核心逻辑,极大地简化了代码,并完善了Spring的核心功能,例如IoC和AOP,资源加载器,事件侦听器,类型转换,容器扩展点,Bean生命周期和作用域以及应用程序上下文,被保留。 如果这个项目可以为您...
摘录了spring的核心逻辑,代码极度简化,保留spring的核心功能,如IoC和AOP,资源加载器,事件监听器,类型转换,容器扩展点,bean生命周期和作用域,应用某些等核心功能。 如果本项目能帮助到你,请给个STAR,谢谢...
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. BeanFactory 还是 ...
整个视频课程将由浅入深,介绍spring5源码的构建、spring5IOC容器的初始化过程、bean的声明周期过程、spring BeanFactoryPostporcessor并且结合...比如mybaits的源码当中到底如何利用了spring的扩展点写出优雅的代码...
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 ...
3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. BeanFactory 还是 ...