中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring-boot之一:簡單入門

發布時間:2020-07-09 18:12:44 來源:網絡 閱讀:6367 作者:gauyanm 欄目:軟件技術

1.在IDE中創建一個web項目

spring-boot之一:簡單入門spring-boot之一:簡單入門

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

<parent>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-parent</artifactId>

   <version>1.5.9.RELEASE</version>

</parent>

<dependencies>

   <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-web</artifactId>

   </dependency>

</dependencies>

spring-boot之一:簡單入門spring-boot之一:簡單入門

3.創建程序入口類Application.java

package com.xuejia.ad;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @author kehaojian

* @date 2017-12-28 8:33

* @since 1.0.0

*/

@SpringBootApplication

public class Application {

   public static void main(String[] args) {

       SpringApplication.run(Application.class, args);

   }

}

spring-boot之一:簡單入門

4.創建一個controller類

package com.xuejia.ad.controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* @author kehaojian

* @date 2017-12-28 8:35

* @since 1.0.0

*/

@RestController

public class StartDemoController {

   @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依賴包

<!--測試-->

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-test</artifactId>

</dependency>

<!--redis-->

<dependency>

   <groupId>org.springframework.boot</groupId>

   <artifactId>spring-boot-starter-redis</artifactId>

</dependency>

8.創建redis配置類RedisConfig

package com.xuejia.ad.config;

import com.fasterxml.jackson.annotation.JsonAutoDetect;

import com.fasterxml.jackson.annotation.PropertyAccessor;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.data.redis.connection.RedisConnectionFactory;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.core.StringRedisTemplate;

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import redis.clients.jedis.JedisPoolConfig;

/**

* @author kehaojian

* @date 2017-12-28 9:08

* @since 1.0.0

*/

@Configuration

@ConfigurationProperties(prefix="spring.redis")

public class RedisConfig {

   private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);

   /*@Bean

   public RedisTemplate<String, String> 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

   public JedisPoolConfig getRedisConfig(){

       JedisPoolConfig config = new JedisPoolConfig();

       return config;

   }

   @Bean

   public JedisConnectionFactory getConnectionFactory(){

       JedisConnectionFactory factory = new JedisConnectionFactory();

       JedisPoolConfig config = getRedisConfig();

       factory.setPoolConfig(config);

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

       return factory;

   }

   @Bean

   public RedisTemplate<?, ?> getRedisTemplate(){

       RedisTemplate<?,?> template = new StringRedisTemplate(getConnectionFactory());

       return template;

   }

}

9.創建redis簡單工具類

package com.xuejia.ad.common;

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

import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

/**

* @author kehaojian

* @date 2017-12-28 9:04

* @since 1.0.0

*/

@Repository

public class RedisUtil {

   @Autowired

   private RedisTemplate<String, String> redisTemplate;

   public void add(String key, String value, Long time){

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

   }

   public String get(String key) {

       return redisTemplate.opsForValue().get(key);

   }

   public void delete(String key) {

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

   }

}

10.在test目錄下創建一個測試類RedisTest

import com.xuejia.ad.common.RedisUtil;

import com.xuejia.ad.config.RedisConfig;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

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

import org.springframework.test.context.ContextConfiguration;

import org.springframework.test.context.junit4.SpringJUnit4Cla***unner;

/**

* @author kehaojian

* @date 2017-12-28 9:17

* @since 1.0.0

*/

@RunWith(SpringJUnit4Cla***unner.class)

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

public class RedisTest {

   @Autowired

   private RedisUtil redisUtil;

   @Before

   public void testbefore() {

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

   }

   @Test

   public void testRedis() throws InterruptedException {

       int total = 20;

       for (int i = 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增加打包插件配置

<!--打包發布-->

<build>

   <plugins>

       <plugin>

           <groupId>org.springframework.boot</groupId>

           <artifactId>spring-boot-maven-plugin</artifactId>

           <executions>

               <execution>

                   <goals>

                       <goal>repackage</goal>

                   </goals>

               </execution>

           </executions>

       </plugin>

   </plugins>

</build>

13.執行package操作

spring-boot之一:簡單入門spring-boot之一:簡單入門


向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

惠来县| 汝阳县| 温州市| 土默特左旗| 山东省| 启东市| 聂拉木县| 白水县| 宁津县| 永吉县| 榕江县| 新干县| 云龙县| 平阳县| 自贡市| 满洲里市| 田林县| 大方县| 长子县| 德庆县| 奉贤区| 鄱阳县| 平武县| 宜川县| 承德市| 阜阳市| 洪雅县| 高清| 宣武区| 股票| 门源| 抚顺市| 祁阳县| 弋阳县| 锡林郭勒盟| 侯马市| 临城县| 沂水县| 岳普湖县| 紫金县| 五家渠市|