zoukankan      html  css  js  c++  java
  • Spring Boot

    概述

    Redis 是一个完全开源免费的、遵守BSD 协议的、内存中的数据结构存储,它既可以作为数据库,也可以作为缓存和消息代理。因其性能优异等优势,目前已被很多企业所使用,但通常在企业中我们会将其作为缓存来使用。Spring Boot 对 Redis也提供了自动配置的支持,接下来本小节将讲解如何在Spring Boot 项目中使用 Redis。

    添加 Redis 缓存

    1.添加 Redis 起步依赖

    <dependency><groupId>org.springframework. boot</groupId>
    <artifactId>spring-boot-starter-redis</artifactId
    <version>1.4.4.RELEASE</version></dependency>

    2.添加缓存注解

    
(1)在引导类 Application.java中,添加@EnableCaching注解开启缓存

    @SpringBootApplication
    @EnableCaching
     //开启缓存 
    public class Application {
        public static void main(String[] args)
{
         SpringApplication.run(Application.class, args) ;
    
      }
    }

    (2)@Cacheable 注解来支持缓字,添加后的实现代码如下:

    @Cacheable(value="UserCache",key="'user.getAllUsers'")
       public List<User> getAllUsers (){

       return this.userMapper.getAllUsers();
    }

    需要注意的是,@Cacheable 注解中的 key 属性值除了需要被英文双引号引用外,还需要加入英文单引号,否则系统在执行缓存操作时将出错。

    3.实现可序列化接口

    4.指定 Redis 缓存主机地址

    通常情况下,Redis 缓存与 Web 应用并非部署在一台机器上,此时就需要远程调用 Redis。

    application.properties

    spring.redis.host=192.168.2.100 

    spring.redis.port=63795

    5.启动项目,测试缓存使用

     清除 Redis 缓存


    Redis 中的缓存数据不会一直存在,当执行添加、更新和删除操作后,数据库中的数据会发生变化,而 Redis 缓存中的数据同样也需要进行相应的变化。为了保证Redis 缓存中的数据与数据库中的一致,通常需要在执行添加、更新和删除操作之前清除缓存,然后在下一次执行查询操作时,将新的数据存储到Redis 缓存中。

    使用@CacheEvict 注解

    //删除用户
    @CacheEvict(value="UserCache",key="'user.getAllUsers'")
    public void deleteUser(Integer id){
    
     this.userMapper.delete (id);
         
System.out.println("删除了id为"+id+"的用户");
    }

    Spring Boot与ActiveMQ的集成

    1.添加ActiveMQ起步依赖

    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId>
    </dependency>
    <!-- activemq -->
    <dependency>
        <groupId>org.apache.xbean</groupId>
        <artifactId>xbean-spring</artifactId>
        <version>3.16</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>${activemq.version}</version>
    </dependency>

     2.创建消息队列对象

    在 Application.java中编写一个创建消息队列的方法

           @Bean
         public Queue queue(){
            return new ActiveMQQueue("active.queue");
         }

    3.创建消息生产者

    /**
     * 消息队列控制器
     */
    @RestController
    public class QueueController {
    
       @Autowired
        private JmsTemplate jmsTemplate;
       @Autowired
        private Queue queue;
    
       public void send(){
           //指定消息发送的目的地及内容
           this.jmsTemplate.convertAndSend(this.queue,"新发送的消息!");
       }
    }

    4.创建消息监听者

    /**
     * 客户控制器接收器
     */
    @RestController
    public class CustomerController {
        /**
         * 监听和读取消息队列
         * @param message
         */
        @JmsListener(destination = "active.queue")
        public  void  readActiveQueue(String message){
            System.out.println("接收到:"+message);
        }
    }

    @JmsListener是 Spring 4.1所提供的用于监听 JMS消息的注解,该注解的属性 destination 用于指定要监听的目的地。本案例中监听的是 active.queue中的消息。

    5.启动项目,测试应用

    打包部署

    WAR包

    
虽然通过Spring Bot 内嵌的Tomcat 可以直接运行所打的JAR包,但是有时候我们也会希望通过外部的Tomcat来管理多个项目。由于JAR包在Tomcat 中是无法运行的,所以我们需要将项目打成WAR包的形式
要想将项目打成 WAR包,并可以在Tomcat 中运行,需要执行以下两个步骤。
1.修改打包方式,并添加Tomcat 依赖
将项目 pom.xml 中<packaging>元素内的 jar修改为 war,并在文件中添加Tomcat 的依赖配置。其修改和添加的配置信息如下:

         <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope></dependency>

    上述配置代码中,spring-boot-starter-tomcat 是 Spring Boot自带的Tomcat依赖,默认会被打到项目的 lib包中。当我们将其依赖范围设置为 provided 时,将产生一个可执行的WAR包,在包中的lib-provided目录会有 provided 依赖。这样不仅可以部署到 Servlet 容器启动项目,还可以通过命令行执行java-jar命令来运行此应用。

    创建 SpringBootServletInitializer 子类,并覆盖其配置方法
要产生一个可部署的 war包,还需要提供一个SpringBootServletInitilizer子类,并覆盖它的configure()方法。通常我们可以采用两种方式:一是把应用的主类(引导类)修改为继承 SpringBootServletInitializer的类,并覆盖 contigure()方法;二是创建一个继承了SpringBootServletInitializer 的类,并覆盖 contigure()方法。以创建 SpringBootServletInitializer子类的方式为例,其子类实现代码如文件3-11所示。

    /**
     * web容器中进行部署
     *
     * @author miao
     */
    public class CocodeServletInitializer extends SpringBootServletInitializer {
        @Override
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(CocodeApplication.class);
        }
    }
  • 相关阅读:
    企业应用的互联网化
    企业应用开发平台GAP平台
    技术有什么用?
    Samba服务器配置
    IT从业人员需要知道的10个小秘密
    在雪豹10.6.2(Mac OS X)上安装Oracle10g
    项目经理的职责
    通过堡垒机远程连接Windows Server
    MySQL:Data truncated for column 'last_apply_time' at row 1
    当有莫名其妙的错误里, 可以
  • 原文地址:https://www.cnblogs.com/sxw123/p/12819962.html
Copyright © 2011-2022 走看看