Spring import 配置文件使用占位符
import使用占位符连接池切换导入配置的代码: <import resource="classpath:META-INF/spring/spring-${db.connection.pool}.xml" /> 在配置文件添加配置 db.connection.pool=druid 启动直接报错,读取不到配置,因为属性文件的加载在import配置文件之后。 Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'db.connection.pool' in value "classpath:META-INF/spring/spring-${db.connection.pool}.xml" 所以,要在应用启动的时候添加属性 1、添加AppContextInitializer启动类: public clas...
Spring 快速开启计划任务
Spring3.1开始让计划任务变得非常简单,只需要几个注解就能快速开启计划任务的支持。 @EnableScheduling@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(SchedulingConfiguration.class) @Documented public @interface EnableScheduling { } @EnableScheduling、@Configuration两个同时使用开启计划任务支持。 import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; @EnableScheduling @Configuration public class TaskConfiguration { } @Scheduled在要使...
终于,Spring 5.0 正式发布了!
Spring 5.0都有什么新功能?1、最低要求支持JDK8+, J2EE 7+。 2、移除了一些包、类及方法。 3、核心功能加强:全面应用jdk8并兼容jdk9等。 4、SpringMVC:支持servlet4.0、Reactor 3.1等。 5、支持响应式堆栈web框架Spring WebFlux。 6、支持kotlin。 7、加强了测试:完美支持Junit5等。 Spring 5.0下载Maven: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.0.RELEASE</version> </dependency> Gradle: compile group: 'org.springframework', name: 'spring-...
Spring 开启方法异步执行
@EnableAsync@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AsyncConfigurationSelector.class) public @interface EnableAsync { Class<? extends Annotation> annotation() default Annotation.class; boolean proxyTargetClass() default false; AdviceMode mode() default AdviceMode.PROXY; int order() default Ordered.LOWEST_PRECEDENCE; } @EnableAsync注解即开启Spring对方法异步执行的能力,需要和注解@Configuration配合使用。 @Configuration @EnableAsync public class AppConfig...
Spring MVC 常用注解
常用注解 Controller 注解一个类表示控制器,Spring MVC会自动扫描标注了这个注解的类。 RequestMapping 请求路径映射,可以标注类,也可以是方法,可以指定请求类型,默认不指定为全部接收。 RequestParam 放在参数前,表示只能接收参数a=b格式的数据,即Content-Type为application/x-www-form-urlencoded类型的内容。 RequestBody 放在参数前,表示参数从request body中获取,而不是从地址栏获取,所以这肯定是接收一个POST请求的非a=b格式的数据,即Content-Type不为application/x-www-form-urlencoded类型的内容。 ResponseBody 放在方法上或者返回类型前,表示此方法返回的数据放在response body里面,而不是跳转页面。一般用于ajax请求,返回json数据。 RestController 这个是Controller和ResponseBody的组合注解,表示@Controller标识的...
Spring MVC 表单防重复提交
利用Spring MVC的过滤器及token传递验证来实现表单防重复提交。 创建注解@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Token { boolean create() default false; boolean remove() default false; } 在跳转页面的方法上加上:@Token(create = true)在提交的action方法上加上:@Token(remove = true) 创建过滤器public class TokenInterceptor extends HandlerInterceptorAdapter { private Logger logger = Logger.getLogger(TokenInterceptor.class); private static final String TOKEN =...
Spring MVC 表单验证器
本章讲解SpringMVC中怎么通过注解对表单参数进行验证。 SpringBoot配置使用springboot,spring-boot-starter-web会自动引入hiberante-validator,validation-api依赖。 在WebMvcConfigurerAdapter实现类里面添加验证器及国际化指定资源文件。 @Override public Validator getValidator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(messageSource()); return validator; } @Bean public MessageSource messageSource() { ResourceBundleMessageSource messageSource = new ResourceBundleMe...
Spring Cloud @RefreshScope 原理是什么?
要清楚RefreshScope,先要了解ScopeScope(org.springframework.beans.factory.config.Scope)是Spring 2.0开始就有的核心的概念 RefreshScope(org.springframework.cloud.context.scope.refresh)是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。 Scope -> GenericScope -> RefreshScope Scope与ApplicationContext生命周期 AbstractBeanFactory#doGetBean创建Bean实例 protected <T> T doGetBean(...){ final RootBeanDefinition mbd = ... if (mbd.isSingleton()) { ... } else if (mbd.isPrototype()) .....
Spring Cloud Alibaba Sentinel 整合 Feign 的设计实现
作者 | Spring Cloud Alibaba 高级开发工程师洛夜来自公众号阿里巴巴中间件投稿 前段时间 Hystrix 宣布不再维护之后(Hystrix 停止开发。。。Spring Cloud 何去何从?),Feign 作为一个跟 Hystrix 强依赖的组件,必然会有所担心后续的使用。 作为 Spring Cloud Alibaba 体系中的熔断器 Sentinel,Sentinel 目前整合了 Feign,本文对整合过程做一次总结,欢迎大家讨论和使用。 Feign 是什么?Feign 是一个 Java 实现的 Http 客户端,用于简化 Restful 调用。 Feign 跟 OkHttp、HttpClient 这种客户端实现理念不一样。Feign 强调接口的定义,接口中的一个方法对应一个 Http 请求,调用方法即发送一个 Http 请求;OkHttp 或 HttpClient 以过程式的方式发送 Http 请求。Feign 底层发送请求的实现可以跟 OkHttp 或 HttpClient 整合。 要想整合 Feign,首先要了解 Feign 的使用以及执行过...
Spring Cloud Eureka 常用配置详解,建议收藏!
前几天,栈长分享了 《Spring Cloud Eureka 注册中心集群搭建,Greenwich 最新版!》,今天来分享下 Spring Cloud Eureka 常用的一些参数配置及说明。 Spring Cloud Eureka 主要分为下面三个模块的参数: Eureka Server Eureka Client Eureka Instance Eureka ServerEureka Server 的配置参数格式:eureka.server.xxx。 enable-self-preservation表示注册中心是否开启服务的自我保护能力。 renewal-percent-threshold表示 Eureka Server 开启自我保护的系数,默认:0.85。 eviction-interval-timer-in-ms表示 Eureka Server 清理无效节点的频率,默认 60000 毫秒(60 秒)。 更多 Eureka Server 参数配置可以看一下这个类: org.springframework.cloud.netflix.eureka.server.Eure...
