新聞中心
Spring Boot簡介
Spring Boot是為了簡化Spring開發(fā)而生,從Spring 3.x開始,Spring社區(qū)的發(fā)展方向就是弱化xml配置文件而加大注解的戲份。最近召開的SpringOne2GX2015大會上顯示:Spring Boot已經(jīng)是Spring社區(qū)中增長最迅速的框架,前三名是:Spring Framework,Spring Boot和Spring Security,這個應(yīng)該是未來的趨勢。
我學習Spring Boot,是因為通過cli工具,spring boot開始往flask(python)、express(nodejs)等web框架發(fā)展和靠近,并且Spring Boot幾乎不需要寫xml配置文件。
學習新的技術(shù)最佳途徑是看官方文檔,現(xiàn)在Spring boot的release版本是1.3.0-RELEASE,相應(yīng)的參考文檔是Spring Boot Reference Guide(1.3.0-REALEASE),如果有絕對英文比較吃力的同學,可以參考中文版Spring Boot參考指南。在前段時間閱讀一篇技術(shù)文章,介紹如何閱讀ios技術(shù)文檔,我從中也有所收獲,那就是我們應(yīng)該重視spring.io上的guides部分——Getting Started Guides,這部分都是一些針對特定問題的demo,值得學習。
Spring Boot的項目結(jié)構(gòu)
com ?+-?example ?????+-?myproject ?????????+-?Application.java?????????| ?????????+-?domain ?????????|???+-?Customer.java?????????|???+-?CustomerRepository.java ?????????| ?????????+-?service?????????|???+-?CustomerService.java ?????????| ?????????+-?web ?????????????+-?CustomerController.java
如上所示,Spring boot項目的結(jié)構(gòu)劃分為web->service->domain,其中domain文件夾可類比與業(yè)務(wù)模型和數(shù)據(jù)存儲,即xxxBean和Dao層;service層是業(yè)務(wù)邏輯層,web是控制器。比較特別的是,這種類型的項目有自己的入口,即主類,一般命名為Application.java。Application.java不僅提供入口功能,還提供一些底層服務(wù),例如緩存、項目配置等等。
例子介紹
本文的例子是取自我的side project之中,日報(report)的查詢,試圖利用Redis作為緩存,優(yōu)化查詢效率。
知識點解析
1. 自定義配置
Spring Boot允許外化配置,這樣你可以在不同的環(huán)境下使用相同的代碼。你可以使用properties文件、yaml文件,環(huán)境變量和命令行參數(shù)來外化配置。使用@Value注解,可以直接將屬性值注入到你的beans中。
Spring Boot使用一個非常特別的PropertySource來允許對值進行合理的覆蓋,按照優(yōu)先考慮的順序排位如下:
1.?命令行參數(shù)2.?來自java:comp/env的JNDI屬性3.?Java系統(tǒng)屬性(System.getProperties())4.?操作系統(tǒng)環(huán)境變量5.?只有在random.*里包含的屬性會產(chǎn)生一個RandomValuePropertySource6.?在打包的jar外的應(yīng)用程序配置文件(application.properties,包含YAML和profile變量)7.?在打包的jar內(nèi)的應(yīng)用程序配置文件(application.properties,包含YAML和profile變量)8.?在@Configuration類上的@PropertySource注解9.?默認屬性(使用SpringApplication.setDefaultProperties指定)
使用場景:可以將一個application.properties打包在Jar內(nèi),用來提供一個合理的默認name值;當運行在生產(chǎn)環(huán)境時,可以在Jar外提供一個application.properties文件來覆蓋name屬性;對于一次性的測試,可以使用特病的命令行開關(guān)啟動,而不需要重復(fù)打包jar包。
具體的例子操作過程如下:
新建配置文件(application.properties)
spring.redis.database=0 spring.redis.host=localhost spring.redis.password=?#?Login?password?of?the?redis?server.spring.redis.pool.max-active=8 spring.redis.pool.max-idle=8 spring.redis.pool.max-wait=-1 spring.redis.pool.min-idle=0 spring.redis.port=6379 spring.redis.sentinel.master=?#?Name?of?Redis?server.spring.redis.sentinel.nodes=?#?Comma-separated?list?of?host:port?pairs.spring.redis.timeout=0
使用@PropertySource引入配置文件
@Configuration@PropertySource(value?=?"classpath:/redis.properties")@EnableCachingpublic?class?CacheConfig?extends?CachingConfigurerSupport?{ ????...... }
使用@Value引用屬性值
@Configuration@PropertySource(value?=?"classpath:/redis.properties")@EnableCachingpublic?class?CacheConfig?extends?CachingConfigurerSupport?{????@Value("${spring.redis.host}")????private?String?host;????@Value("${spring.redis.port}")????private?int?port;????@Value("${spring.redis.timeout}")????private?int?timeout; ????...... }
2. redis使用
添加pom配置
???? org.springframework.boot ????spring-boot-starter-redis
編寫CacheConfig
@Configuration@PropertySource(value?=?"classpath:/redis.properties")@EnableCachingpublic?class?CacheConfig?extends?CachingConfigurerSupport?{????@Value("${spring.redis.host}")????private?String?host;????@Value("${spring.redis.port}")????private?int?port;????@Value("${spring.redis.timeout}")????private?int?timeout;????@Bean ????public?KeyGenerator?wiselyKeyGenerator(){????????return?new?KeyGenerator()?{????????????@Override ????????????public?Object?generate(Object?target,?Method?method,?Object...?params)?{ ????????????????StringBuilder?sb?=?new?StringBuilder(); ????????????????sb.append(target.getClass().getName()); ????????????????sb.append(method.getName());????????????????for?(Object?obj?:?params)?{ ????????????????????sb.append(obj.toString()); ????????????????}????????????????return?sb.toString(); ????????????} ????????}; ????}????@Bean ????public?JedisConnectionFactory?redisConnectionFactory()?{ ????????JedisConnectionFactory?factory?=?new?JedisConnectionFactory(); ????????factory.setHostName(host); ????????factory.setPort(port); ????????factory.setTimeout(timeout);?//設(shè)置連接超時時間 ????????return?factory; ????}????@Bean ????public?CacheManager?cacheManager(RedisTemplate?redisTemplate)?{ ????????RedisCacheManager?cacheManager?=?new?RedisCacheManager(redisTemplate);????????//?Number?of?seconds?before?expiration.?Defaults?to?unlimited?(0) ????????cacheManager.setDefaultExpiration(10);?//設(shè)置key-value超時時間 ????????return?cacheManager; ????}????@Bean ????public?RedisTemplate?redisTemplate(RedisConnectionFactory?factory)?{ ????????StringRedisTemplate?template?=?new?StringRedisTemplate(factory); ????????setSerializer(template);?//設(shè)置序列化工具,這樣ReportBean不需要實現(xiàn)Serializable接口 ????????template.afterPropertiesSet();????????return?template; ????}????private?void?setSerializer(StringRedisTemplate?template)?{ ????????Jackson2JsonRedisSerializer?jackson2JsonRedisSerializer?=?new?Jackson2JsonRedisSerializer(Object.class); ????????ObjectMapper?om?=?new?ObjectMapper(); ????????om.setVisibility(PropertyAccessor.ALL,?JsonAutoDetect.Visibility.ANY); ????????om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); ????????jackson2JsonRedisSerializer.setObjectMapper(om); ????????template.setValueSerializer(jackson2JsonRedisSerializer); ????} }
啟動緩存,使用@Cacheable注解在需要緩存的接口上即可
@Servicepublic?class?ReportService?{????@Cacheable(value?=?"reportcache",?keyGenerator?=?"wiselyKeyGenerator")????public?ReportBean?getReport(Long?id,?String?date,?String?content,?String?title)?{ ????????System.out.println("無緩存的時候調(diào)用這里---數(shù)據(jù)庫查詢");????????return?new?ReportBean(id,?date,?content,?title); ????} }
測試驗證
訪問:http://localhost:8080/report/test2
訪問:http://localhost:8080/report/test
訪問:http://localhost:8080/report/test2
mvn clean package
java -jar target/dailyReport-1.0-SNAPSHOT.jar
運行方法如下:
驗證緩存起作用:
驗證緩存失效(10s+后執(zhí)行):
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
分享標題:SpringBootwithRedis-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://ef60e0e.cn/article/doepgc.html