常用注解
-
@Configuration
默认@Configuration注解下的文件进行自动配置
但是如果加了@ConditionalOnProperty
@ConditionalOnClass
添加注解 则必须在满足条件的情况下才进行自动配置 -
NestedConfigurationProperty
读取yml属性的 嵌套对象属性 -
SerializedName
字段添加 接受json类型可以大写转小写JsonProperty
也可以 -
ApplicationContextAware
@JsonProperty
@SerializedName("AGE")
private String age;
跨域配置
延时任务
taskScheduler.schedule(() -> {“”, date)
常用依赖
项目搭建
启动
-Dfile.encoding=utf-8 --server.port=8080 --spring.config.location=application.yml
spring boot dependencies 模块搭建
依赖集合模块
<dependencyManagement>
<dependencies>
</dependencies>
</dependencyManagement>
主pom
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.kdzt</groupId>
<artifactId>excel-dependencies</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
aop配置
Cannot find current proxy: Set 'exposeProxy' property on Advised to 'true' to make it available, and ensure that AopContext.currentProxy() is invoked in the same thread as the AOP invocation context.
添加自动配置
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* 程序注解配置
*
* @author Lion Li
*/
@Configuration
// 表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
public class ApplicationConfig {
}
国际化配置
- 注入国际化bin
- yml配置 spring.messages.basename=i18n/messages
- resource 配置国际化文件
/**
* 1. 国际化配置
* 2. yml配置 spring.messages.basename=i18n/messages
* 3. 配置国际化文件
*
* @author Lion Li
*/
@Configuration
public class I18nConfig {
@Bean
public LocaleResolver localeResolver() {
return new I18nLocaleResolver();
}
/**
* 获取请求头国际化信息
*/
static class I18nLocaleResolver implements LocaleResolver {
@Override
public Locale resolveLocale(HttpServletRequest httpServletRequest) {
String language = httpServletRequest.getHeader("content-language");
Locale locale = Locale.getDefault();
if (StrUtil.isNotBlank(language)) {
String[] split = language.split("_");
locale = new Locale(split[0], split[1]);
}
return locale;
}
@Override
public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {
}
}
}
yml配置
# 国际化资源文件位置
spring.messages.basename=i18n.messages
# messages 文件的缓存失效时间
spring.messages.cache-duration=1
# 属性配置文件中文乱码问题
spring.messages.encoding=utf-8
# 解决中文乱码
server:
servlet:
encoding:
charset: UTF-8
enabled: true
force: true
# 解决Tomcat中文乱码问题
server.tomcat.accesslog.encoding=UTF-8
工具类
/**
* 获取i18n资源文件
*
* @author Lion Li
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MessageUtils {
private static final MessageSource MESSAGE_SOURCE = SpringUtils.getBean(MessageSource.class);
/**
* 根据消息键和参数 获取消息 委托给spring messageSource
*
* @param code 消息键
* @param args 参数
* @return 获取国际化翻译值
*/
public static String message(String code, Object... args) {
return MESSAGE_SOURCE.getMessage(code, args, LocaleContextHolder.getLocale());
}
}