zoukankan      html  css  js  c++  java
  • SpringBoot自动配置的演示

    1. 需求说明:
        当加入redis客户端的坐标的时候,自动配置jedis的bean 加载到spring容器中;
        
    2. 实现步骤:
        1.创建工程 it-redis-springboot-starter 用作起步依赖
            
        2.添加依赖
            1. pom文件
            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                <modelVersion>4.0.0</modelVersion>
    
                <groupId>com.it</groupId>
                <artifactId>it-redis-springboot-starter</artifactId>
                <version>1.0-SNAPSHOT</version>
                <description>自定义起步依赖,给别人用,需要在这个项目中进行自动配置</description>
    
                <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.1.4.RELEASE</version>
                </parent>
    
                <dependencies>
                    <!--springboot的starter-->
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter</artifactId>
                    </dependency>
                    <!--redis的依赖jedis-->
                    <dependency>
                        <groupId>redis.clients</groupId>
                        <artifactId>jedis</artifactId>
                        <version>3.2.0</version>
                    </dependency>
                </dependencies>
    
    
            </project>
        3.创建自动配置类和POJO
            1. 创建pojo,并在配置类中使用注解@EnableConfigurationProperties(value = RedisProperties.class)建立映射:
            package com.it.redis.config;
    
            import org.springframework.boot.context.properties.ConfigurationProperties;
            import org.springframework.boot.context.properties.EnableConfigurationProperties;
    
            import java.io.Serializable;
    
            /**
             * ToDo
             *
             * @author Lyle
             * @date 2020/4/2
             */
            @ConfigurationProperties(prefix = "redis")
            public class RedisProperties implements Serializable {
                private String host = "localhost";//给与默认值
                private Integer port = 6379;//给与默认值
    
                public String getHost() {
                    return host;
                }
    
                public void setHost(String host) {
                    this.host = host;
                }
    
                public Integer getPort() {
                    return port;
                }
    
                public void setPort(Integer port) {
                    this.port = port;
                }
            }
            
            2. 创建配置类:
                package com.it.redis.config;
    
                import org.springframework.beans.factory.annotation.Autowired;
                import org.springframework.boot.context.properties.EnableConfigurationProperties;
                import org.springframework.context.annotation.Bean;
                import org.springframework.context.annotation.Configuration;
                import redis.clients.jedis.Jedis;
    
                /**
                 * ToDo
                 *
                 * @author Lyle
                 * @date 2020/4/2
                 */
                @Configuration//表是这是一个配置类
                @EnableConfigurationProperties(value = RedisProperties.class)
                public class RedisAutoConfiguration {
    
                    @Autowired
                    private RedisProperties redisProperties;
    
                    //注册类,交给spring容器
                    @Bean
                    public Jedis jedis(){
                        System.out.println("该方法已经执行。。。。");
                        System.out.println(redisProperties.getHost()+"<=======>"+redisProperties.getPort());
                        return new Jedis(redisProperties.getHost(),redisProperties.getPort());
                    }
                }
            
            3. 在resources下创建文件夹:META-INF,并在META-INF下创建文件spring.factories,文件中设置如下信息:
                第一行信息为EnableAutoConfiguration的全限定路径名;
                第二行为RedisAutoConfiguration的全限定路径;
                :表示换行;
                org.springframework.boot.autoconfigure.EnableAutoConfiguration=
                com.it.redis.config.RedisAutoConfiguration
                
    
    
        4.创建工程 it-test-starter 用于测试使用:
            4.1 测试类的pom文件:
            <?xml version="1.0" encoding="UTF-8"?>
            <project xmlns="http://maven.apache.org/POM/4.0.0"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
                <modelVersion>4.0.0</modelVersion>
    
                <groupId>com.it</groupId>
                <artifactId>it-test-starter</artifactId>
                <version>1.0-SNAPSHOT</version>
                <description>依赖自定义的starter,可以直接使用jedis</description>
    
                <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.1.4.RELEASE</version>
                </parent>
    
                <dependencies>
                    <dependency>
                        <groupId>com.it</groupId>
                        <artifactId>it-redis-springboot-starter</artifactId>
                        <version>1.0-SNAPSHOT</version>
                    </dependency>
                </dependencies>
    
    
            </project>
            
            4.2 创建启动类;
                package com.it;
    
                import org.springframework.boot.SpringApplication;
                import org.springframework.boot.autoconfigure.SpringBootApplication;
                import org.springframework.context.ConfigurableApplicationContext;
                import redis.clients.jedis.Jedis;
    
                /**
                 * ToDo
                 *
                 * @author Lyle
                 * @date 2020/4/2
                 */
                @SpringBootApplication
                public class JedisStarterApplication {
                    public static void main(String[] args) {
                        ConfigurableApplicationContext context = SpringApplication.run(JedisStarterApplication.class, args);
                        Jedis jedis = context.getBean(Jedis.class);
                        System.out.println(jedis);
                    }
                }
                
            4.3 配置文件application.properties(可以不配置,使用pojo中的默认配置)
                redis.host=localhost
                redis.port=666
    
        
        
  • 相关阅读:
    说说你对集成测试中自顶向下集成和自底向上集成两个策略的理解,要谈出它们各自的优缺点和主要适应于哪种类型测试;
    通过画因果图来写测试用例的步骤为___、___、___、___及把因果图转换为状态图共五个步骤。&#160;利用因果图生成测试用例的基本步骤是:
    性能测试的流程?
    简述bug的生命周期?
    主键、外键的作用,索引的优点与不足?
    循序渐进VUE+Element 前端应用开发(30)--- ABP后端和Vue+Element前端结合的分页排序处理(转载)
    循序渐进VUE+Element 前端应用开发(31)--- 系统的日志管理,包括登录日志、接口访问日志、实体变化历史日志(转载)
    黑盒测试和白盒测试是软件测试的两种基本方法,请分别说明各自的优点和缺点!     
    如何测试一个纸杯?
    测试计划工作的目的是什么?测试计划文档的内容应该包括什么?其中哪些是最重要的?
  • 原文地址:https://www.cnblogs.com/lyle-liu/p/12622271.html
Copyright © 2011-2022 走看看