首页 > 编程语言 > 详细

springboot03_核心特性及设计思想

时间:2020-07-06 20:40:35      阅读:97      评论:0      收藏:0      [点我收藏+]

4.1.5.springboot核心特性及设计思想【上】

时长:55min

5.1.springboot注解驱动

5.1.1.spring4.X版本注解驱动

  主要是注解装饰功能的一个完善。提供条件注解装配,即@Conditional注解使用。

5.1.1.1.什么是条件化装配?

  其实,它是对是否进行bean装配的一个条件限制,如果条件返回true,刚允许装配。否则,不允许。

下面通过示例代码来,说明条件装配的应用。

1.定义一个配置类

  首先,创建一个springboot项目。然后创建配置类:

package com.wf.demo.springbootdemo.condition;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName SpringConfiguration
 * @Description 统一的配置类
 * @Author wf
 * @Date 2020/7/6 18:09
 * @Version 1.0
 */
@Configuration
public class SpringConfiguration {
    /**
     * 在某个环境下不装载
     * 或不满足某个条件不装载
     * 或者已经装载过了,不要重复装载
     * @return
     */
    @Conditional(MyCondition.class)
    @Bean
    public DemoService demoService(){
        return new DemoService();
    }
}

 

说明:

  配置类中,通过@Bean进行bean装配。然而加上@Conditional注解,就能实现条件装配。

  @Conditional注解,传参为Condition接口子类Class对象,可以传参多个子类对象,多个对象之间是且的关系。

2.定义Condition子类
package com.wf.demo.springbootdemo.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @ClassName MyCondition
 * @Description 实现条件子类
 * @Author wf
 * @Date 2020/7/6 18:14
 * @Version 1.0
 */
public class MyCondition implements Condition {
    @Override
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //TODO 这里可以写对应的判断逻辑
        return false;
    }
}

 

说明:

  子类中实现match方法,进行条件判断,如果返回false,则不允许bean装配。

3.bean定义
package com.wf.demo.springbootdemo.condition;

/**
 * @ClassName DemoService
 * @Description service bean类
 * @Author wf
 * @Date 2020/7/6 18:10
 * @Version 1.0
 */
public class DemoService {
}

 

4.测试bean实例获取
package com.wf.demo.springbootdemo.condition;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @ClassName ConditionMain
 * @Description 测试类
 * @Author wf
 * @Date 2020/7/6 18:17
 * @Version 1.0
 */
public class ConditionMain {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        DemoService bean = context.getBean(DemoService.class);
        System.out.println(bean);
    }
}

 

测试结果如下:

技术分享图片

 

 

 结果抛出异常,因为条件返回false,不允许装配。

 

下面修改match方法,返回true,如下所示:

技术分享图片

 

 

 再次运行测试,成功装配,如下所示:

技术分享图片

 

 

5.1.2.spring5.X版本注解驱动

引入@Indexed注解。是用来提升性能的。当项目文件目录很多时,扫描@Component,@Service。。。这些注解时,

就会很耗时,而@Indexed注解,可以提升注解扫描的性能。

 

总结

  spring的注解驱动的发展,是为了bean装配更加简单。

  

下面通过springboot中整合redis来说明,注解驱动的简便性。

5.1.2.1.springboot整合redis示例

1.pom.xml中引入redis依赖
 <!--整合redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

 

版本由springboot-parent来管理。

2.redis服务安装

  在生产中会有专门的redis服务安装redis服务。而学习环境下,一般是在虚拟机上安装redis应用服务器。

3.controller中引用redis代码
package com.wf.demo.springbootdemo.web;

import com.wf.demo.springbootdemo.dao.pojo.User;
import com.wf.demo.springbootdemo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName HelloController
 * @Description 测试controller
 * @Author wf
 * @Date 2020/7/6 11:27
 * @Version 1.0
 */
@RestController
public class HelloController {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //配置文件注入
//    @Value("Mic")
//    private String name;
    @Autowired
    private IUserService userService;

    @GetMapping("hello")
    public String test(){
        User user = new User();
        user.setId(18);
        user.setName("MIc");
        userService.insert(user);
        return "success";
    }

    @GetMapping("/say")
    public String say(){
        return redisTemplate.opsForValue().get("name");
    }
}

 

说明:

  springboot已经为了我们使用redis封装了客户端工具类RedisTemplate。

  只有使用它的api即可。

4.配置redis连接参数
spring.redis.host=192.168.216.128
#redis.port=6379默认

 

5.运行项目,测试

  只需要启动main,浏览器端访问controller接口即可。

 

说明:

  我们可以看到,springboot整合redis是如此之简单。

  这里RedisTemplate实例,能够直接引用,说明spring IOC容器中已经完成bean的装配。

  但是,我们是没有做这部分工作的,而是由springboot进行了装配。

【1】回顾spring4.X中bean装配方式

  》xml配置

  》@Configuration注解装配

  》@Enable装配

现在,springboot提供了自动装配的功能。  

5.2.springboot自动装配

  自动装配的原理是什么?是如何实现的呢?

5.2.1.spring动态Bean的装配方案

主要有两种:
ImportSelector:

Registator

  所谓动态装载,即根据上下文,或某些条件去装载一些配置类。

下面通过代码示例,来说明springboot实现批量扫描配置类的原理。

    所谓批量扫描,就是一次性扫描所有jar包中的配置类。示例代码中以分包的形式,来模拟不同jar包的扫描。

 

 

5.3.springboot的starter原理

4.1.6.springboot核心特性及设计思想【下】

springboot03_核心特性及设计思想

原文:https://www.cnblogs.com/wfdespace/p/13256570.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!