Spring Boot加载Properties文件的方法

最近使用Spring Boot框架写了一个小网站.感觉Spring Boot写网站十分的优雅

本文将介绍如何在Spring Boot内引用Properties的值

1.开启文件扫描

2.在需要引入配置文件的Class上使用@PropertySource注解.

3.在Class中的属性上使用@Value注解来注入配置文件中的值.

 

演示

package org.jzbk.example.util;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Getter
@Component
@PropertySource(value = "classpath:versionInfo.properties")
public class VersionInfo {
    @Value("${example.platform}")
    private String platform;

    @Value("${example.version}")
    private String version;

    @Value("${example.branch}")
    private String branch;

    @Value("${example.buildDate}")
    private String buildDate;
}

 

Munin提示min must be less than max in DS definition

因为家中使用的树莓派意外损坏,使用NAS上的虚拟机来代替原来树莓派的工作.在配置Munin的时候出现错误,当更新网络设备数据时,munin服务器端报错

2016/12/20 17:30:05 [INFO] creating rrd-file for if_ens3->down: '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-down-d.rrd'
2016/12/20 17:30:05 [ERROR] Unable to create '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-down-d.rrd': min must be less than max in DS definition
2016/12/20 17:30:05 [ERROR] In RRD: Error updating /var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-down-d.rrd: opening '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-down-d.rrd': No such file or directory
2016/12/20 17:30:05 [INFO] creating rrd-file for if_ens3->up: '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-up-d.rrd'
2016/12/20 17:30:05 [ERROR] Unable to create '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-up-d.rrd': min must be less than max in DS definition
2016/12/20 17:30:05 [ERROR] In RRD: Error updating /var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-up-d.rrd: opening '/var/lib/munin/ARCHLINUX/mx.ARCHLINUX-if_ens3-up-d.rrd': No such file or directory

 

在做了一些搜索之后发现文章 https://github.com/mail-in-a-box/mailinabox/issues/896

 

执行munin-run if_ens3 config 后出现以下输出

graph_order down up
graph_title ens3 traffic
graph_args --base 1000
graph_vlabel bits in (-) / out (+) per ${graph_period}
graph_category network
graph_info This graph shows the traffic of the ens3 network interface. Please note that the traffic is shown in bits per second, not bytes. IMPORTANT: On 32-bitsystems the data source for this plugin uses 32-bit counters, which makes the plugin unreliable and unsuitable for most 100-Mb/s (or faster) interfaces, where traffic is expected to exceed 50 Mb/s over a 5 minute period.  This means that this plugin is unsuitable for most 32-bit production environments. To avoid this problem, use the ip_ plugin instead.  There should be no problems on 64-bit systems running 64-bit kernels.
down.label received
down.type DERIVE
down.graph no
down.cdef down,8,*
down.min 0
up.label bps
up.type DERIVE
up.negative down
up.cdef up,8,*
up.min 0
up.max -1000000
up.info Traffic of the ens3 interface. Maximum speed is -1 Mb/s.
down.max -1000000

根据文章的内容,应用munin-monitoring/munin@f982751到插件中即可修复这个问题.

 

使用以下patch可以解决问题

--- if_.orig    2016-08-02 23:52:05.691224811 +0200
+++ if_ 2016-08-02 23:52:49.563223127 +0200
@@ -91,7 +91,7 @@
     # iwlist first)
     if [[ -r /sys/class/net/$INTERFACE/speed ]]; then
             SPEED=$(cat /sys/class/net/$INTERFACE/speed 2>/dev/null)
-            if [[ -n "$SPEED" ]]; then
+            if [ -n "$SPEED" -a "$SPEED" -gt "0" ]; then
                 echo $SPEED
                 return
             fi

 

Spring Session整合Redisson

前言:

Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)。它不仅提供了一系列的分布式的Java常用对象,还提供了许多分布式服务。其中包括(BitSet, Set, Multimap, SortedSet, Map, List, Queue, BlockingQueue, Deque, BlockingDeque, Semaphore, Lock, AtomicLong, CountDownLatch, Publish / Subscribe, Bloom filter, Remote service, Spring cache, Executor service, Live Object service, Scheduler service) Redisson提供了使用Redis的最简单和最便捷的方法。Redisson的宗旨是促进使用者对Redis的关注分离(Separation of Concern),从而让使用者能够将精力更集中地放在处理业务逻辑上。 (摘自官方WIKI)

 

Redisson在3.2.0版本里新增了对Spring-Session整合的支持.本文将介绍整合Spring-Boot Spring-Session Redisson的方法

1. 添加依赖

Maven

<dependency>
   <groupId>org.springframework.session</groupId>
   <artifactId>spring-session</artifactId>
   <version>1.2.2.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.2.0</version>
</dependency>

 

Gradle

compile 'org.springframework.session:spring-session:1.2.2.RELEASE'  
compile group: 'org.redisson', name: 'redisson', version:'3.2.0'

 

2.创建配置文件RedisConfig.java

package org.jzbk.example.configuration;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.spring.session.config.EnableRedissonHttpSession;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;

@EnableRedissonHttpSession
public class RedisConfig {
    @Bean(destroyMethod = "shutdown")
    public RedissonClient getRedis() throws IOException {
        return Redisson.create(
                Config.fromYAML(
                        new ClassPathResource("redisson.yaml").getInputStream()
                )
        );
    }
}

因为我的redisson.yaml在classpath根目录下,所以直接使用ClassPathResource,请根据实际情况来修改.

 

3.修改application.properties,增加下列配置

spring.session.store-type=redis
server.session.timeout=7200

 

4.启动项目,刷新页面即可看到成功整合成功.

 

参考: https://github.com/redisson/redisson/wiki/14.-Integration%20with%20frameworks#145-spring-session

Spring Boot使用@Cacheable注解

通常,我使用Hibernate的@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)注解来缓存@Entity类.

在JAP2规范内另一个注解@Cacheable有与Hibernate的@Cache的一样的功能使用@Cacheable的条件如下.

1.Entity Class实现Serializable接口

2.在Entity Class前加入@Cacheable(true)

例如

@Entity
@Cacheable(true) 
public class UserEntity implements Serializable {
 // properties
}

 

3.在配置文件内开启缓存[1]

spring.jpa.properties.javax.persistence.sharedCache.mode=ENABLE_SELECTIVE

 

参考: http://docs.oracle.com/javaee/6/tutorial/doc/gkjio.html [1]

http://stackoverflow.com/questions/31585698/spring-boot-jpa2-hibernate-enable-second-level-cache