我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

spring-boot之一:简单入门

1.在IDE中创建一个web项目

东风网站建设公司成都创新互联公司,东风网站设计制作,有大型网站制作公司丰富经验。已为东风近千家提供企业网站建设服务。企业网站搭建\成都外贸网站建设公司要多少钱,请找那个售后服务好的东风做网站的公司定做!

spring-boot之一:简单入门spring-boot之一:简单入门

2.在pom.xml文件中增加maven依赖

   org.springframework.boot

   spring-boot-starter-parent

   1.5.9.RELEASE

   

       org.springframework.boot

       spring-boot-starter-web

   

spring-boot之一:简单入门spring-boot之一:简单入门

3.创建程序入口类Application.java

packagecom.xuejia.ad;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

/**

*@authorkehaojian

*@date2017-12-28 8:33

*@since1.0.0

*/

@SpringBootApplication

public classApplication {

   public static voidmain(String[] args) {

       SpringApplication.run(Application.class, args);

   }

}

spring-boot之一:简单入门

4.创建一个controller类

packagecom.xuejia.ad.controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RestController;

/**

*@authorkehaojian

*@date2017-12-28 8:35

*@since1.0.0

*/

@RestController

public classStartDemoController {

   @RequestMapping("/")

   String home(){

       return"hello";

   }

}

5.配置application.yml

server:

 port:8888

 tomcat:

   uri-encoding:utf-8

spring:

 redis:

   host:localhost

   port:6379

   pool:

     max-active:8

     min-idle:0

     max-idle:8

     max-wait:-1

spring-boot之一:简单入门spring-boot之一:简单入门

5.运行Application的main方法

spring-boot之一:简单入门spring-boot之一:简单入门

6.在浏览器中输入http://localhost:8888/

spring-boot之一:简单入门spring-boot之一:简单入门

7.在pom.xml增加spring-boot-starter-redis以及spring-boot-starter-test依赖包

   org.springframework.boot

   spring-boot-starter-test

   org.springframework.boot

   spring-boot-starter-redis

8.创建redis配置类RedisConfig

packagecom.xuejia.ad.config;

importcom.fasterxml.jackson.annotation.JsonAutoDetect;

importcom.fasterxml.jackson.annotation.PropertyAccessor;

importcom.fasterxml.jackson.databind.ObjectMapper;

importorg.slf4j.Logger;

importorg.slf4j.LoggerFactory;

importorg.springframework.boot.context.properties.ConfigurationProperties;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.data.redis.connection.RedisConnectionFactory;

importorg.springframework.data.redis.connection.jedis.JedisConnectionFactory;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.data.redis.core.StringRedisTemplate;

importorg.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

importredis.clients.jedis.JedisPoolConfig;

/**

*@authorkehaojian

*@date2017-12-28 9:08

*@since1.0.0

*/

@Configuration

@ConfigurationProperties(prefix="spring.redis")

public classRedisConfig {

   private staticLoggerlogger= LoggerFactory.getLogger(RedisConfig.class);

   /*@Bean

   public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {

       StringRedisTemplate template = new StringRedisTemplate(redisConnectionFactory);

       Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

       ObjectMapper objectMapper = new ObjectMapper();

       objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

       objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

       jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

       template.setValueSerializer(jackson2JsonRedisSerializer);

       template.afterPropertiesSet();

       return template;

   }

*/

   @Bean

   publicJedisPoolConfig getRedisConfig(){

       JedisPoolConfig config =newJedisPoolConfig();

       returnconfig;

   }

   @Bean

   publicJedisConnectionFactory getConnectionFactory(){

       JedisConnectionFactory factory =newJedisConnectionFactory();

       JedisPoolConfig config = getRedisConfig();

       factory.setPoolConfig(config);

       logger.info("JedisConnectionFactory bean init success.");

       returnfactory;

   }

   @Bean

   publicRedisTemplate getRedisTemplate(){

       RedisTemplate template =newStringRedisTemplate(getConnectionFactory());

       returntemplate;

   }

}

9.创建redis简单工具类

packagecom.xuejia.ad.common;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.data.redis.core.RedisTemplate;

importorg.springframework.stereotype.Repository;

importjava.util.concurrent.TimeUnit;

/**

*@authorkehaojian

*@date2017-12-28 9:04

*@since1.0.0

*/

@Repository

public classRedisUtil {

   @Autowired

   privateRedisTemplateredisTemplate;

   public voidadd(String key, String value, Long time){

       redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);

   }

   publicString get(String key) {

       returnredisTemplate.opsForValue().get(key);

   }

   public voiddelete(String key) {

       redisTemplate.opsForValue().getOperations().delete(key);

   }

}

10.在test目录下创建一个测试类RedisTest

importcom.xuejia.ad.common.RedisUtil;

importcom.xuejia.ad.config.RedisConfig;

importorg.junit.Before;

importorg.junit.Test;

importorg.junit.runner.RunWith;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.test.context.ContextConfiguration;

importorg.springframework.test.context.junit4.SpringJUnit4Cla***unner;

/**

*@authorkehaojian

*@date2017-12-28 9:17

*@since1.0.0

*/

@RunWith(SpringJUnit4Cla***unner.class)

@ContextConfiguration(classes = {RedisConfig.class, RedisUtil.class})

public classRedisTest {

   @Autowired

   privateRedisUtilredisUtil;

   @Before

   public voidtestbefore() {

       redisUtil.add("kehaojian","helloworld",100L);

   }

   @Test

   public voidtestRedis()throwsInterruptedException {

       inttotal =20;

       for(inti =0; i <20; i++){

           System.out.println(redisUtil.get("kehaojian"));

           Thread.sleep(5000);

       }

   }

}

11.运行测试类,结果如下

18:57:26.975 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection

18:57:26.975 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection

helloworld

18:57:31.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection

18:57:31.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection

helloworld

18:57:36.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection

18:57:36.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection

helloworld

18:57:41.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Opening RedisConnection

18:57:41.976 [main] DEBUG org.springframework.data.redis.core.RedisConnectionUtils - Closing Redis Connection

12.打包,在pom.xml增加打包插件配置

   

       

           org.springframework.boot

           spring-boot-maven-plugin

           

               

                   

                       repackage

                   

               

           

       

   

13.执行package操作

spring-boot之一:简单入门spring-boot之一:简单入门


网页名称:spring-boot之一:简单入门
标题链接:http://mswzjz.cn/article/jeseod.html

其他资讯