spring boot学习

常用注解

  1. @Configuration
    默认@Configuration注解下的文件进行自动配置
    但是如果加了@ConditionalOnProperty @ConditionalOnClass 添加注解 则必须在满足条件的情况下才进行自动配置

  2. NestedConfigurationProperty 读取yml属性的 嵌套对象属性

  3. SerializedName 字段添加 接受json类型可以大写转小写 JsonProperty 也可以

  4. 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 {

}

国际化配置

  1. 注入国际化bin
  2. yml配置 spring.messages.basename=i18n/messages
  3. 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());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值