spring-boot之一:简单入门

发布时间:2020-07-09 18:12:44 作者:gauyanm
来源:网络 阅读:6367

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之一:简单入门


推荐阅读:
  1. 简单了解MySQL入门架构
  2. oracle 简单入门

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot 微服务

上一篇:性能测试-JMeter安装环境及运行原理

下一篇:路由器恢复出厂设置

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》