美文网首页
日常开发问题

日常开发问题

作者: Kenny_Yu | 来源:发表于2018-10-05 22:04 被阅读0次

    Springboot的配置问题

    @Component就是将这个类作为组件给spring进行管理

    要记得在Application中配置 @ComponentScan(路径)进行扫描

    springboot中如何注册bean呢:

    写完一个类后,这个类不需要添加任何注解给spring管理,而是在配置类中(就是@Configuration注解的类中)

    声明

    @Bean

    public 要注册的类名 方法名(){

        return new 要注册的类的构造方法;

    }


    Springboot2整合redis

    1.

    pom.xml

    spring-boot-starter-data-redis

    exclusions

        exclusion

            jedis

        exclusion

            lettuce-core

    添加jedis客户端

    jedis

    commons-pool2

    fastjson

    2.

    配置文件

    package com.kenny.shiroweb.config;

    import org.slf4j.Logger;

    import org.slf4j.LoggerFactory;

    import org.springframework.beans.factory.annotation.Value;

    import org.springframework.cache.annotation.CachingConfigurerSupport;

    import org.springframework.cache.annotation.EnableCaching;

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    import redis.clients.jedis.JedisPool;

    import redis.clients.jedis.JedisPoolConfig;

    /**

    * Redis 配置类

    *

    * @author Leon

    * @version 2018/6/17 17:46

    */

    @Configuration

    // 必须加,使配置生效

    @EnableCaching

    public class RedisConfigextends CachingConfigurerSupport {

    /**

    * Logger

    */

        private static final Loggerlg = LoggerFactory.getLogger(RedisConfig.class);

        @Value("${spring.redis.host}")

    private Stringhost;

        @Value("${spring.redis.port}")

    private int port;

        @Value("${spring.redis.timeout}")

    private int timeout;

        @Value("${spring.redis.jedis.pool.max-idle}")

    private int maxIdle;

        @Value("${spring.redis.jedis.pool.max-wait}")

    private long maxWaitMillis;

        @Value("${spring.redis.password}")

    private Stringpassword;

        @Bean

        public JedisPoolredisPoolFactory() {

    lg.info("JedisPool注入成功!!");

            lg.info("redis地址:" +host +":" +port);

            JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();

            jedisPoolConfig.setMaxIdle(maxIdle);

            jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

            JedisPool jedisPool =new JedisPool(jedisPoolConfig, host, port);

            return jedisPool;

        }

    }

    3.

    在Jedis工具类中:

    自动注入

    @Autowired

    private JedisPool jedisPool;

    通过jedisPool.getResource();获取Jedis进行操作

    将一List集合中某一属性提取出来成为一个新List

    原来List.stream().map(该List的类型::get这个类型的属性)

    .collect(Collectors.toList())

    例如:

    List productInfoList=productService.findUpAll();

    List categoryTypeList=productInfoList.stream().map(ProductInfo::getCategoryType)

    .collect(Collectors.toList());

    timestamp有两个属性,分别是CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMP两种,使用情况分别如下:

    1.

    CURRENT_TIMESTAMP 

    当要向数据库执行insert操作时,如果有个timestamp字段属性设为

    CURRENT_TIMESTAMP,则无论这个字段有没有set值都插入当前系统时间

    2.

    ON UPDATE CURRENT_TIMESTAMP

    当执行update操时,并且字段有ON UPDATE CURRENT_TIMESTAMP属性。则字段无论值有没有变化,它的值也会跟着更新为当前UPDATE操作时的时间。

    用了RequestBody必须用POST

    跨域问题:

    https://blog.csdn.net/qq_35414305/article/details/78379949

    跨域指的是不在同一协议,同一域名,同一端口进行访问就会出现跨域问题。

    通常解决方法:CORS

    在回应头中添加:

    1.Access-Control-Allow-Origin 表示是否跨域

    2.Access-Control-Allow-Methods 跨域请求类型

    3.Access-Control-Allow-Headers

    阿里云服务器内存不够 导致启动项目自动挂了mysql

    http://hongjiang.info/aliyun-vps-mysql-aborting/

    对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序)

    HashMap<String,String> data=new HashMap<>();

    data.put();

    Set<String> keySet = data.keySet();

    String[] keyArray = keySet.toArray(new String[keySet.size()]);

    Arrays.sort(keyArray);

    @RequestBody的执行顺序比Aop的@Before还快

    导致Aop获取前端参数的时候 HttpServletRequest.getInputStream()被@RequestBody消费了,导致流被关闭了。

    相关文章

      网友评论

          本文标题:日常开发问题

          本文链接:https://www.haomeiwen.com/subject/yuahoftx.html